使用 function_score 时如何向弹性查询添加过滤器?
how to add filters to elastic query when using function_score?
这是我当前的弹性查询:
{
"from": 0,
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "ocean",
"fields": [],
"fuzziness": "AUTO"
}}],
"must_not": [{
"exists": {
"field": "parentId"
}
}]
}
},
"functions" : [
{
"gauss": {
"createTime": {
"origin": "2020-07-09T23:50:00",
"scale": "365d",
"decay": 0.3
}
}
}
]
}
}
}
如何正确添加 过滤器?我想也许是因为我使用的是 function_score
才有所不同?我想添加一个硬过滤器,例如,只显示带有 uploadUser: 'Mr. Bean'
的结果......但仍然为通过此过滤器的结果保留评分。
我尝试在不同的地方使用 filter
,也使用 must
,但我要么没有得到任何结果,要么得到所有结果。
我正在使用 Elastic Search 7。感谢您的帮助
您可以尝试以下搜索查询:
参考这位ES官方documentation了解更多关于Function score query
{
"from": 0,
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"filter": {
"term": {
"uploadUser": "Mr. Bean"
}
},
"must": [
{
"multi_match": {
"query": "ocean",
"fields": [
],
"fuzziness": "AUTO"
}
}
],
"must_not": [
{
"exists": {
"field": "parentId"
}
}
]
}
},
"functions": [
{
"gauss": {
"createTime": {
"origin": "2020-07-09T23:50:00",
"scale": "365d",
"decay": 0.3
}
}
}
]
}
}
}
这是我当前的弹性查询:
{
"from": 0,
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"must": [{
"multi_match": {
"query": "ocean",
"fields": [],
"fuzziness": "AUTO"
}}],
"must_not": [{
"exists": {
"field": "parentId"
}
}]
}
},
"functions" : [
{
"gauss": {
"createTime": {
"origin": "2020-07-09T23:50:00",
"scale": "365d",
"decay": 0.3
}
}
}
]
}
}
}
如何正确添加 过滤器?我想也许是因为我使用的是 function_score
才有所不同?我想添加一个硬过滤器,例如,只显示带有 uploadUser: 'Mr. Bean'
的结果......但仍然为通过此过滤器的结果保留评分。
我尝试在不同的地方使用 filter
,也使用 must
,但我要么没有得到任何结果,要么得到所有结果。
我正在使用 Elastic Search 7。感谢您的帮助
您可以尝试以下搜索查询:
参考这位ES官方documentation了解更多关于Function score query
{
"from": 0,
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"filter": {
"term": {
"uploadUser": "Mr. Bean"
}
},
"must": [
{
"multi_match": {
"query": "ocean",
"fields": [
],
"fuzziness": "AUTO"
}
}
],
"must_not": [
{
"exists": {
"field": "parentId"
}
}
]
}
},
"functions": [
{
"gauss": {
"createTime": {
"origin": "2020-07-09T23:50:00",
"scale": "365d",
"decay": 0.3
}
}
}
]
}
}
}