如何在Elasticsearch 5中为function_score查询添加过滤器

时间:2017-02-16 16:41:54

标签: elasticsearch

如何在弹性搜索5.2.0中将此过滤器添加到此查询中。

我试图将过滤器包装在一个没有运气的过滤数组中。

过滤

"filter" : {
     "term" : { "body.category" : 1}
}

查询

{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "operator": "and",
          "query": "my search",
          "fuzziness": 1,
          "fields": [
            "body.name^5",
            "body.manufacturer^33",
            "body.sections.name",
            "body.sections.parent.name",
            "body.sections.parent.parent.name"
          ]
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "body.rank"
          }
        }
      ],
      "score_mode": "multiply"
    }
  }
}

1 个答案:

答案 0 :(得分:5)

我通过bool包装查询并将过滤器放在那里来解决它。

   {
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "filter": {
                "term": {
                  "body.category": 1
                }
              },
              "should": {
                "multi_match": {
                  "operator": "and",
                  "query": "my search",
                  "fuzziness": 1,
                  "fields": [
                    "body.name",
                    "body.manufacturer",
                    "body.sections.name",
                    "body.sections.parent.name",
                    "body.sections.parent.parent.name"
                  ]
                }
              }
            }
          },
          "functions": [
            {
              "field_value_factor": {
                "field": "body.rank"
              }
            }
          ],
          "score_mode": "multiply"
        }
      }
    }