System design Level 2 This level · 48 of 48
Medium search elasticsearch

Why is a wildcard search that starts with an asterisk slow, and how do you fix it?

The 30 second answer

The term list is sorted, so a pattern like phone-star can jump straight to the terms starting with phone. Star-phone has no fixed start, so Elasticsearch must check almost every term in the field, and the docs warn against leading wildcards. I'd fix it at index time: n-grams for matching inside words, a reversed copy for suffix search, or the wildcard field type for log-style values.

Could you say this in an interview?

Why the position of the star matters. Picture the term list of a field as a dictionary sorted alphabetically. For phone*, Elasticsearch jumps to "phone" and reads forward until the terms stop starting with "phone". For *phone, the match could start with any letter, so it has to visit almost every term in the dictionary and test each one. The Elasticsearch docs say this directly: avoid starting a pattern with * or ?, because it increases the number of terms to check and slows the search. This is the same reason LIKE '%phone' can't use a B-tree index in SQL.

It can also be switched off. Wildcard, regexp and prefix queries count as expensive queries. A cluster can block them entirely with search.allow_expensive_queries: false, and some platform teams do that to protect shared clusters.

Fixes, chosen by what the user actually needs.

  • Find a word inside a longer word ("contains"). Index the field with an n-gram analyzer, so a word is stored as all its short pieces ("pho", "hon", "one" and so on). The search becomes a normal term lookup. The cost is a much larger index, so use it on short fields such as names or SKUs, not on descriptions.
  • Find values by their ending, such as file extensions or the last digits of an order number. Index a reversed copy with the reverse token filter. A suffix search then becomes a prefix search on the reversed field, which is fast.
  • Grep-style search over long, machine-generated values such as logs or URLs. Use the wildcard field type. It's built for wildcard and regexp queries on those kinds of values.
  • Often the best fix is in the product. Does a shopper really need "contains" search on a product name? Normal analyzed full-text search, which splits text into words, plus autocomplete covers most real needs.
PUT products
{
  "settings": { "analysis": {
    "analyzer":  { "trigram": { "tokenizer": "trigram_tok", "filter": ["lowercase"] } },
    "tokenizer": { "trigram_tok": { "type": "ngram", "min_gram": 3, "max_gram": 3 } }
  }},
  "mappings": { "properties": {
    "sku": { "type": "keyword",
             "fields": { "grams": { "type": "text", "analyzer": "trigram" } } }
  }}
}

Alternate interview scenarios

"Support agents search orders by the last four digits of an order number."

Add a reversed sub-field, or store the last four digits as their own keyword field at index time. That turns the search into an exact lookup.

Sources (4)
  1. Elasticsearch docs: Wildcard query (leading wildcard warning and expensive queries)elastic.co
  2. Elasticsearch docs: N-gram tokenizerelastic.co
  3. Elasticsearch docs: Reverse token filterelastic.co
  4. Elasticsearch docs: Keyword type family, including the wildcard field typeelastic.co
Previous
Next