match, term and match_phrase

Elasticsearch Course · lesson 8 of 19 · 5 min read

Three queries that look interchangeable and behave completely differently.

Open this lesson in the learning hub

Key points

  • match analyses your query text with the field analyzer, then looks up each resulting term.
  • term performs no analysis at all, so it only ever finds a term exactly as it was indexed.
  • That is why a term query on a text field with any capital letter usually returns zero hits.
  • match_phrase requires the terms adjacent and in order, using positions from the postings.
  • slop permits that many token moves, so a slop of 1 allows one word between the two terms.
  • multi_match spreads one match across fields, with per-field boosts such as title^3.

Example

# title is mapped as text, so "Spring Boot" indexed as [spring, boot]

GET /articles/_search
{ "query": { "match": { "title": "Spring Boot" } } }        # 1 hit

GET /articles/_search
{ "query": { "term": { "title": "Spring Boot" } } }         # 0 hits

GET /articles/_search
{ "query": { "term": { "title.keyword": "Spring Boot" } } } # 1 hit

Use match on text fields and term on keyword fields - swapping them fails silently.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Elasticsearch Course course, and every lesson in it is listed on the Elasticsearch Course contents page.