首页 > 解决方案 > 使用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。感谢您的帮助

标签: elasticsearchsearchelastic-stack

解决方案


您可以尝试以下搜索查询:

参考这个 ES 官方文档了解更多关于函数分数查询的信息

    {
  "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
            }
          }
        }
      ]
    }
  }
}

推荐阅读