==========
1.檢查ES節點是否正常啟動
curl http://192.168.6.16:9200
正常狀態:
非正常狀態:
1>確保服務是不是正常啟動了,端口用的是哪個
2>防火牆是否關閉或者端口是否開放
3>你的curl命令是否有問題,curl命令可能導致服務無法訪問,可以嘗試重啟服務后,在外部瀏覽器訪問URL地址即可。不一定非得用curl
2.cat檢測集群健康狀況
curl http://192.168.6.16:9200/_cat/health?v
綠色表示一切正常, 黃色表示所有的數據可用但是部分副本還沒有分配,紅色表示不可用
3.查詢es中所有索引,所有已存在的索引
curl http://192.168.6.16:9200/_cat/indices?v
4.創建新的索引【索引要求是全小寫字符,可以有下划線隔開】
curl -XPUT http://192.168.6.16:9200/my_new_index?pretty
再查看:
curl http://192.168.6.16:9200/_cat/indices?v
5.對新增的索引,插入一條數據
type是user, id指定為1
curl -XPUT http://192.168.6.16:9200/my_new_index/user/1?pretty -d '{"name":"張三","age":"23"}'
6.根據ID,獲取剛剛索引中新增的數據
curl -XGET http://192.168.6.16:9200/my_new_index/user/1?pretty
7.修改數據
7.1先新增一條數據
curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四","age":"25"}'
7.2 根據ID查詢這條數據
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
7.3修改id為2的數據
curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四修改","age":"28"}'
即使用相同的新增命令操作 相同的ID,數據不同
7.4查詢修改結果
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
8.更新數據,使用POST請求,注意請求體,格式
curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230"}}'
查看更新后的數據:
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
9.更新數據的同時,新增列
就是將doc中的json數據列增加即可
curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230","address":"北京東直門"}}'
查看:
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
10.將age字段字符串類型,修改為數字類型,並使用簡單腳本對其操作
10.1 查看數據
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
10.2 將age類型由字符串更改為數值
就是將json中的age的值的引號去掉
curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":230,"address":"北京東直門"}}'
10.3 查看修改后數據
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
10.4使用簡單腳本,對年齡增加5
如果報錯。解決方法:https://www.cnblogs.com/sxdcgaq8080/p/11119420.html
curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"script" : "ctx._source.age += 5"}'
查看:
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
11.刪除數據,根據ID刪除
curl -XDELETE http://192.168.6.16:9200/my_new_index/user/2?pretty
13.批量插入 bulk
【注意JSON字符串格式】
curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty -d '
{"index":{"_id":"3"}}
{"name":"趙思","age":12}
{"index":{"_id":"4"}}
{"name":"錢三一","age":13}
'
想要看插入以后索引下的數據,查詢在后面16
14.批處理語句,bulk,更新id為1的數據,刪除id為3的數據
curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty -d '
{"update":{"_id":"1"}}
{"doc": {"name":"張三變李四","age":25}}
{"delete":{"_id":"3"}}
'
15.導入批量數據集文件json文件【使用bulk批量導入】
測試的json批量數據集文件,java生成代碼:

public static void main(String[] args) { File file = new File("E:\\1\\myjson.json"); FileWriter writer = null; int size = 200; try { writer =new FileWriter("E:\\1\\myjson.json"); for (int i = 10; i < size+10; i++) { writer.write("{\"index\":{\"_id\":\""+i+"\"}}"+"\r\n"+"{\"name\":\"張三"+i+"\",\"age\": "+i+",\"address\":\"北京"+i+"\"}"+"\r\n"); } writer.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
如果報錯,解決方案:https://www.cnblogs.com/sxdcgaq8080/p/11119883.html
指定要導入的 索引、type、使用bulk命令 @符號后面跟json文件的絕對路徑
curl -XPOST http://192.168.6.16:9200/my_new_index/user/_bulk?pretty --data-binary @/cjf/es/elasticsearch-2.3.3/data/myjson.json
查看index詳情:
curl http://192.168.6.16:9200/_cat/indices?v
可以看到成功批量插入了200條
===================================下來看查詢(刪除索引在最后)=========================================
16.查詢某個索引中的所有數據
curl http://192.168.6.16:9200/my_new_index/_search?q=*&pretty
等價於
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match_all":{ } } } '
即
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '{"query":{ "match_all":{}}}'
17.查詢指定索引下的數據
【如果不指定size,默認返回10條】
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '
{
"query":{
"match_all":{
}
},
"size":10
}
'
18.分頁查詢,從第10條,返回10條
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match_all":{ } }, "from": 10, "size": 10 } '
19.按照age字段倒序排序 sort,取出20條
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match_all":{ } }, "sort":{ "age":{ "order":"desc" } }, "from": 0, "size": 20 } '
20.只返回想查詢的部分字段
只返回name和address列
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match_all":{ } }, "_source":[ "name", "address" ] } '
21.條件匹配查詢
21.1查詢age=200的數據
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match":{ "age":200 } } } '
21.2 查詢address中包含 “北京” 的數據
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match":{ "address":"北京" } } } '
21.3 查詢 address中 包含“北京” 或 “西安”的所有數據 【匹配單個詞語 空格分隔】
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match":{ "address":"北京 西安" } } } '
21.4 查詢address中包含“北京 西安” 完整詞語的【短語匹配,“北京 西安”作為一個完整詞語查詢】、
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "match_phrase":{ "address":"北京 西安" } } } '
22.布爾查詢 bool
22.1布爾查詢bool and查詢,必須同時滿足 address中包含“北京”,又要滿足address中包含“西安”
must表示所有查詢必須都為真才被認為匹配
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "bool":{ "must":[ { "match":{ "address":"北京" } }, { "match":{ "address":"西安" } } ] } } } '
22.2 布爾查詢bool or查詢 address中包含“北京” 或者 address中包含“西安” 都可以
should 表示查詢列表中只要有任何一個為真則認為匹配
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "bool":{ "should":[ { "match":{ "address":"北京" } }, { "match":{ "address":"西安" } } ] } } } '
22.3 布爾查詢bool 都不能滿足的 既不能包含這個,也不能包含那個
must_not表示查詢列表中沒有為真的(也就是全為假)時則認為匹配
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "bool":{ "must_not":[ { "match":{ "address":"北京" } }, { "match":{ "address":"西安" } } ] } } } '
22.4 這樣,就可以布爾查詢 多條件組合 查詢
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "bool":{ "must":[ { "match":{ "age":200 } } ], "must_not":[ { "match":{ "address":"西安" } } ] } } } '
23. 范圍查詢 range 查詢年齡25-30之間的
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "query":{ "range":{ "age":{ "gte":25, "lte":30 } } } } '
24.聚合查詢 aggs
按照name進行聚合分組,然后按照記錄數,從大到小排序,默認返回前10條
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "size":0, "aggs":{ "group_by_name":{ "terms":{ "field":"name" } } } } '
25. 聚合查詢 aggs ,求age的平均值
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "size":0, "aggs":{ "average_age":{ "avg":{ "field":"age" } } } } '
按name分組,求age的平均值
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d ' { "size":0, "aggs":{ "group_by_name":{ "terms":{ "field":"name" }, "aggs":{ "average_age":{ "avg":{ "field":"age" } } } } } } '
100.刪除索引
100.1 查看所有索引信息
curl http://192.168.6.16:9200/_cat/indices?v
100.2 刪除指定索引
curl -XDELETE http://192.168.6.16:9200/my_new_index?pretty
100.3 再次查看
curl http://192.168.6.16:9200/_cat/indices?v
==========================================