Elasticsearch 基本查詢學習
簡單查詢
在 Elasticsearch 當中最簡單的查詢是使用 URI 請求查詢,例如下面的查詢:
http :9200/test/_search q==name:rcx
{
"query" : {
"query_string" : {"query" : "name:rcx"}
}
}</pre>
上面的這兩個查詢返回的結果是相同的,上面的是 URI 方式的查詢,下面的是 DSL 查詢。
當然如果是需要分頁查詢的話可以進行如下方式:
{
"from" : 10,
"size" : 10,
"query" : {
"query_string" : {"query" : "name:rcx"}
}
}
也可以添加如下的參數:
{
"from" : 10,
"size" : 10,
"version" : true,//返回文檔的版本信息
"min_score" : 0.75,//查詢返回的文檔得分高于0.75的
"fields" : ["title", "age"],//查詢返回的字段
"query" : {
"query_string" : {"query" : "name:rcx"}
}
}
理解查詢過程
搜索類型
- query_then_fetch :第一步,執行查詢得到對文檔進行排序和分級所需要信息,在所有分片上執行。然后,只在相關分片上查詢文檔的實際內容。返回結果的最大數量是 size 參數的值。這個類型是默認的查詢類型。
- query_and_fetch :查詢在所有分片上并行執行,所有分片返回等于 size 值的結果數。返回文檔的最大數等于 size 乘以 分片的數量。
- dfs_query_and_fetch :與 query_and_fetch 類似,在初始查詢中執行分布式詞頻的計算,以得到返回文件的更精確的得分,從而讓查詢結果更想相關。
- dfs_query_then_fetch :與 query_then_fetch 類似,在初始查詢中執行分布式詞頻的計算,以得到返回文件的更精確的得分,從而讓查詢結果更想相關。
- count :特殊搜索,只返回匹配查詢的文檔數。
- scan :在發送第一個請求后,響應一個滾動標識符,類似于數據庫當中的游標。
</ul>
基本查詢
詞條查詢
它僅匹配在給定字段中含有該詞條的文檔,而且是確切的、未經分析的詞條。請注意是未經過分析的。
http :9200/test/bulk/_search
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "2",
"_index": "test",
"_score": 1.0,
"_source": {
"title": "this is a new titld"
},
"_type": "bulk"
},
{
"_id": "1",
"_index": "test",
"_score": 1.0,
"_source": {
"title": "this is a new title"
},
"_type": "bulk"
},
{
"_id": "3",
"_index": "test",
"_score": 1.0,
"_source": {
"title": "this is a bad title"
},
"_type": "bulk"
}
],
"max_score": 1.0,
"total": 3
},
"timed_out": false,
"took": 2
}
http :9200/test/bulk/_search query:='{"term":{"title":"title"}}'
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "1",
"_index": "test",
"_score": 0.4375,
"_source": {
"title": "this is a new title"
},
"_type": "bulk"
},
{
"_id": "3",
"_index": "test",
"_score": 0.13424811,
"_source": {
"title": "this is a bad title"
},
"_type": "bulk"
}
],
"max_score": 0.4375,
"total": 2
},
"timed_out": false,
"took": 2
}
http :9200/test/bulk/_search query:='{"term":{"title":"this is"}}'
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [],
"max_score": null,
"total": 0
},
"timed_out": false,
"took": 1
}</pre>
多詞條查詢
http :9200/test/bulk/_search query:='{"terms":{"title":["this", "is"]}}'
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "1",
"_index": "test",
"_score": 0.61871845,
"_source": {
"title": "this is a new title"
},
"_type": "bulk"
},
{
"_id": "2",
"_index": "test",
"_score": 0.18985549,
"_source": {
"title": "this is a new titld"
},
"_type": "bulk"
},
{
"_id": "3",
"_index": "test",
"_score": 0.18985549,
"_source": {
"title": "this is a bad title"
},
"_type": "bulk"
}
],
"max_score": 0.61871845,
"total": 3
},
"timed_out": false,
"took": 7
}</pre>
match_all 查詢
如果想查詢索引中的所有文檔,只需要如下查詢:
{
"query" : {
"match_all" : {}
}
}
match 查詢
match 查詢把 query 參數中的值拿出來,加以分析,然后構建對應的查詢。使用 match 查詢時,ElasticSearch 將對一個字段選擇合適的分析器。
http :9200/test/bulk/_search query:='{"match":{"title":"this is"}}'
{
"_shards": {
"failed": 0,
"successful": 5,
"total": 5
},
"hits": {
"hits": [
{
"_id": "1",
"_index": "test",
"_score": 0.61871845,
"_source": {
"title": "this is a new title"
},
"_type": "bulk"
},
{
"_id": "2",
"_index": "test",
"_score": 0.18985549,
"_source": {
"title": "this is a new titld"
},
"_type": "bulk"
},
{
"_id": "3",
"_index": "test",
"_score": 0.18985549,
"_source": {
"title": "this is a bad title"
},
"_type": "bulk"
}
],
"max_score": 0.61871845,
"total": 3
},
"timed_out": false,
"took": 7
}</pre>
match 查詢的幾種類型:
布爾值匹配查詢
布爾值匹配查詢分析提供的文本,然后做出布爾查詢,有如下參數可以控制布爾值匹配行為:
- operator:可以接受 or 和 and。or 是匹配其中一個,and 是匹配所有。
- analyzer:這個參數定義了分析查詢文本用到的分析器。
- fuzziness:構建模糊查詢,可以傳入 0.0 ~ 1.0 之間的值來設置相似度。
- prefix_length:控制模糊查詢的行為。
- max_expansions:控制模糊查詢的行為。
</ul>
match_phrase查詢
類似布爾值查詢,不同的是,它從分析后的文本中構建短語查詢,而不是布爾子句。
match_phrase_prefix 查詢
基本上與 match_phrase 查詢一樣,它允許查詢文本的最后一個詞條只做前綴匹配。
query_string 查詢
query_string 提供了如下的參數:
- query:參數指定查詢文本
- default_field :參數指定默認的查詢字段,默認是 _all
- default_operator:默認值是 or
- 等其他參數
</ul>
標識符查詢
標識符查詢是一個簡單的查詢,僅用提供的標識符來過濾返回的文檔。
{
"query" : {
"ids" : {
"values" : ["10", "11", "12"]
}
}
}
前綴查詢
如果想查詢所有 title 字段以 cri 開頭的文檔,可以如下:
{
"query" : {
"prefix" : {
"title" : "cri"
}
}
}
fuzzy_like_this 查詢
查詢與提供的文本類似的文檔,它利用模糊字符串并選擇生成的最佳差分詞條:
{
"query":{
"fuzzy_like_this":{
"fields": ["title", "otitle"],
"like_text":"crime punishment"
}
}
}
fuzzy_like_this 支持如下參數:
- fields:此參數定義應該執行查詢的字段數組,默認是 _all 字段。
- like_text:必須參數,參數文本
- ignore_tf:計算相似度是否忽略詞頻,默認是 false,意味著使用詞頻。
- max_query_terms :此參數指定生成的查詢中能包括的最大查詢詞條數,默認是 25。
- min_similarity:指定差分詞條應該有的最小相似性,默認是 0.5。
- prefix_length:指定差分詞的公共前綴長度,默認是0。
- boost:加權值,默認是1。
- analyzer:分析器名稱
</ul>
fuzzy_like_this_field 查詢
fuzzy_like_this_field 查詢與 fuzzy_like_this 查詢類似,但它只能對應單個字段。
fuzzy 查詢
模糊匹配的最簡單形式,比較耗費 CPU 資源:
{
"query":{
"fuzzy":{
"title":"crke"
}
}
}
通配符查詢
在查詢當中允許使用 * 和 ? 等通配符:
{
"query":{
"wildcard":{
"title":"cr?e"
}
}
}
【參考資料】
- Elasticsearch服務器開發
</ol>
---EOF---
</article>
</code></code></code></code></code></code></code></code></code></code></code></code>