柚子快報激活碼778899分享:linux操作es 命令
柚子快報激活碼778899分享:linux操作es 命令
1.檢查ES節(jié)點是否正常啟動
curl http://192.168.6.16:9200
正常狀態(tài):
非正常狀態(tài):
1>確保服務(wù)是不是正常啟動了,端口用的是哪個
2>防火墻是否關(guān)閉或者端口是否開放
3>你的curl命令是否有問題,curl命令可能導(dǎo)致服務(wù)無法訪問,可以嘗試重啟服務(wù)后,在外部瀏覽器訪問URL地址即可。不一定非得用curl
2.cat檢測集群健康狀況
curl http://192.168.6.16:9200/_cat/health?v
?
綠色表示一切正常, 黃色表示所有的數(shù)據(jù)可用但是部分副本還沒有分配,紅色表示不可用
3.查詢es中所有索引,所有已存在的索引
curl http://192.168.6.16:9200/_cat/indices?v
?
4.創(chuàng)建新的索引【索引要求是全小寫字符,可以有下劃線隔開】
curl -XPUT http://192.168.6.16:9200/my_new_index?pretty
再查看:
curl http://192.168.6.16:9200/_cat/indices?v
?
5.對新增的索引,插入一條數(shù)據(jù)
type是user, id指定為1
curl -XPUT http://192.168.6.16:9200/my_new_index/user/1?pretty -d '{"name":"張三","age":"23"}'
?
6.根據(jù)ID,獲取剛剛索引中新增的數(shù)據(jù)
curl -XGET http://192.168.6.16:9200/my_new_index/user/1?pretty
?
7.修改數(shù)據(jù)
7.1先新增一條數(shù)據(jù)
curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四","age":"25"}'
7.2 根據(jù)ID查詢這條數(shù)據(jù)
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
?
7.3修改id為2的數(shù)據(jù)
curl -XPUT http://192.168.6.16:9200/my_new_index/user/2?pretty -d '{"name":"李四修改","age":"28"}'
即使用相同的新增命令操作 相同的ID,數(shù)據(jù)不同
?
7.4查詢修改結(jié)果
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
?
8.更新數(shù)據(jù),使用POST請求,注意請求體,格式
curl -XPOST http://192.168.6.16:9200/my_new_index/user/2/_update?pretty -d '{"doc":{"name":"李四更新","age":"230"}}'
?
查看更新后的數(shù)據(jù):
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
?
9.更新數(shù)據(jù)的同時,新增列
就是將doc中的json數(shù)據(jù)列增加即可
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字段字符串類型,修改為數(shù)字類型,并使用簡單腳本對其操作
10.1 查看數(shù)據(jù)
curl -XGET http://192.168.6.16:9200/my_new_index/user/2?pretty
?
10.2 將age類型由字符串更改為數(shù)值
就是將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 查看修改后數(shù)據(jù)
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.刪除數(shù)據(jù),根據(jù)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} '
?
想要看插入以后索引下的數(shù)據(jù),查詢在后面16
14.批處理語句,bulk,更新id為1的數(shù)據(jù),刪除id為3的數(shù)據(jù)
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.導(dǎo)入批量數(shù)據(jù)集文件json文件【使用bulk批量導(dǎo)入】
測試的json批量數(shù)據(jù)集文件,java生成代碼:
publicstaticvoidmain(String[] args) { File file=newFile("E:\1\myjson.json"); FileWriter writer=null;intsize = 200;try{ writer=newFileWriter("E:\1\myjson.json");for(inti = 10; i < size+10; i++) { writer.write("{"index":{"_id":""+i+""}}"+" "+"{"name":"張三"+i+"","age": "+i+","address":"北京"+i+""}"+" "); } writer.flush(); }catch(IOException e) { e.printStackTrace(); }finally{try{ writer.close(); }catch(IOException e) { e.printStackTrace(); } } }
View Code
如果報錯,解決方案:https://www.cnblogs.com/sxdcgaq8080/p/11119883.html
指定要導(dǎo)入的 索引、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.查詢某個索引中的所有數(shù)據(jù)
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.查詢指定索引下的數(shù)據(jù)
【如果不指定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的數(shù)據(jù)
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '{"query":{"match":{"age":200} } }'
?
21.2 查詢address中包含 “北京” 的數(shù)據(jù)
curl -XPOST http://192.168.6.16:9200/my_new_index/_search?pretty -d '{"query":{"match":{"address":"北京"} } }'
?
21.3 查詢 address中 包含“北京” 或 “西安”的所有數(shù)據(jù) 【匹配單個詞語 空格分隔】
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進行聚合分組,然后按照記錄數(shù),從大到小排序,默認返回前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
?
==========================================
柚子快報激活碼778899分享:linux操作es 命令
參考閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。