總結一下ElasticSearch中的常用查詢


查詢

curl 'http://localhost:9200/indexName/typeName/docID?pretty

用於查詢指定ID的文檔內容

curl 'http://localhost:9200/indexName/typeName/_search?pretty&size=10

用於查詢指定索引,指定類型下的文檔,使用size參數指定返回文檔數量,默認返回10條

 

curl 'http://localhost:9200/indexName/typeName/_search?pretty&size=10' -d '{"query":{"match":{"fieldName":"value"}}}'

用於查詢文檔中fieldName指定的字段匹配指定value的文檔,注意這里的value是可能被分詞的

合並查詢條件

{"query":{"constant_score":{"filter":{"bool":{"must":[{"term":{"account_id":524055}},{"term":{"action":"download file"}}]}}}}}
{"query":{"constant_score":{"filter":{"bool":{"must":[{"term":{"user_id":1365565}},{"term":{"nsid":1064549}},{"term":{"neid":914594193}}]}}}}}
{"query":{"bool":{"must_not":{"exists":{"field":"path"}}}}}

{"query": {   "bool": {     "must": { "match": { "tweet": "elasticsearch" }},     "must_not": { "match": { "name": "mary" }},     "should": { "match": { "tweet": "full text" }},     "filter": { "range": { "age" : { "gt" : 30 }} }   } }}

{"query":{
  
"bool": {
     "must": { "match": { "email": "business opportunity" }},

      "should": [
            { "match": { "starred": true }},
            { "bool": {
              "must": { "match": { "folder": "inbox" }},
              "must_not": { "match": { "spam": true }}
      }}],

      "minimum_should_match": 1
    }

}}

前綴查詢,不會分析查詢字符串 '{"query":{"prefix":{"postcode": "W1"}}}'

排序  '{"sort":{"age":{"order":"desc"}}}'

聚集 '{"size":0,"aggs":{"total_count":{"terms":{"field":"account_id"}}}}'

查看索引列表
curl 'http://localhost:9200/_cat/indices'

重建索引
{"size":5000,"conflicts":"proceed", "source":{"index":"log"}, "dest":{"index":"log_v2", "op_type":"create","routing":"keep"}}

curl -XPOST 'http://localhost:9200/_reindex?pretty' -d

'{"size":5000,"conflicts":"proceed", "source":{"index":"log"}, "dest":{"index":"log_v2", "op_type":"create","routing":"keep"}}'

curl -XPOST 'http://localhost:9200/_reindex?pretty' -d

'{"conflicts":"proceed", "source":{"index":"log","size":5000}, "dest":{"index":"log_v2", "op_type":"create","routing":"keep"}}'

創建別名
curl -XPUT 'http://localhost:9200/log/_alias/log_v2'

刪除別名
curl -XDELETE 'http://localhost:9200/log/_alias/log_v2'

查看別名
方式一:curl 'http://localhost:9200/*/_alias/log'
方式二:curl 'http://localhost:9200/log/_alias/*'

查看es當前執行的任務列表
curl 'http://localhost:9200/_tasks?pretty'

查看某一個任務執行詳細信息
curl 'http://localhost:9200/_tasks/k0xbXNGRT0uTNQAlJJixqw:1243709676?pretty'

需求某一賬戶,所有子賬戶,最后登錄時間?

先按條件過濾,然后按 user id 分組,最后求每組最大值

curl 'http://localhost:9200/log/_search?pretty' -d

  '{"size":0, "query":{"bool":{"must":[{"term":{"account_id":123456}}, {"term":{"action":"login"}}]}},

  "aggs":{"users":{"terms":{"field":"user_id", "size":20}, "aggs":{"time":{"max":{"field":"@timestamp"}}}}}}'

更新

curl 'http://localhost:9200/indexName/type/docID/_update?pretty' -d '{"doc": {"addNewFieldName": "value"}}'

用於更新ID為docID的文檔,新增字段;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM