ES常用操作

#查看所有索引
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_cat/indices?v'

#查询索引下所有文档
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d '{"query": { "match_all": {} }}'

#查询索引下所有文档,并返回第一个文档
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d '{"query": { "match_all": {} },"size":1}'

#匹配所有并返回第1到10
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d '{"query": { "match_all": {} },"from": 0,"size": 10}'

#根据属性降序排序(size未指定默认为10)
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d '{"query": { "match_all": {} },"sort": {"status": { "order": "desc" }}}'

#返回name和status这2个fields(_source里),相当于sql:SELECT name, status FROM....
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d '{"query": { "match_all": {} },"_source": ["name", "status"]}'

#返回status为1的,相当于sql的WHERE
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d '{"query": { "match": { "status": 1 } }}'

#返回type=77 && status=1的文档
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d  '{"query": {"bool": {"must": [{ "match": { "type": 77 } },{ "match": { "status": 1 } }]}}}'

#返回type=77 || status=1的文档
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d  '{"query": {"bool": {"should": [{ "match": { "type": 77 } },{ "match": { "status": 1 } }]}}}'

#返回type=77 非 status=1的文档
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d  '{"query": {"bool": {"must_not": [{ "match": { "type": 77 } },{ "match": { "status": 1 } }]}}}'

#返回type是134但status不是2的
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d  '{"query": {"bool": { "must": [{ "match": { "type": 134 } }],"must_not": [{ "match": { "status": 2 } }]}}}'

# 相当于sql: SELECT COUNT() from bank GROUP BY state ORDER BY COUNT() DESC。
#「size=0」是设置不显示search hit
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d   '{"size": 0,"aggs": {"group_by_state": {"terms": {"field": "status"}}}}'

#按state分组并计算(组)平均balance(默认返回前10个按state的COUNT的降序(DESC)排)
curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/yuntu_poi_index_7/_search?pretty' -d   '{"size": 0,"aggs": {"group_by_state": {"terms": {"field": "status"},"aggs": {"average_balance": {"avg": {"field": "type"}}}}}}'

创建索引

curl -XPUT 'http://localhost:9200/test_index'

 创建文档,将一个简单的客户文档索引到customer索引、“external”类型中,这个文档的ID是1,操作如下:

 curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/test_index/external/2' -d '{"name": "Feng zi"}'

 删除文档

curl -XDELETE ‘http://localhost:9200/customer/external/1’

更新文档

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc":{"name":"lisi"}
}'

 给索引起别名

curl --request PUT   --url 'http://xxxxx:9200/faq-oral-exercise-20191112/_alias/faq-oral-exercise-alias'

删除索引,或者多个索引

curl -XDELETE http://10.27.73.65:9200/bot_logs_index_2022-05-18,bot_logs_index_2022-04-19

 修改es副本数

curl -XPUT 'http://localhost:9200/test_index/_settings' -d '{"number_of_replicas": 2}'

 合并索引

curl -XPOST http://127.0.0.1:9200/_reindex -d '{"source": {"index": ["test_index1", "test_index2"]},"dest": {"index": "my_test"}}'

查询索引数据

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/my_test/_search?pretty' -d '{"query": { "match_all": {} }}'

创建快照仓库

curl -XPUT 127.0.0.1:9200/_snapshot/my_backup -d '{"type": "fs","settings": {"location": "/usr/share/elasticsearch/snapshot","compress": true}}'

查看仓库

curl 127.0.0.1:9200/_snapshot/my_backup

快照指定索引

curl -XPUT 127.0.0.1:9200/_snapshot/my_backup/snapshot1 -d '{"indices": "test_index1"}'

清空索引数据

curl -H 'Content-Type: application/json' -XPOST 127.0.0.1:9200/test_index1/_delete_by_query -d '{"query": {"match_all": {}}}'

恢复快照

curl -XPOST 127.0.0.1:9200/_snapshot/my_backup/snapshot1/_restore -d '{"indices": "snapshot1","ignore_unavailable": true,"include_aliases": false}'

 

posted @ 2022-02-16 10:54  力王7314  阅读(102)  评论(0)    收藏  举报