Regexp query
Returns documents that contain terms matching a regular expression.
A regular expression is a way to match patterns in data using placeholder characters, called operators. For a list of operators supported by the regexp query, see Regular expression syntax.
Example request
The following search returns documents where the user.id field contains any term that begins with k and ends with y. The .* operators match any characters of any length, including no characters. Matching terms can include ky, kay, and kimchy.
resp = client.search(
query={
"regexp": {
"user.id": {
"value": "k.*y",
"flags": "ALL",
"case_insensitive": True,
"max_determinized_states": 10000,
"rewrite": "constant_score_blended"
}
}
},
)
print(resp) response = client.search(
body: {
query: {
regexp: {
'user.id' => {
value: 'k.*y',
flags: 'ALL',
case_insensitive: true,
max_determinized_states: 10_000,
rewrite: 'constant_score_blended'
}
}
}
}
)
puts response const response = await client.search({
query: {
regexp: {
"user.id": {
value: "k.*y",
flags: "ALL",
case_insensitive: true,
max_determinized_states: 10000,
rewrite: "constant_score_blended",
},
},
},
});
console.log(response); GET /_search
{
"query": {
"regexp": {
"user.id": {
"value": "k.*y",
"flags": "ALL",
"case_insensitive": true,
"max_determinized_states": 10000,
"rewrite": "constant_score_blended"
}
}
}
} Top-level parameters for regexp
-
<field> - (Required, object) Field you wish to search.
Parameters for <field>
-
value -
(Required, string) Regular expression for terms you wish to find in the provided
<field>. For a list of supported operators, see Regular expression syntax.By default, regular expressions are limited to 1,000 characters. You can change this limit using the
index.max_regex_lengthsetting.The performance of the
regexpquery can vary based on the regular expression provided. To improve performance, avoid using wildcard patterns, such as.*or.*?+, without a prefix or suffix. -
flags - (Optional, string) Enables optional operators for the regular expression. For valid values and more information, see Regular expression syntax.
-
case_insensitive[7.10.0] Added in 7.10.0. - (Optional, Boolean) Allows case insensitive matching of the regular expression value with the indexed field values when set to true. Default is false which means the case sensitivity of matching depends on the underlying field’s mapping.
-
max_determinized_states -
(Optional, integer) Maximum number of automaton states required for the query. Default is
10000.Elasticsearch uses Apache Lucene internally to parse regular expressions. Lucene converts each regular expression to a finite automaton containing a number of determinized states.
You can use this parameter to prevent that conversion from unintentionally consuming too many resources. You may need to increase this limit to run complex regular expressions.
-
rewrite - (Optional, string) Method used to rewrite the query. For valid values and more information, see the
rewriteparameter.
Notes
Allow expensive queries
Regexp queries will not be executed if search.allow_expensive_queries is set to false.
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/query-dsl-regexp-query.html