Medium search elasticsearch

How do you make prefix and autocomplete queries fast in Elasticsearch?

The 30 second answer

A plain prefix query has to expand the prefix into every matching term at search time, and short prefixes like "s" match a huge number of terms. I'd build prefixes at index time instead: index prefixes on a text field, the search-as-you-type field type, or edge n-grams. For fixed-order titles, the completion suggester is faster still. Then I'd require a minimum length and cap the results.

Could you say this in an interview?

Why prefix search gets slow. A prefix query is cheaper than a leading wildcard, because the sorted term list lets Elasticsearch jump to the start. But it still expands the prefix into every matching term before searching. For the prefix "s" in a catalog, that's an enormous number of terms. Autocomplete sends a new query on every keystroke, so this cost repeats many times per user.

Build the prefixes at index time.

  • **index_prefixes on a text field** stores term prefixes as extra terms. By default the lengths run from 2 (min_chars) to 5 (max_chars). The search-speed guide says query parsers use it automatically, so a prefix query becomes a normal term lookup.
  • **search_as_you_type field type** is a text-like field built for as-you-type completion. It creates sub-fields with word groups and prefixes, and you query it with a multi_match of type bool_prefix.
  • Edge n-gram analyzer stores the beginnings of each word ("s", "sn", "sne"...). The docs note that for text with a well-known order, such as movie or song titles, the completion suggester is much more efficient than edge n-grams. Edge n-grams win when the typed word can appear anywhere in the text.
  • Use a different search analyzer. With edge n-grams, index with the n-gram analyzer but search with a plain one. Otherwise the query text is cut into n-grams too and matches far too much.

Guardrails in the API layer. Require a minimum number of characters, debounce keystrokes in the client, cap the number of suggestions, and cache popular prefixes. These protect the cluster no matter which index trick you use.

PUT products
{ "mappings": { "properties": {
    "name":    { "type": "text", "index_prefixes": { "min_chars": 2, "max_chars": 5 } },
    "suggest": { "type": "search_as_you_type" }
}}}

GET products/_search
{ "query": { "multi_match": { "query": "snea",
    "type": "bool_prefix",
    "fields": ["suggest", "suggest._2gram", "suggest._3gram"] } } }

Alternate interview scenarios

"Autocomplete is fast in tests but slow in production."

Test data is small. In production, one- and two-letter prefixes expand into many terms. Look at query latency by prefix length and enforce a minimum length.

"Suggestions should match 'sneakers' when the user types 'neak'."

That's matching inside a word, not a prefix. It needs n-grams, at a real index-size cost, or a decision that prefix matching is good enough.

Sources (4)
  1. Elasticsearch docs: index_prefixeselastic.co
  2. Elasticsearch docs: Search-as-you-type field typeelastic.co
  3. Elasticsearch docs: Edge n-gram tokenizer (completion suggester note)elastic.co
  4. Elasticsearch docs: Tune for search speed (faster prefix queries)elastic.co