本文參考官方提供api提煉出來的html
https://www.elastic.co/guide/en/elasticsearch/reference/current/search.htmlapi
Compound Queries緩存
複合查詢包裝其餘複合或葉子查詢,以組合其結果和分數,更改其行爲,或從查詢切換到篩選器上下文。app
Constant Score Querydom
一個包裝另外一個查詢的查詢,只返回一個等於篩選器中每一個文檔的查詢提高的常量分數。elasticsearch
GET /_search { "query": { "constant_score" : { "filter" : { "term" : { "user" : "kimchy"} }, "boost" : 1.2 } } }
Bool Queryide
用於組合多個葉或化合物查詢子句,做爲默認查詢 must
,should
,must_not
,或filter
條款。在must
和should
條款有他們的分數相結合-更匹配的條款,更好的-而must_not
和filter
條款在過濾器上下文中執行。函數
發生 | 描述 |
---|---|
|
子句(查詢)必須出如今匹配的文檔中,並有助於得分。this |
|
子句(查詢)必須出如今匹配的文檔中。可是不一樣於 |
|
子句(查詢)應出如今匹配的文檔中。若是 |
|
子句(查詢)不得出如今匹配的文檔中。子句在過濾器上下文中執行,這意味着忽略評分並考慮使用子句進行高速緩存。因爲忽略了評分,所以 |
GET _search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "term": { "status": "active" } } } } }
等價
GET _search { "query": { "constant_score": { "filter": { "term": { "status": "active" } } } } }
Dis Max Query
一個接受多個查詢的查詢,並返回與任何查詢子句匹配的任何文檔。當bool
查詢組合來自全部匹配查詢的分數時,dis_max
查詢使用單個最佳匹配查詢子句的分數。
GET /_search { "query": { "dis_max" : { "tie_breaker" : 0.7, "boost" : 1.2, "queries" : [ { "term" : { "age" : 34 } }, { "term" : { "age" : 35 } } ] } } }
Function Score Query
function_score
容許你修改的由查詢檢索文檔的分數。
要使用function_score
,用戶必須定義查詢和一個或多個函數,這些函數計算查詢返回的每一個文檔的新分數。
GET /_search { "query": { "function_score": { "query": { "match_all": {} }, "boost": "5", "random_score": {}, "boost_mode":"multiply" } } }
GET /_search { "query": { "function_score": { "query": { "match_all": {} }, "boost": "5", "functions": [ { "filter": { "match": { "test": "bar" } }, "random_score": {}, "weight": 23 }, { "filter": { "match": { "test": "cat" } }, "weight": 42 } ], "max_boost": 42, "score_mode": "max", "boost_mode": "multiply", "min_score" : 42 } } }
每一個文檔都由定義的函數評分。該參數 score_mode
指定計算得分的組合方式:
|
分數乘以(默認) |
|
得分總和 |
|
分數是平均的 |
|
應用具備匹配過濾器的第一個函數 |
|
使用最高分 |
|
使用最低分數 |
該參數boost_mode
定義:
|
查詢得分和功能得分相乘(默認) |
|
僅使用功能得分,忽略查詢得分 |
|
查詢得分和功能得分被添加 |
|
平均 |
|
最大查詢分數和功能分數 |
|
最小查詢得分和功能得分 |
script_score
函數容許您使用腳本表達式包裝另外一個查詢並使用從doc中其餘數字字段值派生的計算來自定義它的評分。
GET /_search { "query": { "function_score": { "query": { "match": { "message": "elasticsearch" } }, "script_score" : { "script" : { "source": "Math.log(2 + doc['likes'].value)" } } } } }
field_value_factor
功能容許您使用文檔中的字段來影響分數。它與使用該script_score
函數相似,但它避免了腳本的開銷。若是在多值字段上使用,則僅在計算中使用該字段的第一個值。
GET /_search { "query": { "function_score": { "field_value_factor": { "field": "likes", "factor": 1.2, "modifier": "sqrt", "missing": 1 } } } }
這將轉化爲如下評分公式:
sqrt(1.2 * doc['likes'].value)
該功能有多種選擇field_value_factor
:
|
要從文檔中提取的字段。 |
|
將字段值乘以的可選因子,默認爲 |
|
修改適用於該字段的值,能夠是一個: |
修改 | 含義 |
---|---|
|
不要對字段值應用任何乘數 |
|
取字段值的經常使用對數 |
|
將1添加到字段值並採用經常使用對數 |
|
將2添加到字段值並採用經常使用對數 |
|
取字段值的天然對數 |
|
將1添加到字段值並採用天然對數 |
|
將2添加到字段值並採用天然對數 |
|
平方字段值(乘以它本身) |
|
取字段值的平方根 |
|
報答字段值,同 |
missing
若是文檔沒有該字段,則使用的值。修飾符和因子仍然應用於它,就像從文檔中讀取同樣。
Boosting Query
返回與positive
查詢匹配的文檔,但減小與negative
查詢匹配的文檔的分數。
GET /_search { "query": { "boosting" : { "positive" : { "term" : { "field1" : "value1" } }, "negative" : { "term" : { "field2" : "value2" } }, "negative_boost" : 0.2 } } }
Joining Queries
Nested Query
嵌套查詢容許查詢嵌套對象/文檔
GET /_search { "query": { "nested" : { "path" : "obj1", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "match" : {"obj1.name" : "blue"} }, { "range" : {"obj1.count" : {"gt" : 5}} } ] } } } } }
Has Child Query
has_child
過濾器接受查詢和子類型針對其運行,並致使具備子文檔與查詢匹配的父文件。
GET /_search { "query": { "has_child" : { "type" : "blog_tag", "query" : { "term" : { "tag" : "something" } } } } }
has_child
也有得分支持。支持的得分模式min
,max
,sum
,avg
或none
。默認值爲 none
and,產生與先前版本相同的行爲。若是將得分模式設置爲除了以外的其餘值none
,則將全部匹配的子文檔的得分聚合到關聯的父文檔中。
GET /_search { "query": { "has_child" : { "type" : "blog_tag", "score_mode" : "min", "query" : { "term" : { "tag" : "something" } } } } }
Has Parent Query
該has_parent
查詢接受查詢和父類型。查詢在父文檔空間中執行,該父文檔空間由父類型指定。此查詢返回關聯父項已匹配的子文檔。對於其他has_parent
查詢具備相同的選項,其工做方式與has_child
查詢相同。
GET /_search { "query": { "has_parent" : { "parent_type" : "blog", "score" : true, "query" : { "term" : { "tag" : "something" } } } } }
Parent Id Query
parent_id
查詢可用於查找屬於特定父級的子文檔。
GET /my_index/_search { "query": { "parent_id": { "type": "my_child", "id": "1" } } }
此查詢有兩個必需參數:
|
該子類型名稱,在指定的 |
|
父文檔的ID。 |
|
設置爲 |