Elasticsearch QueryDSL 經常使用查詢

主要是把Elasticsearch QueryDSL查詢入門的部分進行了翻譯總結,用以備查
注:'bank' 爲示例索引sql

==> 列出全部index: curl 'localhost:9200/_cat/indices?vcurl

1、檢出全部

  1. 檢出索引(index)爲bank的全部文檔(document)url

    • REST request URI方式: curl 'localhost:9200/bank/_search?q=*&pretty翻譯

    • REST request body方式:code

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
       {
         "query": { "match_all": {} }
       }'
  2. 匹配全部並返回第一個文檔排序

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": { "match_all": {} },
       "size": 1
     }'
  3. 匹配全部並返回第11到20索引

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": { "match_all": {} },
       "from": 10,
       "size": 10
     }'
  4. 匹配全部根據balance降序(DESC)排序並返回前10條數據文檔

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": { "match_all": {} },
       "sort": { "balance": { "order": "desc" } }
     }'

-- ! 注意 !:上述中若size未指定默認爲10it

2、搜索

  1. 返回account_number和balance這2個fields(_source裏),至關於sql:SELECT account_number, balance FROM....入門

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": { "match_all": {} },
       "_source": ["account_number", "balance"]
     }'
  2. 返回account_number爲20的,至關於sql的WHERE

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": { "match": { "account_number": 20 } }
     }'
  3. 返回address中包含'mill'或'lane'的

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
     	"query": { "match": { "address": "mill lane" } }
     }'
  4. 返回address中包含'mill lane'的

  5. curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": { "match_phrase": { "address": "mill lane" } }
     }'
  6. 返回address中包含'mill'和'lane'的

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": {
         "bool": {
           "must": [
             { "match": { "address": "mill" } },
             { "match": { "address": "lane" } }
           ]
         }
       }
     }'
  7. 返回address中包含'mill'或'lane'的

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": {
         "bool": {
           "should": [
             { "match": { "address": "mill" } },
             { "match": { "address": "lane" } }
           ]
         }
       }
     }'
  8. 返回address中不包含'mill'和'lane'的

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": {
         "bool": {
           "must_not": [
             { "match": { "address": "mill" } },
             { "match": { "address": "lane" } }
           ]
         }
       }
     }'
  9. 返回age是40但state不是'ID'的

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": {
         "bool": {
           "must": [
             { "match": { "age": "40" } }
           ],
           "must_not": [
             { "match": { "state": "ID" } }
           ]
         }
       }
     }'

3、過濾器

  1. 返回balance在20000和30000之間的(含)(balance大於等於20000,小於等於30000)

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "query": {
         "filtered": {
           "query": { "match_all": {} },
           "filter": {
             "range": {
               "balance": {
                 "gte": 20000,
                 "lte": 30000
               }
             }
           }
         }
       }
     }'
  2. 按state分組並返回前10個(默認),按分組COUNT降序(DESC)排

    • 至關於sql: SELECT COUNT() from bank GROUP BY state ORDER BY COUNT() DESC。

    • 「size=0」是設置不顯示search hit

      curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
        {
          "size": 0,
          "aggs": {
            "group_by_state": {
              "terms": {
                "field": "state"
              }
            }
          }
        }'
  3. 按state分組並計算(組)平均balance(默認返回前10個按state的COUNT的降序(DESC)排)

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "size": 0,
       "aggs": {
         "group_by_state": {
           "terms": {
             "field": "state"
           },
           "aggs": {
             "average_balance": {
               "avg": {
                 "field": "balance"
               }
             }
           }
         }
       }
     }'
  4. 同上一個,但,本例按balance平均數的降序排

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "size": 0,
       "aggs": {
         "group_by_state": {
           "terms": {
             "field": "state",
             "order": {
               "average_balance": "desc"
             }
           },
           "aggs": {
             "average_balance": {
               "avg": {
                 "field": "balance"
               }
             }
           }
         }
       }
     }'
  5. 按年齡(age)20-29, 30-39, 40-49區間分組(GROUP BY), 每一個區間按性別(gender)分組, 計算平均(balance)。按句話說,就是計算不一樣年齡區間的男女平均餘額。

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
     {
       "size": 0,
       "aggs": {
         "group_by_age": {
           "range": {
             "field": "age",
             "ranges": [
               {
                 "from": 20,
                 "to": 30
               },
               {
                 "from": 30,
                 "to": 40
               },
               {
                 "from": 40,
                 "to": 50
               }
             ]
           },
           "aggs": {
             "group_by_gender": {
               "terms": {
                 "field": "gender"
               },
               "aggs": {
                 "average_balance": {
                   "avg": {
                     "field": "balance"
                   }
                 }
               }
             }
           }
         }
       }
     }'
相關文章
相關標籤/搜索