【elasticsearch】Linux上操作elasticsearch的命令
1、检查es节点是否正常启动
curl http://10.10.1.6:9200
2、检测集群健康状态
curl http://10.10.1.6:9200/_cat/health?v
# 哪些索引状态是 yellow ?
GET /_cat/indices?v&health=yellow
# 哪些索引的文件数最多?
GET /_cat/indices?v&s=docs.count:desc
# 指定显示哪些列
GET /_cat/indices/twitter?pri&v&h=health,index,pri,rep,docs.count,mt
# 每一个索引使用了多少内存?
GET /_cat/indices?v&h=i,pri.memory.total,tm&s=tm:desc
# 通过help参数,查看所有的参数释义
GET _cat/indices?help
绿色:正常
黄色:数据可用但是部分副本还没有分配
红色:不可用
3、查询es中所有索引
curl http://10.10.1.6:9200/_cat/indices?v
4、创建新索引(小写字符)
curl -XPUT http://10.10.1.6:9200/test_index?pretty
test_index:新索引名
5、给索引插入数据
curl -XPUT http://10.10.1.6:9200/test_index/user/1?pretty -d '{"name":"张三","age":"23"}'
type为user,id为1
6、根据id,获取索引中的数据
curl -XGET http://10.10.1.6:9200/test_index/user/1?pretty
7、修改索引中的数据(使用put)
即使用相同的新增命令操作,相同的ID,数据不同
curl -XPUT http://10.10.1.6:9200/test_index/user/1?pretty -d '{"name":"张三修改","age":"23"}'
8、修改索引中的数据(使用post)
curl -XPOST http://10.10.1.6:9200/test_index/user/1/_update?pretty -d '{"doc":{"name":"张三修改","age":"23"}}'
也可以在修改数据的同时新增字段
curl -XPOST http://10.10.1.6:9200/test_index/user/1/_update?pretty -d '{"doc":{"name":"张三修改","age":"23","address":"北京东直门"}}'
9、根据id删除索引数据
curl -XDELETE http://10.10.1.6:9200/test_index/user/1?pretty
10、批量插入bulk
curl -XPOST http://10.10.1.6:9200/test_index/user/_bulk?pretty -d '
{"index":{"_id":"2"}}
{"name":"李四","age":25}
{"index":{"_id":"3"}}
{"name":"王五","age":26}
'
11、批量处理语句
curl -XPOST http://10.10.1.6:9200/test_index/user/_bulk?pretty -d '
{"update":{"_id":"1"}}
{"doc": {"name":"张三变李四","age":25}}
{"delete":{"_id":"3"}}
'
12、导入批量数据集文件json文件
curl -XPOST http://10.10.1.6:9200/test_index/user/_bulk?pretty --data-binary @/home/.../testdata.json
13、查询某个索引的所有数据
curl http://10.10.1.6:9200/test_index/_search?q=*&pretty
等价于
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"query":{
"match_all":{
}
}
}'
14、查询某个索引的数据
不指定size则默认查询10条
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"query":{
"match_all":{
}
},
"size":10
}'
15、分页查询某个索引的数据
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"query":{
"match_all":{
}
},
"from": 1,
"size": 10
}'
16、排序查询某个索引的数据
按照age字段倒序排序 sort,取出20条
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"query":{
"match_all":{
}
},
"sort":{
"age":{
"order":"desc"
}
},
"from": 0,
"size": 20
}'
17、查询某个索引数据时,只查询部分字段
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"query":{
"match_all":{
}
},
"_source":[
"name",
"address"
]
}'
18、条件匹配查询
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"query":{
"bool":{
"must/should/must_not":[
{
"match/match_phrase":{
"address":"南京"
}
},
{
"match/match_phrase":{
"address":"上海"
}
}
]
}
}
}'
19、限定范围查询
curl -XPOST http://10.10.16:9200/test_index/_search?pretty -d '
{
"query":{
"range":{
"age":{
"gte":25,
"lte":30
}
}
}
}'
20、聚合查询,分组查询
按照name进行聚合分组,然后按照记录数,从大到小排序,默认返回前10条
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"size":0,
"aggs":{
"group_by_name":{
"terms":{
"field":"name"
}
}
}
}'
21、聚合查询,求平均值
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"size":0,
"aggs":{
"average_age":{
"avg":{
"field":"age"
}
}
}
}'
22、聚合查询,按name分组,求age的平均值
curl -XPOST http://10.10.1.6:9200/test_index/_search?pretty -d '
{
"size":0,
"aggs":{
"group_by_name":{
"terms":{
"field":"name"
},
"aggs":{
"average_age":{
"avg":{
"field":"age"
}
}
}
}
}
}'
23、删除索引
curl -XDELETE http://10.10.1.6:9200/test_index?pretty
24、重建索引
由于elasticsearch索引创建之后分片个数、mapping定义等都不支持修改,只能通过重建索引来完成,所以这里需要用到API_reindex
a 创建一个新的索引new_index,mapping按自己的需求去定义即可,包含节点、副本、分词器、mapping字段定义
PUT /new_index
{
}
b 重建索引
POST _reindex { "source": { "index": "old_index" }, "dest": { "index": "new_index" } }
c 查看重建索引的设置
GET /new_index
d 删除旧索引
DELETE /old_index
e 创建索引别名
POST /_aliases { "actions": [ { "add": { "index": "new_index", "alias": "old_index" } } ] }
f 如果要调整副本数
PUT new_index/_settings { "number_of_replicas" : 1 }
25、索引模板使用
模板的主要作用:可以帮助简化创建索引的语句,将模板中的配置和映射应用到创建的索引中。
新建索引时,索引名称满足index_patterns条件的,将会使用索引模板中的配置和映射。index_patterns使用*进行通配,不支持复杂的正则。
indexPattern要求:
不能包含空字符
不能以_开头
不能包含以下特殊字符
\ / ? " < > | , #
如果索引匹配了多个索引模板,将通过order,按升序逐个应用和覆盖相同的配置和映射,order默认值为0,如果多个模板的order一致,则模板应用顺序不可控。
a 创建&修改模板
创建模板
PUT _template/original_data_face_template { "index_patterns": [ "original_data_face_index*" ], "order": 0, "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 1 }, "analysis": { "analyzer": { "ngram_analyzer": { "tokenizer": "ngram_tokenizer" } }, "tokenizer": { "ngram_tokenizer": { "type": "ngram", "min_gram": 1, "max_gram": 256 } } } }, "aliases": { "alias_1": {} }, "mappings": { "_doc": { "dynamic": "false", "properties": { "captureTime": { "type": "long" }, "createdTime": { "type": "long" }, ... } } } }
创建索引
PUT original_data_face_index_202209
b 多模板应用
新建第二个模板
PUT _template/original_data_face_template_2 { "index_patterns": [ "original_data_face_index*" ], "order": 0, "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 1 }, "analysis": { "analyzer": { "ngram_analyzer": { "tokenizer": "ngram_tokenizer" } }, "tokenizer": { "ngram_tokenizer": { "type": "ngram", "min_gram": 1, "max_gram": 256 } } } }, "aliases": { "alias_1": {} }, "mappings": { "_doc": { "dynamic": "false", "properties": { "captureTime": { "type": "long" }, "createdTime": { "type": "long" }, ... } } } }
创建索引
PUT original_data_face_index_202209
查询索引结果,可以发现同时应用了模板 original_data_face_template 和 original_data_face_template_2
c 查询模板
查询所有的模板列表
GET _cat/templates?v&s=name
验证模板是否存在,通过HTTP状态码来判断, 200 表示存在,404 表示不存在
HEAD _template/original_data_face_template
查询模板详细内容
GET _template/original_data_face_template, original_data_face_template_2
查询所有模板的详细内容
GET /_template
通配符查询
GET /_template/original_data_face_template*
d 删除模板
单个模板删除
DELETE /_template/original_data_face_template
通配符删除
DELETE /_template/original_data_face_template*
不支持多个模板名以逗号隔开的方式进行删除,不支持 DELETE /_template/template_1,template_2
e 版本化模板
模板可以选择添加一个版本号,它可以是任何整数值,以简化外部系统对模板的管理。 version 字段是完全可选的,它仅用于模板的外部管理。 要取消设置版本,只需替换模板而不指定模板。
PUT /_template/template_1 { "index_patterns" : ["*"], "order" : 0, "settings" : { "number_of_shards" : 1 }, "version": 123 }
直接查询版本
GET /_template/template_1?filter_path=*.version
26、es数据迁移
a 在新es中添加白名单
在elasticsearch.yml中添加
reindex.remote.whitelist: ["xx.xx.xx.xx:9200"]
重启新集群
b 利用kibana修改新es参数
PUT _settings?pretty { "index.refresh_interval": -1, "index.number_of_replicas": 0 }
c 使用reindex拉取数据
(1)索引不拆分,迁移数据
PUT basic { "mappings": { "properties": { "operateTime": { "type": "long" } } } }
POST _reindex?wait_for_completion=false { "source": { "remote": { "host": "http://xx.xx.xx.xx:9200" }, "size": 5000, "index": "basic" }, "dest": { "index": "basic" } } #查看迁移状态 GET _tasks/xxx?pretty
(2)索引拆分,迁移数据
PUT basic-2021.02 { "mappings": { "properties": { "operateTime": { "type": "long" } } } }
POST _reindex?wait_for_completion=false { "source": { "remote": { "host": "http://10.0.102.12:8920" }, "size": 5000, "index": "basic", "query": { "bool": { "filter": [ { "range": { "operateTime": { "gte": 1612108800000, "lte": 1614528000000 } } } ] } } }, "dest": { "index": "basic-2021.02" } }
d 查看迁移状况
查看所有task
GET _tasks?detailed=true&actions=*reindex
使用taskId查看指定task详情
GET _tasks/xxx?pretty
e 利用kibana恢复新es设置
PUT _settings?pretty { "index.refresh_interval": "10m", "index.number_of_replicas": 1 }