Skip to the workspace
Pebellslearn by building
Documents and Leaf Queries  ›  Lesson

Match vs Term — Analysed or Exact

Match Query 12 minA match query analyses its input with the field's analyzer and suits text fields, while a term query looks up the exact value and suits keyword, number, date and boolean fields.

elasticsearch / Match vs Term — Analysed or Exact

Explore each scene at your own pace.

01 · The rule

The rule

Match vs Term — Analysed or Exact

A developer writes {"term": {"title": "Quiet Garden"}} and gets zero hits, even though "The Quiet Garden" is right there. Swap term for match and it appears. Understanding why is the single most useful thing to know about Elasticsearch queries.

The rule

Match analyses your words like the field did; term looks up your value untouched.

Recall that the title text field was indexed as lower-cased words: the, quiet, garden.

match runs your query text through the same analyzer the field used, then looks up each resulting word:

GET /books/_search
{ "query": { "match": { "title": "Quiet Garden" } } }

"Quiet Garden" becomes quiet, garden. Both exist, so the book matches. By default the words are joined with OR — a book with only "garden" in its title also matches, just with a lower score. To require every word:

{ "query": { "match": { "title": { "query": "quiet garden", "operator": "and" } } } }

term skips analysis. It looks for the exact value Quiet Garden — capital letters, space and all — as a single term. The text field holds no such term, so nothing matches.

term is built for keyword, number, date and boolean fields, where values are stored whole:

GET /books/_search
{ "query": { "term": { "genre": "fiction" } } }

genre is a keyword field holding exactly fiction, so this is a precise, fast lookup. To match any of several exact values, use terms: {"terms": {"genre": ["fiction", "hobby"]}}.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.