0
votes

I'm having trouble adding a filter to my existing multimatch query which is embedded inside of a function_score.

Ideally, I'd like to filter by "term" : { "lang" : "en" }, only get back documents which are in the english language.

I've tried moving around the order, tried wrapping my query in bool, but just can't get the filter to work with the other functions I'm using.

My query code:

GET /my_index/_search/
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": {
            "term": {
              "lang": "en"
            }
          },
          "multi_match": {
            "query": "Sample Query here",
            "type": "most_fields",
            "fields": [
              "body",
              "title",
              "permalink",
              "name"
            ]
          }
        }
      },
      "script_score": {
        "script": {
          "source": "_score + 10"
        }
      }
    }
  }
}

Error code:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[bool] query does not support [multi_match]",
        "line": 11,
        "col": 19
      }
    ],
    "type": "parsing_exception",
    "reason": "[bool] query does not support [multi_match]",
    "line": 11,
    "col": 19
  },
  "status": 400
}

I'm using the latest version of Elasticsearch (I believe 6.2)

2

2 Answers

1
votes

Try wrapping your multi_match in a must clause like so

"must": {
    "multi_match": ...
}

The error message is clear, bool query accepts only filter, must, should

0
votes

Final Solution:

GET /my_index/_search/
{
  "query": {
    "function_score": {
      "query": {
        "bool" : {
          "filter": {
            "term": {
              "lang": "en"
            }
          },
          "must" : {
            "multi_match" : {
              "query": "Sample Query Here",
              "type": "most_fields",
              "fields": [ "body", "title", "permalink", "name"]
            }
          }
        }
      },
    "script_score" : {
      "script" : {
        "source": "_score + 10"
      }
    }
    }
  }
}