Wildcard query

Use wildcard queries to search for terms that match a wildcard pattern. Wildcard queries support the following operators.

OperatorDescription
*Matches zero or more characters.
?Matches any single character.
case_insensitiveIf true, the wildcard query is case insensitive. If false, the wildcard query is case sensitive. Default is false (case sensitive).

For a case-sensitive search for terms that start with H and end with Y, use the following request:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "speaker": {
  6. "value": "H*Y",
  7. "case_insensitive": false
  8. }
  9. }
  10. }
  11. }

copy

If you change * to ?, you get no matches because ? refers to a single character.

Wildcard queries tend to be slow because they need to iterate over a lot of terms. Avoid placing wildcard characters at the beginning of a query because it could be a very expensive operation in terms of both resources and time.

Parameters

The query accepts the name of the field (<field>) as a top-level parameter:

  1. GET _search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "<field>": {
  6. "value": "patt*rn",
  7. ...
  8. }
  9. }
  10. }
  11. }

copy

The <field> accepts the following parameters. All parameters except value are optional.

ParameterData typeDescription
valueStringThe wildcard pattern used for matching terms in the field specified in <field>.
boostFloating-pointBoosts the query by the given multiplier. Useful for searches that contain more than one query. Values in the [0, 1) range decrease relevance, and values greater than 1 increase relevance. Default is 1.
case_insensitiveBooleanIf true, allows case-insensitive matching of the value with the indexed field values. Default is false (case sensitivity is determined by the field’s mapping).
rewriteStringDetermines how OpenSearch rewrites and scores multi-term queries. Valid values are constant_score, scoring_boolean, constant_score_boolean, top_terms_N, top_terms_boost_N, and top_terms_blended_freqs_N. Default is constant_score.

If search.allow_expensive_queries is set to false, wildcard queries are not run.