1、查看集群狀態
curl '10.18.37.223:9200/_cat/health?v'
綠色表示一切正常, 黃色表示所有的數據可用但是部分副本還沒有分配,紅色表示部分數據因為某些原因不可用
2、獲取集群節點列表
curl '10.18.37.223:9200/_cat/nodes?v'
3、查看所有index
curl -X GET 'http://10.18.37.223:9200/_cat/indices?v'
4、查詢所有的index包含其所有的type
curl '10.18.37.223:9200/_mapping?pretty=true'
5、查詢某個index下的所有type
curl '10.18.37.223:9200/test/_mapping?pretty=true' 查詢test下的所有type
6、查詢某個index的所有數據
curl '10.18.37.223:9200/test/_search?pretty=true'
7、查詢index下某個type類型的數據
curl '10.18.37.223:9200/test/test_topic/_search?pretty=true'
其中:根據規划,Elastic 6.x 版只允許每個 Index 包含一個 Type,7.x 版將會徹底移
除 Type, index=test type=test_topic 注意自己使用的版本
8、查詢index下某個type下id確定的數據
curl '10.18.37.223:9200/test/test_topic/3525?pretty=true'
index = test type= test_topic id = 3525
9、和sql一樣的查詢數據
curl "10.18.37.223:9200/test/_search" -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"],
"sort": { "balance": { "order": "desc" },
"from": 10,
"size": 10
}
'
注:-d之后的內容使用回車輸入,不能使用換行符,es不能識別
query:里面為查詢條件此處為全部,不做限制,_source:為要顯示的那些字段
sort:為排序字段 from為從第10條開始,size:取10條
除此之外還有:布爾匹配,or匹配。包含匹配。范圍匹配。更多查詢請去官網查看:
官網查詢API地址
10、創建索引(index)
curl -X PUT '10.18.37.223:9200/test?pretty'
OR
curl -X PUT '10.18.37.223:9200/test'
創建一個名為test的索引
注:索引只能是小寫,不能以下划線開頭,也不能包含逗號
如果沒有明確指定索引數據的ID,那么es會自動生成一個隨機的ID,需要使用POST參數
11、往index里面插入數據
curl -X PUT '10.18.37.223:9200/test/test_zhang/1?pretty' -d '
{"name":"tom","age":18}'
往es中插入index=test,type=test_zhang id = 1的數據為
{"name":"tom","age":18}的數據。
-X POST也即可
12、修改數據
curl -X PUT '10.18.37.223:9200/test/test_zhang/1?pretty' -d '{"name":"pete","age":20}'
注:修改 index = test type=test_zhang id = 1 數據: {"name":"tom","age":18}
為{"name":"pete","age":20} 成功之后執行查看數據命令可看到最新數據,且
version 會增加一個版本
13、更新數據同時新增數據,在一個index,type中
curl -X POST '10.18.37.223:9200/test/test_zhang/1/_update?pretty' -d '{"doc":{"name":"Alice","age":18,"addr":"beijing"}}'
注:修改了名字,年齡,同時新增了字段addr=beijing
14、利用script更新數據
curl -X POST '10.18.37.223:9200/test/test_zhang/1/_update?pretty' -d '{"script": "ctx._source.age += 5"}'
注:將年齡加5
從ES 1.4.3以后, inline script默認是被禁止的
要打開, 需要在config/elasticsearch.yml中添加如下配置:
script.inline:true
script.indexed:true 然后重啟 (如果是集群模式:需要每個節點都添加 然后重啟)
15、刪除記錄
curl -X DELETE '10.18.37.223:9200/test/test_zhang/1'
注:刪除index = test type = test_zhang id = 1 的數據
16、刪除index
curl -X DELETE '10.18.37.223:9200/test'
刪除index=test的數據
17、批量操作
curl -X POST '10.18.37.223:9200/test/test_zhang/_bulk?pretty' -d '
{"index":{"_id":"2"}}
{"name":"zhangsan","age":12}
{"index":{"_id":"3"}}
{"name":"lisi"}
'
注:在index = test type = test_zhang下
新增id= 2 和 id=3 的兩條數據
curl -X POST '10.18.37.223:9200/test/test_zhang/_bulk?pretty' -d '
{"update":{"_id":"2"}}
{"doc":{"name":"wangwu"}}
{"delete":{"_id":"3"}}'
注: 修改id = 2 的數據 並且同時刪除掉id=3的數據
在index = test type = test_zhang下
18、根據條件刪除
curl -X POST "10.18.37.223:9200/test/_delete_by_query" -d'
{
"query": {
"match": {
"name": "pete"
}
}
}'
使用es的_delete_by_query,此插件在es2.0版本以后被移除掉,要使用此命令。
需要自己安裝_delete_by_query插件:
在es安裝目錄下。bin目錄下,執行:
./plugin install delete-by-query 安裝插件
如果是集群模式,則每個節點都需要安裝然后重啟