AI Hints
Home›Elasticsearch Tutorial›Phrases and Many Fields
Elasticsearch · Lesson 26 of 120

match_phrase

Words in order.

⏱ 2 min read▶ Video at 23:57🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Sometimes the order of words matters. Heavy rain isn't the same as rain that's heavy. Match phrase finds the words next to each other, in the same order.

  • Order matters
  • Words together
  • match_phrase

An exact phrase

Match phrase looks for heavy, followed straight away by rain. Run it. Only descriptions with the phrase heavy rain match.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source.description
{
  "query": { "match_phrase": { "description": "heavy rain" } }
}
Output
{
  "hits": {
    "hits": [
      {
        "_source": {
          "description": "A durable tent that sets up in minutes and keeps you dry in
          heavy rain."
        }
      }
    ]
  }
}
Also uses _load
products

A little slop

Slop lets the words be a few positions apart. Here we search for keeps dry, with a slop of two. Run it. It finds keeps you dry, because you sits between the words.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source.description
{
  "query": {
    "match_phrase": {
      "description": { "query": "keeps dry", "slop": 2 }
    }
  },
  "size": 2
}
Output
{
  "hits": {
    "hits": [
      {
        "_source": {
          "description": "A durable tent that sets up in minutes and keeps you dry in
          heavy rain."
        }
      }
    ]
  }
}
Also uses _load
products

Summary

Match phrase needs the words together and in order. Slop allows a few words in between. Next, searching several fields at once.

Key takeaways
  • match_phrase
  • In order
  • slop
Next up: multi_match — one query, many fields