The prefix, wildcard and regexp queries match terms by pattern instead of by exact value. prefix finds terms starting with a string, wildcard supports * and ? placeholders, and regexp accepts a regular expression written in Lucene syntax. All three work on individual terms in the index and are not analyzed.
These term-level pattern queries are flexible, but they are also the easiest way to build a slow search. This guide explains each one, when to use it and how to keep it fast. The primary example is the elasticsearch wildcard query.
Pattern queries are normally run on keyword fields, where each value is a single term.
On a text field the patterns would be applied to analyzed, lowercase tokens, which usually surprises people. Matching is case sensitive on keyword fields unless you pass case_insensitive.
prefix returns documents whose term starts with the given string.
This matches alice_dev and alina_ops. Prefix queries are the cheapest of the three because terms are stored in sorted order, so Elasticsearch can jump to the first term that starts with ali. For frequent prefix searches, the mapping option index_prefixes on a text field makes them faster.
wildcard supports two operators. The * matches zero or more characters and ? matches exactly one.
This returns Alice and Bob. A pattern such as al?ce* matches alice_dev only, because ? stands for a single character.
A pattern that begins with * or ?, such as *@example.com, cannot use the sorted term order and must check many terms. On large indices this is expensive. Prefer patterns that start with fixed characters, and consider the wildcard field type, which is built for this: it indexes values so that arbitrary wildcard and regexp searches are faster on large strings such as log lines.
regexp uses Lucene regular expression syntax, which differs from Perl or JavaScript syntax. The expression must match the whole term, so there is no need for anchors.
The pattern matches alice_dev and alina_ops. Lucene regexps do not support lookaheads, \d shortcuts or word boundaries. Use character classes like [0-9] instead. Limit complexity with max_determinized_states (the default is 10000) so a bad pattern cannot consume too many resources.
| Query | Syntax | Speed | Typical use |
|---|---|---|---|
prefix | plain start string | Fastest | Starts-with searches, simple autocomplete |
wildcard | * and ? | Medium; slow with a leading * | SKU or code patterns, email domains |
regexp | Lucene regular expression | Slowest | Complex pattern validation |
keyword fields, not on large analyzed text fields.bool filter so they are not scored.search.allow_expensive_queries can be set to false to reject wildcard, regexp, prefix and fuzzy queries on shared clusters.rewrite parameter, for example constant_score, to control how the matched terms are scored and combined. The default is already a constant score for these queries.search_as_you_type, completion suggesters or edge n-grams.On keyword fields, yes by default. Set "case_insensitive": true on the query, or use a lowercase normalizer in the mapping.
It probably starts with *, which forces a scan of many terms. Anchor the pattern with fixed characters, or use the wildcard field type.
You can, but they run against analyzed tokens such as quick and brown, not the original string, so results are often confusing. Use a keyword sub-field.
Neither is ideal. Use a prefix query for a small field, or search_as_you_type and completion for proper autocomplete.
Use prefix for starts-with matches, wildcard for simple patterns with * and ?, and regexp for full pattern logic in Lucene syntax. They are not analyzed, work best on keyword fields and become expensive with leading wildcards, so keep patterns anchored and filter first.