Vincent Decaux
Vincent Decaux

Reputation: 10714

ElasticSearch use match_phrase and multi_match together

I use ES 7.1, I want to query using multi match to grab result from several fields, but I want to grab results with match_phrase on a specific field.

I tried a lot of things, this is the version the closest on what I want :

'bool' : {
    'must' : {
        'multi_match' : {
            'fields' => ['titre', 'subtitre', 'description'],
            'query' => $query
        },
        'match_phrase' : {
            'titre' => $query
        }
    },
    'filters' : { // ... some filters }
}

I get an error :

parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

Is there a way to combine both of criterias ? I don't want to create 2 queries.

Upvotes: 0

Views: 285

Answers (1)

clayjones
clayjones

Reputation: 13

When you want more than one must subquery, you just need to pass the subqueries in as an array. This modification should work (translated to JSON):

{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "hello",
            "fields": ["name", "brand_name"]
          }
        },
        {
          "match_phrase": {
            "title": "hello"
          }
        }
      ]
    }
  }
}

Upvotes: 0

Related Questions