Function_score, multi_match, script_score, and filter in Elasticsearch


(Hacker 21) #1

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)


(Ivan Brusic) #2

Place your multi_match query inside a must or should clause. Since you only
have one query, I would place it inside the must clause.

"bool": {
"filter": {
"term": {
"lang": "en"
}
},
"must": {
"multi_match": {
"query": "Sample Query here",
...
}
}
}


(Hacker 21) #3

Thanks Ivan!


(system) #4

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.