Elasticsearch 是一個開源的搜尋引擎,建立在一個全文檢索搜尋引擎庫 Apache Lucene 基礎之上。 Lucene 可能是目前存在的,不論開源還是私人的,擁有最先進,高效能和全功能搜尋引擎功能的庫。但是 Lucene 僅僅只是一個庫。為了利用它,你需要編寫 Java 程式,並在你的 java 程式裡面直接整合 Lucene 包。 更壞的情況是,你需要對資訊檢索有一定程度的理解才能明白 Lucene 是怎麼工作的。Lucene 是 很 複雜的。
在上一篇部落格中介紹了Elasticsearch的簡單使用,接下來記錄一下Elasticsearch的查詢: 查詢所有資料
# 搜尋所有資料es.search(index="my_index",doc_type="test_type")# 或者body = { "query":{ "match_all":{} }}es.search(index="my_index",doc_type="test_type",body=body)
term與terms
# termbody = { "query":{ "term":{ "name":"python" } }}# 查詢name="python"的所有資料es.search(index="my_index",doc_type="test_type",body=body)# termsbody = { "query":{ "terms":{ "name":[ "python","android" ] } }}# 搜尋出name="python"或name="android"的所有資料es.search(index="my_index",doc_type="test_type",body=body)
match與multi_match
# match:匹配name包含python關鍵字的資料body = { "query":{ "match":{ "name":"python" } }}# 查詢name包含python關鍵字的資料es.search(index="my_index",doc_type="test_type",body=body)# multi_match:在name和addr裡匹配包含深圳關鍵字的資料body = { "query":{ "multi_match":{ "query":"深圳", "fields":["name","addr"] } }}# 查詢name和addr包含"深圳"關鍵字的資料es.search(index="my_index",doc_type="test_type",body=body)
ids
body = { "query":{ "ids":{ "type":"test_type", "values":[ "1","2" ] } }}# 搜尋出id為1或2d的所有資料es.search(index="my_index",doc_type="test_type",body=body)
複合查詢bool
bool有3類查詢關係,must(都滿足),should(其中一個滿足),must_not(都不滿足)
body = { "query":{ "bool":{ "must":[ { "term":{ "name":"python" } }, { "term":{ "age":18 } } ] } }}# 擷取name="python"並且age=18的所有資料es.search(index="my_index",doc_type="test_type",body=body)
切片式查詢
body = { "query":{ "match_all":{} } "from":2 # 從第二條資料開始 "size":4 # 擷取4條資料}# 從第2條資料開始,擷取4條資料es.search(index="my_index",doc_type="test_type",body=body)
範圍查詢
body = { "query":{ "range":{ "age":{ "gte":18, # >=18 "lte":30 # <=30 } } }}# 查詢18<=age<=30的所有資料es.search(index="my_index",doc_type="test_type",body=body)
首碼查詢
body = { "query":{ "prefix":{ "name":"p" } }}# 查詢首碼為"趙"的所有資料es.search(index="my_index",doc_type="test_type",body=body)
萬用字元查詢
body = { "query":{ "wildcard":{ "name":"*id" } }}# 查詢name以id為尾碼的所有資料es.search(index="my_index",doc_type="test_type",body=body)
排序
body = { "query":{ "match_all":{} } "sort":{ "age":{ # 根據age欄位升序排序 "order":"asc" # asc升序,desc降序 } }}
filter_path
響應過濾
# 只需要擷取_id資料,多個條件用逗號隔開es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"])# 擷取所有資料es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"])
count
執行查詢並擷取該查詢的匹配數
# 擷取資料量es.count(index="my_index",doc_type="test_type")
度量類彙總 擷取最小值
body = { "query":{ "match_all":{} }, "aggs":{ # 彙總查詢 "min_age":{ # 最小值的key "min":{ # 最小 "field":"age" # 查詢"age"的最小值 } } }}# 搜尋所有資料,並擷取age最小的值es.search(index="my_index",doc_type="test_type",body=body) 擷取最大值
body = { "query":{ "match_all":{} }, "aggs":{ # 彙總查詢 "max_age":{ # 最大值的key "max":{ # 最大 "field":"age" # 查詢"age"的最大值 } } }}# 搜尋所有資料,並擷取age最大的值es.search(index="my_index",doc_type="test_type",body=body) 擷取和
body = { "query":{ "match_all":{} }, "aggs":{ # 彙總查詢 "sum_age":{ # 和的key "sum":{ # 和 "field":"age" # 擷取所有age的和 } } }}# 搜尋所有資料,並擷取所有age的和es.search(index="my_index",doc_type="test_type",body=body) 擷取平均值
body = { "query":{ "match_all":{} }, "aggs":{ # 彙總查詢 "avg_age":{ # 平均值的key "sum":{ # 平均值 "field":"age" # 擷取所有age的平均值 } } }}# 搜尋所有資料,擷取所有age的平均值es.search(index="my_index",doc_type="test_type",body=body)
更多的搜尋用法:
https://elasticsearch-py.readthedocs.io/en/master/api.html