在我的ES索引中,文档有两个字段score_min
和score_max
,我试图在bool查询中提升。
我想提升score_min <= expected_score <= score_max
为真的所有文件。
我知道我可以在must子句中放入两个range
个查询,但这意味着其他文档会被忽略。
有没有办法做这样的事情
..
..
"should": [
...
...
"some_query": {
"and": [
"range": {
"score_min": {
"lte": expected_score
},
},
"range": {
"score_max": {
"gte": expected_score
}
}
"boost": 2
]
}
]
答案 0 :(得分:2)
您可以使用function_score
查询执行此操作。另外一个好处是,您的range
查询可以编写为过滤器,因此可以利用过滤器缓存:
curl -XGET "http://localhost:9200/_search" -d'
{
"query": {
"function_score": {
"query": {
"match": { "some_field": "foo bar" }
},
"functions": [
{
"boost_factor": 1.2
"filter": {
"bool": {
"must": [
{ "range": { "score_min": { "lte": 10 }}},
{ "range": { "score_max": { "gte": 10 }}}
]
}
}
}
]
}
}
}'
返回与查询匹配的所有结果,但任何与过滤器匹配的结果都会将其分数乘以boost_factor
。