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)            
| 
 
 | 
新聞熱點
疑難解答