Why an Elasticsearch query is slow even with an index 7 questions in 1 chapter

  1. 01Query shapes that defeat the index7 questions

Query shapes that defeat the index

  • 1.1 A search query is slow even though every field is indexed. What do you check first? Medium 3 min

    First I'd find out where the time goes, using the slow log and the Profile API, which breaks a query down into its parts. Then I'd look for query shapes that can't use the index well: a leading wildcard, a script, a term lookup on a numeric ID, a range with now that can't be cached, or a search across dozens of fields. Most fixes move work from search time to index time.

    Read the full answer
  • 1.2 Why is a wildcard search that starts with an asterisk slow, and how do you fix it? Medium 3 min

    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.

    Read the full answer
  • 1.3 Why do scripts make Elasticsearch queries slow, and what do you do instead? Medium 3 min

    A script is a small piece of code that Elasticsearch runs for every document it touches, so it can't use the index, a bit like wrapping a SQL column in a function. The docs say to avoid scripts in sorting, aggregations and script score where possible. Instead, I'd compute the value once at index time, in the application or an ingest pipeline, store it as a normal field, and sort or filter on that.

    Read the full answer
  • 1.4 Should a product ID be mapped as a number or a keyword in Elasticsearch? Easy 3 min

    Usually keyword. A product ID looks like a number, but you never ask for IDs between two values. You look up exact IDs. Elasticsearch optimizes numeric fields for range queries, while keyword fields are better for exact term lookups, and the docs say to map identifiers like product IDs as keyword. Keep a numeric type only for real quantities such as price or stock, or add both through multi-fields.

    Read the full answer
  • 1.5 Why do date range queries with now miss the cache, and how does rounding help? Medium 3 min

    The word now means the current millisecond, so a range like the last hour is a slightly different query every time and its result can't be reused. Elasticsearch's docs say queries with now are typically not cacheable. If I round, for example from now minus one hour rounded to the minute up to now rounded to the minute, every request in that minute is identical and the filter cache can be reused.

    Read the full answer
  • 1.6 How do you make prefix and autocomplete queries fast in Elasticsearch? Medium 3 min

    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.

    Read the full answer
  • 1.7 Why does searching across many fields slow Elasticsearch down, and how does copy_to help? Medium 2 min

    A multi-match query runs the search once per field and then combines the scores, so twenty fields means roughly twenty searches. The docs say the more fields a query targets, the slower it is. With copy_to, I copy the values of those fields into one combined field at index time and search just that field. I keep a few important fields separate, like the title, so I can still boost them.

    Read the full answer