主要是把Elasticsearch QueryDSL查詢入門的部分進行了翻譯總結,用以備查
注:'bank' 爲示例索引sql
==> 列出全部index:
curl 'localhost:9200/_cat/indices?v
curl
檢出索引(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": {} } }'
匹配全部並返回第一個文檔排序
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "size": 1 }'
匹配全部並返回第11到20索引
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "from": 10, "size": 10 }'
匹配全部根據balance降序(DESC)排序並返回前10條數據文檔
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "sort": { "balance": { "order": "desc" } } }'
-- ! 注意 !:上述中若size未指定默認爲10it
返回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"] }'
返回account_number爲20的,至關於sql的WHERE
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "account_number": 20 } } }'
返回address中包含'mill'或'lane'的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "address": "mill lane" } } }'
返回address中包含'mill lane'的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_phrase": { "address": "mill lane" } } }'
返回address中包含'mill'和'lane'的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
返回address中包含'mill'或'lane'的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
返回address中不包含'mill'和'lane'的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
返回age是40但state不是'ID'的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }'
返回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 } } } } } }'
按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" } } } }'
按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" } } } } } }'
同上一個,但,本例按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" } } } } } }'
按年齡(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" } } } } } } } }'