regexp-query.asciidoc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [[query-dsl-regexp-query]]
  2. === Regexp Query
  3. The `regexp` query allows you to use regular expression term queries.
  4. See <<regexp-syntax>> for details of the supported regular expression language.
  5. The "term queries" in that first sentence means that Elasticsearch will apply
  6. the regexp to the terms produced by the tokenizer for that field, and not
  7. to the original text of the field.
  8. *Note*: The performance of a `regexp` query heavily depends on the
  9. regular expression chosen. Matching everything like `.*` is very slow as
  10. well as using lookaround regular expressions. If possible, you should
  11. try to use a long prefix before your regular expression starts. Wildcard
  12. matchers like `.*?+` will mostly lower performance.
  13. [source,js]
  14. --------------------------------------------------
  15. {
  16. "regexp":{
  17. "name.first": "s.*y"
  18. }
  19. }
  20. --------------------------------------------------
  21. Boosting is also supported
  22. [source,js]
  23. --------------------------------------------------
  24. {
  25. "regexp":{
  26. "name.first":{
  27. "value":"s.*y",
  28. "boost":1.2
  29. }
  30. }
  31. }
  32. --------------------------------------------------
  33. You can also use special flags
  34. [source,js]
  35. --------------------------------------------------
  36. {
  37. "regexp":{
  38. "name.first": {
  39. "value": "s.*y",
  40. "flags" : "INTERSECTION|COMPLEMENT|EMPTY"
  41. }
  42. }
  43. }
  44. --------------------------------------------------
  45. Possible flags are `ALL` (default), `ANYSTRING`, `COMPLEMENT`,
  46. `EMPTY`, `INTERSECTION`, `INTERVAL`, or `NONE`. Please check the
  47. http://lucene.apache.org/core/4_9_0/core/org/apache/lucene/util/automaton/RegExp.html[Lucene
  48. documentation] for their meaning
  49. Regular expressions are dangerous because it's easy to accidentally
  50. create an innocuous looking one that requires an exponential number of
  51. internal determinized automaton states (and corresponding RAM and CPU)
  52. for Lucene to execute. Lucene prevents these using the
  53. `max_determinized_states` setting (defaults to 10000). You can raise
  54. this limit to allow more complex regular expressions to execute.
  55. [source,js]
  56. --------------------------------------------------
  57. {
  58. "regexp":{
  59. "name.first": {
  60. "value": "s.*y",
  61. "flags" : "INTERSECTION|COMPLEMENT|EMPTY",
  62. "max_determinized_states": 20000
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. include::regexp-syntax.asciidoc[]