Wildcard query #
Returns documents that contain terms matching a wildcard pattern. The
pattern supports * (any sequence of characters, including none) and
? (any single character).
Inside a
nestedquery: not supported yet — the clause is rejected with an HTTP 400 naming the offending clause.
How it works #
The pattern is expanded against the inverted index’s term dictionary:
the engine walks the field’s terms, keeps those matching the pattern, and
unions their postings lists — the same result as a should of
term queries over the surviving terms. Matching is anchored
at both ends: web-1 is the whole term, so web-* matches web-10 and
web-1 itself, but *web-1* is required for anything with text on both
sides.
- Cost follows the anchors. A pattern starting with a literal
(
web-*) can skip most of the dictionary; a pattern starting with*or?(a leading wildcard) cannot, and degenerates to a full dictionary walk — the same cost class assuffix. Preferprefixwhen the pattern is a pure head. - Matching is per term. On a
keywordfield the term is the whole value; on atextfield each analyzed token is tested separately. No analysis runs on the pattern itself — onstandard-analyzed fields the stored tokens are lowercase. - Scoring. The expansion is rewritten with ES’s
constant_scorerule: every matching document scores exactly theboost(1.0by default), regardless of how many terms matched. Inside abool.filterthe scores are discarded, leaving a pure set filter. rewriteis accepted on the wire for body compatibility (constant_scoredefault); the engine scores expansions as described above regardless of the setting.
Examples #
The pattern below puts the * mid-term — w*b-01 reads “w, anything,
then b-01” and matches the host web-01 (50 documents). The SQL twin
translates * to % and ? to _.
POST /my-collection/_search
{
"query": {
"wildcard": {
"field": "user.id",
"value": "ki*y"
}
}
}
Use cases #
- Mid-string holes. Values with a known head and tail but a variable
middle (
w*b-01overlogs.host→ 50 of 150) — the shape neither prefix nor suffix can express. - Single-character variation.
?covers ordinal or single-char variation without a full pattern language:web-0?forweb-01toweb-03(all 150 lines). - Glob-style file paths.
url.pathpatterns like/api/*(the 50/api/v1/...lines) or*.js(25 lines) read naturally as globs. - Partial IDs from user input. A search box feeding
*<input>*over keyword identifiers — convenient, but remember leading wildcards are the expensive case; anchor when the input allows it.
Parameters for wildcard
#
field
(Required, string) Field you wish to search.value
(Required, string) Wildcard pattern to match terms in the providedfieldagainst. Note: leading wildcards (*foo) are expensive — prefer theprefixquery when possible.case_insensitive
(Optional, boolean, default:false) ASCII case-insensitive matching of the value with the indexed field values.boost
(Optional, float, default:1.0) Multiplier for the clause’s score — with the constant-score rewrite this is the score itself.rewrite
(Optional, string) Method used to rewrite the expanded terms (constant_score,scoring_boolean, …).