2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-regexp-query]]
|
2015-06-03 19:59:22 -04:00
|
|
|
=== Regexp Query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
The `regexp` query allows you to use regular expression term queries.
|
2013-10-07 08:42:13 -04:00
|
|
|
See <<regexp-syntax>> for details of the supported regular expression language.
|
2014-07-03 05:39:08 -04:00
|
|
|
The "term queries" in that first sentence means that Elasticsearch will apply
|
|
|
|
the regexp to the terms produced by the tokenizer for that field, and not
|
|
|
|
to the original text of the field.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
*Note*: The performance of a `regexp` query heavily depends on the
|
|
|
|
regular expression chosen. Matching everything like `.*` is very slow as
|
|
|
|
well as using lookaround regular expressions. If possible, you should
|
|
|
|
try to use a long prefix before your regular expression starts. Wildcard
|
|
|
|
matchers like `.*?+` will mostly lower performance.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-11 08:37:19 -04:00
|
|
|
"query": {
|
|
|
|
"regexp":{
|
|
|
|
"name.first": "s.*y"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Boosting is also supported
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-11 08:37:19 -04:00
|
|
|
"query": {
|
|
|
|
"regexp":{
|
|
|
|
"name.first":{
|
|
|
|
"value":"s.*y",
|
2016-05-12 06:58:22 -04:00
|
|
|
"boost":1.2
|
2016-05-11 08:37:19 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
You can also use special flags
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-11 08:37:19 -04:00
|
|
|
"query": {
|
|
|
|
"regexp":{
|
|
|
|
"name.first": {
|
|
|
|
"value": "s.*y",
|
2016-05-12 06:58:22 -04:00
|
|
|
"flags" : "INTERSECTION|COMPLEMENT|EMPTY"
|
2016-05-11 08:37:19 -04:00
|
|
|
}
|
2014-04-01 14:43:38 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-12-30 13:53:15 -05:00
|
|
|
Possible flags are `ALL` (default), `ANYSTRING`, `COMPLEMENT`,
|
2013-08-28 19:24:34 -04:00
|
|
|
`EMPTY`, `INTERSECTION`, `INTERVAL`, or `NONE`. Please check the
|
2014-07-03 05:39:08 -04:00
|
|
|
http://lucene.apache.org/core/4_9_0/core/org/apache/lucene/util/automaton/RegExp.html[Lucene
|
2013-08-28 19:24:34 -04:00
|
|
|
documentation] for their meaning
|
|
|
|
|
2014-11-10 13:43:48 -05:00
|
|
|
Regular expressions are dangerous because it's easy to accidentally
|
|
|
|
create an innocuous looking one that requires an exponential number of
|
|
|
|
internal determinized automaton states (and corresponding RAM and CPU)
|
|
|
|
for Lucene to execute. Lucene prevents these using the
|
|
|
|
`max_determinized_states` setting (defaults to 10000). You can raise
|
|
|
|
this limit to allow more complex regular expressions to execute.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
GET /_search
|
2014-11-10 13:43:48 -05:00
|
|
|
{
|
2016-05-11 08:37:19 -04:00
|
|
|
"query": {
|
|
|
|
"regexp":{
|
|
|
|
"name.first": {
|
|
|
|
"value": "s.*y",
|
|
|
|
"flags" : "INTERSECTION|COMPLEMENT|EMPTY",
|
|
|
|
"max_determinized_states": 20000
|
|
|
|
}
|
2014-11-10 13:43:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
// CONSOLE
|
2013-10-07 08:42:13 -04:00
|
|
|
|
|
|
|
include::regexp-syntax.asciidoc[]
|