A search on the Internet for `Apple'' is likely to return results about the
company, the fruit, and various recipes. We could try to narrow it down to
just the company by excluding words like `pie
, tart
, crumble
, and tree
,
using a must_not
clause in a bool
query:
GET /_search
{
"query": {
"bool": {
"must": {
"match": {
"text": "apple"
}
},
"must_not": {
"match": {
"text": "pie tart fruit crumble tree"
}
}
}
}
}
But who is to say that we wouldn’t miss a very relevant document about Apple
the company by excluding tree
or crumble
? Sometimes, must_not
can be
too strict.
The {ref}/query-dsl-boosting-query.html[boosting
query] solves this problem.
It allows us to still include results that appear to be about the fruit or
the pastries, but to downgrade them—to rank them lower than they would
otherwise be:
GET /_search
{
"query": {
"boosting": {
"positive": {
"match": {
"text": "apple"
}
},
"negative": {
"match": {
"text": "pie tart fruit crumble tree"
}
},
"negative_boost": 0.5
}
}
}
It accepts a positive
query and a negative
query. Only documents that
match the positive
query will be included in the results list, but documents
that also match the negative
query will be downgraded by multiplying the
original _score
of the document with the negative_boost
.
For this to work, the negative_boost
must be less than 1.0
. In this
example, any documents that contain any of the negative terms will have their
_score
cut in half.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。