Medium search elasticsearch

When do you use match, match_phrase or term, and what does each cost?

The 30 second answer

match analyzes the shopper's text and finds documents with any of the words by default, or all of them with operator and. match_phrase also needs the words in order and next to each other, which reads term positions and costs more. term does no analysis, so it belongs on keyword, numeric and boolean fields for exact filters. I use match for search, match_phrase as a boost, term for filters.

Could you say this in an interview?

From the basics. A text field is split into words (tokens) when the document is indexed. "Slim Fit Jeans" becomes slim, fit, jeans. The three query types treat the shopper's input differently:

  • **match** runs the input through the same analyzer, then looks up each token. The docs: the default operator is OR, so "slim fit jeans" means slim OR fit OR jeans. With "operator": "and" all tokens must be present, in any order and anywhere in the field.
  • **match_phrase** also requires the tokens in the same order, next to each other. slop (default 0) allows that many positions of movement. To check order, Lucene has to read token positions as well as the list of matching documents, so a phrase query does more work per candidate than a plain match.
  • **term** does no analysis at all. It looks for the exact value in the index. The docs say to avoid term on text fields (the Zalando company page has a full question on why). It's the right tool for keyword, numeric, date and boolean fields.

Badly written. A phrase query as the only clause makes results depend on exact word order, so "jeans slim fit" finds nothing, and it reads positions for every candidate:

{ "query": { "match_phrase": { "name": "slim fit jeans" } } }

Better. Use match for recall and add the phrase as an optional boost, so exact phrases rank higher but don't decide what matches:

GET products/_search
{ "query": { "bool": {
    "must":   [ { "match": { "name": { "query": "slim fit jeans", "operator": "and" } } } ],
    "should": [ { "match_phrase": { "name": { "query": "slim fit jeans", "slop": 1, "boost": 2 } } } ],
    "filter": [ { "term": { "market": "de" } } ]
}}}

If the phrase boost is still too expensive at high traffic, move it into a rescore block so it only runs on the top results of each shard (see the scoring question below).

Trade-offs.

  • operator: or gives high recall and noisy tails. operator: and gives precision and more zero-result searches. minimum_should_match sits between them.
  • Phrase matching needs positions in the index. That's the default for text fields; a field mapped with index_options: docs or freqs to save space can't serve phrase queries.
  • term on the wrong field type is the most common correctness bug, not a performance bug.

Alternate interview scenarios

"Search for 'new balance' returns lots of 'new' arrivals from other brands. How do you fix it with the query?"

Add a match_phrase boost on brand and name so the two words together rank first. Better still, have query understanding detect the brand and turn it into a filter or a strong boost on brand.raw.

"Is match_phrase_prefix a good autocomplete?"

Only for small use cases. The last word is expanded like a prefix query, limited by max_expansions, so results depend on which terms are expanded first. A real autocomplete builds prefix structures at index time, which the slow-query page covers.

Sources (3)
  1. Elasticsearch docs: Match query (operator and minimum_should_match)elastic.co
  2. Elasticsearch docs: Match phrase query (slop)elastic.co
  3. Elasticsearch docs: Term queryelastic.co