liveBook · Manning

Match_phrase query

As explained in Elasticsearch in Action, Second Edition

A match_phrase query is an Elasticsearch query that finds documents containing a sequence of terms in the specified order.

Use a match_phrase query when search results must contain a group of words in a specified field and order. Unlike a match query, it treats the input as a phrase rather than independently matching its terms. section 10.6 section 2.3.6

Listing 2.12 Searching for books with an exact phrase
GET books/_search
{
  "query": {
    "match_phrase": {                                          #1
      "synopsis": "must-have book for every Java programmer"   #2
    }
  }
}

Search the synopsis field for the complete phrase “must-have book for every Java programmer”; the example returns Effective Java. section 2.3.6

Adjust the query when real user input differs from the stored phrase:

  • Set slop to a positive integer when words are missing or not in the original order. A slop of 1 permits one ignored word; the default is 0. section 10.6 section 2.3.7
  • Combine match_phrase with other leaf queries inside a bool must clause to require both phrase and field criteria. section 2.5.2
  • Add a highlight object at the same level as query when returned text should emphasize matching words in the selected field. section 2.3.6
Listing 10.11 A match_phrase query with slop that drops one word
GET books/_search
{
  "query": {
    "match_phrase": {
      "synopsis": {                              #1
       "query": "book every Java programmer",    #2
       "slop": 1                                 #3
      }
    }
  }
}

Search “book every Java programmer” while setting slop to 1, allowing the omitted word “for.” section 10.6

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines exact ordered matching, slop adjustments, highlighting, and bool composition without detailing internal execution.
Examples
Uses bookstore synopses, Effective Java, Java programmers, missing words, highlighting, and combined author criteria.
What it assumes
Assumes familiarity with fields, full-text searching, and basic match queries while introducing phrase matching from concrete searches.
What you can do after
Apply it via Elasticsearch’s _search API to require ordered phrases, tolerate specified gaps, combine criteria, and highlight matches.
How it connects
Connects match queries, bool queries, highlighting, full-text search, and match_phrase_prefix queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.