prometheus
prometheus

Reputation: 932

How do I add a filter to function_score query in Elasticsearch 5

How do I add this filter to this query below in elastic search 5.2.0.

I tried to wrap the filter in a filtered array with no luck.

Filter

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

The query

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

Upvotes: 4

Views: 2913

Answers (1)

prometheus
prometheus

Reputation: 932

I solved it by wrapping the query by a bool and place the filter there.

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

Upvotes: 7

Related Questions