混合布尔和多重匹配/功能得分查询 [英] Mixing bool and multi match/function score query
问题描述
我目前正在执行一个包含多个匹配项和功能得分的查询。 JSON的重要部分如下所示:
I'm currently doing a query that's a mix of multi match and function score. The important bit of the JSON looks like this:
"function_score":{
"query":{
"query_string":{
"query":"some query",
"fields":["id","name","strippedDescription","colourSearch","sizeSearch"]
}
}
}
但是,我也想包括结果不一定与查询匹配,而是具有大于0的特定数值。我认为布尔查询可以做到这一点,但是我不知道如何将布尔查询与函数得分查询一起使用。
However, I also want to include results that don't necessarily match the query but have a particular numeric value that's greater than 0. I think a bool query would do this, but I don't know how to use a bool query with a function score query.
我知道多重匹配查询只是布尔查询的简写,我可以将多重匹配查询扩展到其布尔匹配部分,但是,我不这样做知道我将如何在其中进行功能评分。
I understand that a multi match query is just shorthand for a bool query, and I could expand out the multi match query into its bool counter-part, however, I then don't know how I would do function score within that.
有什么想法吗?顺便说一下,我的版本是1.1.0。
Any ideas? I'm on version 1.1.0 by the way.
推荐答案
知道了!我错过了可以在布尔查询中嵌套多字段查询的事实!我的最终解决方案如下:
Figured it out! I was missing the fact that you can nest multi field queries within bool queries! My final solution looks like this:
{
"query":{
"function_score":{
"query":{
"bool":{
"should": [
{
"range": {
"allBoost": {
"gt": 0
}
}
},{
"multi_match":{
"query":"some search query",
"fields":[
"id",
"name",
"description",
"category"
]
}
}
]
}
},
"functions":[
{
"filter":{
"range": {
"allBoost": {
"gt": 0
}
}
},
"script_score":{
"script":"doc['allBoost'].value"
}
},
{
"filter":{
"range": {
"allBoost": {
"lte": 0
}
}
},
"script_score":{
"script":"_score"
}
}
],
"boost_mode": "replace"
}
}
}
这篇关于混合布尔和多重匹配/功能得分查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!