Regexp query
Use the regexp query to search for terms that match a regular expression. For more information about writing regular expressions, see Regular expression syntax.
The following query searches for any term that starts with any uppercase or lowercase letter followed by amlet:
GET shakespeare/_search
{
"query": {
"regexp": {
"play_name": "[a-zA-Z]amlet"
}
}
}
Note the following important considerations:
- Regular expressions are applied to the terms (that is, tokens) in the field—not to the entire field.
- By default, the maximum length of a regular expression is 1,000 characters. To change the maximum length, update the
index.max_regex_lengthsetting. - Regular expressions use the Lucene syntax, which differs from more standardized implementations. Test thoroughly to ensure that you receive the results you expect. To learn more, see the Lucene documentation.
- To improve regexp query performance, avoid wildcard patterns without a prefix or suffix, such as
.*or.*?+. regexpqueries can be expensive operations and require thesearch.allow_expensive_queriessetting to be set totrue. Before making frequentregexpqueries, test their impact on cluster performance and examine alternative queries that may achieve similar results.- The wildcard field type builds an index that is specially designed to be very efficient for wildcard and regular expression queries.
Matching terms instead of field values
A regexp query is not analyzed, but the field it searches might be. When a text field is indexed, the standard analyzer splits its value into lowercase terms, and the regular expression must match one of those terms. As a result, a pattern containing uppercase letters or spaces returns no results for a text field.
To try this, index a document into an index that uses dynamic mapping. The title field is mapped as text and has a title.keyword subfield:
PUT my-index/_doc/1?refresh=true
{
"title": "Henry IV"
}
The title field contains the terms henry and iv, so the following query returns no results:
GET my-index/_search
{
"query": {
"regexp": {
"title": "Henry.*"
}
}
}
To match the original field value, including its capitalization and spaces, search the keyword subfield:
GET my-index/_search
{
"query": {
"regexp": {
"title.keyword": "Henry I.*"
}
}
}
Response
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my-index",
"_id": "1",
"_score": 1.0,
"_source": {
"title": "Henry IV"
}
}
]
}
}
If a regexp query returns no results, also check the following:
- The pattern must match the entire term. For example,
hendoes not match the termhenry, buthen.*does. - The
^and$anchors are not supported. For example,^Henry.*does not matchHenry IVin akeywordfield. For more information, see Unsupported features. - Matching is case sensitive by default. To match terms regardless of case, set
case_insensitivetotrue. For example,henry ivmatchesHenry IVin thetitle.keywordfield whencase_insensitiveistrue.
Parameters
The query accepts the name of the field (<field>) as a top-level parameter:
GET _search
{
"query": {
"regexp": {
"<field>": {
"value": "[Ss]ample",
...
}
}
}
}
The <field> accepts the following parameters. All parameters except value are optional.
| Parameter | Data type | Description |
|---|---|---|
value | String | The regular expression used for matching terms in the field specified in <field>. |
boost | Floating-point | A floating-point value that specifies the weight of this field toward the relevance score. Values above 1.0 increase the field’s relevance. Values between 0.0 and 1.0 decrease the field’s relevance. Default is 1.0. |
case_insensitive | Boolean | If true, allows case-insensitive matching of the regular expression value with the indexed field values. Default is false (case sensitivity is determined by the field’s mapping). |
flags | String | Enables optional operators for Lucene’s regular expression engine. For valid values, see Optional operators. |
max_determinized_states | Integer | Lucene converts a regular expression to an automaton with a number of determinized states. This parameter specifies the maximum number of automaton states the query requires. Use this parameter to prevent high resource consumption. To run complex regular expressions, you may need to increase the value of this parameter. Default is 10,000. |
rewrite | String | Determines 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, then regexp queries are not executed.