這是我參與8月更文挑戰的第6天,活動詳情查看:8月更文挑戰java
本Elasticsearch相關文章的版本爲:7.4.2python
Elasticsearch的bool查詢是使用一個或多個bool查詢子句構建的,每一個子句都是如下特定bool類型:c++
bool類型 | 描述 | 是否參與評分 |
---|---|---|
must | must裏面的全部子句都知足的文檔才返回 | 是 |
should | 應該知足should裏面的0個或多個子句的文檔才返回 | 是 |
must_not | must_not裏面的子句不能出如今文檔裏 | 否 |
filter | 必須知足filter子句的文檔才返回 | 否 |
bool查詢遵循more-matches-is-better的原則。意味着知足must和should子句查詢的分數將會彙總到一塊兒做爲最後的_scorre評分。filter和must_not的子句不歸入到評分範圍,僅進行過濾文檔 下面的查詢語句就是查找獲取知足如下條件的候選人:golang
algorithm
的;it
行業經驗的;POST /candidate/_search
{
"query": {
"bool" : {
"must" : {
"term" : { "tag" : "algorithm" }
},
"filter": {
"term" : { "tag" : "it" }
},
"must_not" : {
"range" : {
"age" : { "gte" : 36, "lte" : 18 }
}
},
"should" : [
{ "term" : { "tag" : "python" } },
{ "term" : { "tag" : "golang" } },
{ "term" : { "tag" : "c++" } },
{ "term" : { "tag" : "java" } }
],
"minimum_should_match" : 1,
"boost" : 1.0
}
}
}
複製代碼
boosting查詢返回知足positive子句的文檔,而且同時下降知足negative子句的文檔的相關性得分。這樣,咱們能夠使用boosting
查詢將某些文檔相關性下降而不將它們從搜索結果中排除。
下面的查詢語句將會獲得如下文檔:編程
GET /bank/_search
{
"query": {
"boosting" : {
"positive" : {
"term" : {
"hobby" : "apple"
}
},
"negative" : {
"term" : {
"hobby" : "chips"
}
},
"negative_boost" : 0.5
}
}
}
複製代碼
boosting查詢適合挑選最重要的但容許存在某些不過重要的條件的文檔。markdown