Catalog My learning Blog
Elasticsearch Tutorial: How Full-Text Search Works · Phase 2 · Lesson 8 · 3 min read

Elasticsearch Query DSL: Match vs Term vs Bool

On this page
  1. Full-text vs exact-value questions
  2. The match query for full text
  3. The term query for exact values
  4. Why a term query on a text field finds nothing
  5. The bool query: must, should, filter, must_not
  6. Check yourself

Full-text vs exact-value questions

  • "Find documents about database performance" is a full-text question. The query text should be analyzed like the field (Day 6). That's match.
  • "Find articles by author ana" is an exact question about a structured value. That's term.

Mixing them up is the most common beginner bug.

The match query for full text

HTTP
GET /articles/_search
{ "query": { "match": { "title": "Database Performance Myths" } } }
Code
total 3
  3    4.759   Database Performance Myths
  7    0.942   Network Performance Basics
  1    0.850   Tuning Databases for Performance

The query is analyzed into database, performance, myths; any document with any of them matches, ranked by how well it matches. Options:

  • "operator": "and" requires every term.
  • "minimum_should_match": "75%" requires most.
  • match_phrase requires the terms in order and adjacent.

The term query for exact values

HTTP
GET /articles/_search
{ "query": { "term": { "author": "ana" } } }
Code
total 2
  1    1.163   Tuning Databases for Performance | ana
  3    1.163   Database Performance Myths | ana

term doesn't analyze its input. On a keyword field that's exactly what you want, and it's case-sensitive: "Ana" found nothing.

Why a term query on a text field finds nothing

HTTP
GET /articles/_search
{ "query": { "term": { "title": "Database Performance Myths" } } }
Code
total 0

The index holds the lowercase terms database, performance, myths, never the whole string Database Performance Myths, so an exact term lookup finds nothing. Use match on text fields, and term on keyword fields (or the .keyword subfield). Related queries: terms (any of several values), range (numbers and dates), exists, prefix.

match analyzes the query text and matches index terms; term looks up the exact value; bool combines clauses that must, should, must not, or filter.
match analyzes the query text and matches index terms; term looks up the exact value; bool combines clauses that must, should, must not, or filter.

The bool query: must, should, filter, must_not

ClauseMust match?Affects score?
mustYesYes
shouldOptional (unless there's no must or filter), raises scoreYes
filterYesNo (cacheable, Day 12)
must_notMust not matchNo

A product search: wireless items, in electronics, at most 50, in stock:

HTTP
GET /products/_search
{
  "query": {
    "bool": {
      "must":   [ { "match": { "description": "wireless" } } ],
      "filter": [
        { "term":  { "category": "electronics" } },
        { "range": { "price": { "lte": 50 } } },
        { "term":  { "in_stock": true } }
      ]
    }
  }
}
Code
total 2
  102    1.162   Wireless Keyboard | 49.99 | True
  101    1.068   Wireless Mouse | 24.99 | True

Only the text match affects ranking; the yes/no constraints sit in filter. Bool queries nest, so complex logic stays readable.

Check yourself

The text field was analyzed into separate lowercase terms, database, performance and myths, at index time. A term query isn't analyzed, so it looked for the exact full string, which never exists in the index. Use match for text, or term on a keyword subfield.

For yes or no conditions that shouldn't influence relevance, such as category, price range or stock status. Filters skip scoring and can be cached, so they're faster, and the ranking reflects only the text query.

Set "operator": "and" on the match query, or use minimum_should_match, such as "75%", to require most words. The default or operator returns documents matching any word.

It excludes matching documents and runs in filter context, so it doesn't affect the scores of the remaining documents and its results can be cached. It's the right place for exclusions like discontinued products.

When words must appear together and in order, like new york. Use slop to allow a few words in between.