ES常用命令
1.快速檢查集群的健康狀況
? ? ? GET /_cat/health?v??
Green:每個索引的primary shard和replica shard都是active狀態(tài)
yellow:每個索引的primary shard都是active狀態(tài),但是部分replica shard不是active狀態(tài),處于不可用狀態(tài)。
red:不是所有索引的primary shard都是active狀態(tài),部分索引有數(shù)據(jù)丟失。
2.快速查詢集群中有哪些索引
? ? ? GET /_cat/indices?v
3.簡單的索引操作
創(chuàng)建索引:PUT? /test_index?pretty
刪除索引:DELETE /test_index?pretty
4.商品的CRUD操作
1)新增
PUT /index/type/id
{}
2)查詢
GET /index/type/id
3) 修改
POST /index/type/id/_update
4) 刪除
DELETE /index/type/id
搜索(6):
1、query string search
搜索全部商品:GET /index/type/_search
搜索商品名稱中包含yaogao的商品,而且按照售價降序排序:GET /index/type/_search?q=name:yagao&sort=price:desc
適用于臨時的在命令行使用一些工具,比如curl,快速的發(fā)出請求,來檢索想要的信息,但是如果查詢請求很復雜,很難構(gòu)建在生產(chǎn)環(huán)境中,幾乎很少使用query string search
?2、query DSL
GET /index/type/_search
{
? ? ?"query":{?"match_all":{}}
}
GET /index/type/_search
{
? ? "query":{"match":{"name":"yaogao"}},
? ? "sort":[{"price":"desc"}]
}
分頁查詢
GET /index/type/_search
{
? ? "query":{"match_all":{}},
? ? ?"from":1,
? ? ?"size":2
}
GET /index/type/_search
{
? ? "query":{"match_all":{}},
? ? ?"_source":["name","price"]
}
3、query filter(篩選,過濾)
GET /index/type/_search
{
? ? ?"query":{
? ? ? ? ? ? "bool":{
? ? ? ? ? ? ? ? ? "must":{"match":{"yagao"}},
? ? ? ? ? ? ? ? ? "filter":{"range":{"price":{"gt":25}}}
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ?}
}
4、full-text search(全文檢索)
GET /index/type/_search
{?
? ? ?"query":{
? ? ? ? ? ? ? ? ? ?"match":{"producer":"yagao producer" }? ?}
}
5、pharse search(短語搜索)
跟全文檢索相對應(yīng),全文搜索只要搜索包含一個單詞即可
短語搜索,要匹配整個短語
GET /index/type/_search
{?
? ? ?"query":{
? ? ? ? ? ? ? ? ? ?"match_pharse":{"producer":"yagao producer" }? ?}
}
6、helight search(高亮顯示)
GET /index/type/_search
{? ??
? ? ? "query":{"match_pharse":{"producer":"yagao producer"}},
? ? ? ?"highlight":{"fileds":{"producer":{}}}
}
}
聚合分析:
1、按年級分級
GET /index/type/_search
{
? ? "aggs":{"group_by_grade":{
? ? ? ? ? ? "terms":{"filed":"grade"}
? ? ? ? ? ?}}
}
2、每個年級的平均值
GET test_index/liuliTest/_search
{
"size": 0,
"aggs":{
"group_by_grade":{
? ? ? ? ? ? ? ? ? "terms": {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "field": "grade"
? ? ? ? ? ? ? ? ? ? ? ? ? ?},
? ? ? ? ? ? ? ? ? ?"aggs":{
? ? ? ? ? ? ? ? ? ? ? ? ?"agg_price":{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"avg":{"field":"score"}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
}
3、按平均值降序
GET test_index/liuliTest/_search
{
? ? ? ? ? ?"size": 0,
? ? ? ? ? ?"aggs":{
? ? ? ? ? ? "group_by_grade":{
? ? ? ? ? ? ? ? ? ?"terms": {
? ? ? ? ? ? ? ? ? ? ? "field": "grade",
? ? ? ? ? ? ? ? ? ? ? "order": {
? ? ? ? ? ? ? ? ? ? ? ? ? ?"agg_price": "desc"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ?"aggs":{
? ? ? ? ? ? ? ? ? ? ? "agg_price":{
? ? ? ? ? ? ? ? ? ? ? ? ? ?"avg":{"field":"score"}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? }
}
4、按成績區(qū)間進行分組,再按年級分組,再求每組平均分。
GET test_index/liuliTest/_search
{
? ? ? ? ? ? ? "size": 0,
? ? ? ? ? ? ?"aggs":{
? ? ? ? ? ? ? ? ? ?"group_by_score":{
? ? ? ? ? ? ? ? ? ? ? ? "range": {
? ? ? ? ? ? ? ? ? ? ? ? ? ? "field": "score",
? ? ? ? ? ? ? ? ? ? ? ? ? ? "ranges":[
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"from":0,"to":80},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"from":80,"to":90},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{"from":90,"to":100}]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?},
? ? ? ? ? ? ? ? ? ? "aggs":{
? ? ? ? ? ? ? ? ? ? ? ? ? ?"group_by_grade":{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "terms":{"field":"grade"},
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"aggs":{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "score_avg":{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"avg":{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"field":"score"}? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
5、批量查詢
GET _mget
{
"docs":[
? ? {
? ? ? ? ? "_index":"test_index",
? ? ? ? ? "_type":"liuliTest",
? ? ? ? ? "_id":"1"
? ? },{
? ? ? ? ?"_index":"test_index",
? ? ? ? ?"_type":"liuliTest",
? ? ? ? ?"_id":"2"
? ? ? ?}
]
}
GET test_index/_mget
{
? ? ?"docs":[
? ? {
? ? ? ? "_type":"liuliTest",
? ? ? ? ?"_id":"1"
? ? ?},{
? ? ? ? "_type":"liuliTest",
? ? ? ? "_id":"2"
? ? ? ? ?}]
}
批量插入:
POST _bulk
{ "index" : { "_index" : "test_index", "_type" : "type1", "_id" : "3" } }
{ "name" : "name改了" }
{"create":{"_index":"test_index","_type":"type1","_id":"4"}}
{"name":"woshigaide"}
{"delete":{"_index":"test_index","_type":"type1","_id":"4"}}
index和create的區(qū)別
index :索引存在則修改,不存在創(chuàng)建
create:索引存在,報版本沖突,拋異常,不存在則創(chuàng)建。
?
轉(zhuǎn)載于:https://www.cnblogs.com/gyll/p/8941870.html
總結(jié)
- 上一篇: rect用法
- 下一篇: hadoop28---netty传对象