As we saw in part 1, we can use boolean queries to influence scoring and boost the relevance of your searches. 
But it soon becomes very verbose and some kind of queries can be quite complicated to write. 
Multi-match comes to help you write more concise search Queries. 

TL:DR

In this article we will focus on Multi_match query.

Elasticsearch Multi-match

Let’s begin with a classical use case. On a website, a user wants to search for a text in several fields : title, content, category, summary.

You can write your boolean query like this:

GET posts/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "elastic search"
          }
        },
        {
          "match": {
            "summary": "elastic search"
          }
        },
        {
          "match": {
            "content": "elastic search"
          }
        },
        {
          "match": {
            "category": "elastic search"
          }
        }
      ]
    }
  }
}

Or do the same with a multi-match like this:

GET posts/_search
{
  "query": {
    "multi_match": {
      "query": "elastic search",
      "type": "most_fields",
      "fields": ["title", "summary", "content", "category"]
    }
  }
}

It’s much more readable.

Pro tip: 
Multi_match comes with a “type” key.
It refers to the score calculation method. 
By default, elasticsearch uses “best_fields” which keeps only the better score from the better matched field. 
The “most_field” type combines the _score from each matched field.

And of course you can add independent boost value on each field:

GET posts/_search
{
  "query": {
    "multi_match": {
      "query": "elastic search",
       "type":       "most_fields",
      "fields": ["title^5", "summary", "content", "category^2"]
    }
  }
}

Good. 
But we can do more.

Multi-match for more precision 

Let’s communicate a pro tip.
In Elasticsearch, fields can be mapped several times.

It means that you can use different analyzers for the same text, and pick one or another, depending on your use case.
But you can also use the same field several times for one search. 

If a dataset have a title with 4 multifield: 

  • title 
  • Title.original
  • Title.shingle
  • Title.phonetic

You can use a multi match to search in all those fields at once and return the best score. 

In this use case, we will use the default type: “best_fields”.

GET posts/_search
{
  "query": {
    "multi_match": {
      "query": "elastic search",
      "type": "best_fields", 
      "fields": ["title", "title.original", "title.shingle", "title.phonetic"]
    }
  }
}

Multi match Boolean 

As seen in the article on Boolean queries, combining several queries can be a good solution to get better results. 

We can define a more complex query for a classic use case: 

GET posts/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {     1
            "query": "elastic search",
            "type": "best_fields",
            "fields": [
              "title",
              "title.original",
              "title.shingle",
              "title.phonetic"
            ]
          }
        }
      ],
      "should": [
        {
          "multi_match": {      2
            "query": "elastic search",
            "type": "phrase",
            "fields": [
              "title",
              "title.original",
              "title.shingle",
              "title.phonetic"
            ]
          }
        },
        {
          "multi_match": {       3
            "query": "elastic search",
            "type": "most_fields",
            "fields": [
              "content",
              "category^2",
              "summary"
            ]
          }
        
      ]
    }
  }
}

Let’s translate this query in a human language: 

  1. “Elastic” OR “search” MUST be found in one of the fields. The better it matches the higher it will be in the results. Either, no result will be returned.
  2. If the phrase “elastic search” is found, in this order in one of the fields add the score of the best field match to the general score. 
  3. If “Elastic” OR “search”  is found in content, category or summary, add matching _score for each field to the general score.

Cross Fields multi match

The “cross_fields” type for multi match is not really a scoring tips, but it’s an interesting feature. 

It gives the ability to use several fields as one, at query time.

Pro tip: for better performance, prefer using a copy_to at ingest time to gather several field in one and query on this field. 

The classical exemple is a search on a full name:

 GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "Will Smith",
      "type":       "cross_fields",
      "fields":     [ "first_name", "last_name" ],
      "operator":   "and"
    }
  }
}

Will just work like: 

GET user/_search
{
  "query": {
    "match": {
      "full_name": {
        "query": "Will Smith",
        "operator": "and"
      }
    }
  }
}

Spoon consulting is a certified partner of Elastic

As a certified partner of the Elastic company, Spoon Consulting offers a high level consulting for all kinds of companies.

Read more information on your personal use Elasticsearch use case on Spoon consulting’s posts

Or contact Spoon consulting now