query-string-syntax.asciidoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. [[query-string-syntax]]
  2. ===== Query string syntax
  3. The query string ``mini-language'' is used by the
  4. <<query-dsl-query-string-query>> and by the
  5. `q` query string parameter in the <<search-search,`search` API>>.
  6. The query string is parsed into a series of _terms_ and _operators_. A
  7. term can be a single word -- `quick` or `brown` -- or a phrase, surrounded by
  8. double quotes -- `"quick brown"` -- which searches for all the words in the
  9. phrase, in the same order.
  10. Operators allow you to customize the search -- the available options are
  11. explained below.
  12. ====== Field names
  13. You can specify fields to search in the query syntax:
  14. * where the `status` field contains `active`
  15. status:active
  16. * where the `title` field contains `quick` or `brown`
  17. title:(quick OR brown)
  18. * where the `author` field contains the exact phrase `"john smith"`
  19. author:"John Smith"
  20. * where the `first name` field contains `Alice` (note how we need to escape
  21. the space with a backslash)
  22. first\ name:Alice
  23. * where any of the fields `book.title`, `book.content` or `book.date` contains
  24. `quick` or `brown` (note how we need to escape the `*` with a backslash):
  25. book.\*:(quick OR brown)
  26. * where the field `title` has any non-null value:
  27. _exists_:title
  28. [[query-string-wildcard]]
  29. ====== Wildcards
  30. Wildcard searches can be run on individual terms, using `?` to replace
  31. a single character, and `*` to replace zero or more characters:
  32. qu?ck bro*
  33. Be aware that wildcard queries can use an enormous amount of memory and
  34. perform very badly -- just think how many terms need to be queried to
  35. match the query string `"a* b* c*"`.
  36. [WARNING]
  37. =======
  38. Pure wildcards `\*` are rewritten to <<query-dsl-exists-query,`exists`>> queries for efficiency.
  39. As a consequence, the wildcard `"field:*"` would match documents with an empty value
  40. like the following:
  41. ```
  42. {
  43. "field": ""
  44. }
  45. ```
  46. \... and would **not** match if the field is missing or set with an explicit null
  47. value like the following:
  48. ```
  49. {
  50. "field": null
  51. }
  52. ```
  53. =======
  54. [WARNING]
  55. =======
  56. Allowing a wildcard at the beginning of a word (eg `"*ing"`) is particularly
  57. heavy, because all terms in the index need to be examined, just in case
  58. they match. Leading wildcards can be disabled by setting
  59. `allow_leading_wildcard` to `false`.
  60. =======
  61. Only parts of the analysis chain that operate at the character level are
  62. applied. So for instance, if the analyzer performs both lowercasing and
  63. stemming, only the lowercasing will be applied: it would be wrong to perform
  64. stemming on a word that is missing some of its letters.
  65. By setting `analyze_wildcard` to true, queries that end with a `*` will be
  66. analyzed and a boolean query will be built out of the different tokens, by
  67. ensuring exact matches on the first N-1 tokens, and prefix match on the last
  68. token.
  69. ====== Regular expressions
  70. Regular expression patterns can be embedded in the query string by
  71. wrapping them in forward-slashes (`"/"`):
  72. name:/joh?n(ath[oa]n)/
  73. The supported regular expression syntax is explained in <<regexp-syntax>>.
  74. [WARNING]
  75. =======
  76. The `allow_leading_wildcard` parameter does not have any control over
  77. regular expressions. A query string such as the following would force
  78. Elasticsearch to visit every term in the index:
  79. /.*n/
  80. Use with caution!
  81. =======
  82. [[query-string-fuzziness]]
  83. ====== Fuzziness
  84. You can run <<query-dsl-fuzzy-query,`fuzzy` queries>> using the `~` operator:
  85. quikc~ brwn~ foks~
  86. For these queries, the query string is <<analysis-normalizers,normalized>>. If
  87. present, only certain filters from the analyzer are applied. For a list of
  88. applicable filters, see <<analysis-normalizers>>.
  89. The query uses the
  90. {wikipedia}/Damerau-Levenshtein_distance[Damerau-Levenshtein distance]
  91. to find all terms with a maximum of
  92. two changes, where a change is the insertion, deletion
  93. or substitution of a single character, or transposition of two adjacent
  94. characters.
  95. The default _edit distance_ is `2`, but an edit distance of `1` should be
  96. sufficient to catch 80% of all human misspellings. It can be specified as:
  97. quikc~1
  98. [[avoid-widlcards-fuzzy-searches]]
  99. [WARNING]
  100. .Avoid mixing fuzziness with wildcards
  101. ====
  102. Mixing <<fuzziness,fuzzy>> and <<query-string-wildcard,wildcard>> operators is
  103. _not_ supported. When mixed, one of the operators is not applied. For example,
  104. you can search for `app~1` (fuzzy) or `app*` (wildcard), but searches for
  105. `app*~1` do not apply the fuzzy operator (`~1`).
  106. ====
  107. ====== Proximity searches
  108. While a phrase query (eg `"john smith"`) expects all of the terms in exactly
  109. the same order, a proximity query allows the specified words to be further
  110. apart or in a different order. In the same way that fuzzy queries can
  111. specify a maximum edit distance for characters in a word, a proximity search
  112. allows us to specify a maximum edit distance of words in a phrase:
  113. "fox quick"~5
  114. The closer the text in a field is to the original order specified in the
  115. query string, the more relevant that document is considered to be. When
  116. compared to the above example query, the phrase `"quick fox"` would be
  117. considered more relevant than `"quick brown fox"`.
  118. ====== Ranges
  119. Ranges can be specified for date, numeric or string fields. Inclusive ranges
  120. are specified with square brackets `[min TO max]` and exclusive ranges with
  121. curly brackets `{min TO max}`.
  122. * All days in 2012:
  123. date:[2012-01-01 TO 2012-12-31]
  124. * Numbers 1..5
  125. count:[1 TO 5]
  126. * Tags between `alpha` and `omega`, excluding `alpha` and `omega`:
  127. tag:{alpha TO omega}
  128. * Numbers from 10 upwards
  129. count:[10 TO *]
  130. * Dates before 2012
  131. date:{* TO 2012-01-01}
  132. Curly and square brackets can be combined:
  133. * Numbers from 1 up to but not including 5
  134. count:[1 TO 5}
  135. Ranges with one side unbounded can use the following syntax:
  136. age:>10
  137. age:>=10
  138. age:<10
  139. age:<=10
  140. [NOTE]
  141. ====================================================================
  142. To combine an upper and lower bound with the simplified syntax, you
  143. would need to join two clauses with an `AND` operator:
  144. age:(>=10 AND <20)
  145. age:(+>=10 +<20)
  146. ====================================================================
  147. The parsing of ranges in query strings can be complex and error prone. It is
  148. much more reliable to use an explicit <<query-dsl-range-query,`range` query>>.
  149. ====== Boosting
  150. Use the _boost_ operator `^` to make one term more relevant than another.
  151. For instance, if we want to find all documents about foxes, but we are
  152. especially interested in quick foxes:
  153. quick^2 fox
  154. The default `boost` value is 1, but can be any positive floating point number.
  155. Boosts between 0 and 1 reduce relevance.
  156. Boosts can also be applied to phrases or to groups:
  157. "john smith"^2 (foo bar)^4
  158. ====== Boolean operators
  159. By default, all terms are optional, as long as one term matches. A search
  160. for `foo bar baz` will find any document that contains one or more of
  161. `foo` or `bar` or `baz`. We have already discussed the `default_operator`
  162. above which allows you to force all terms to be required, but there are
  163. also _boolean operators_ which can be used in the query string itself
  164. to provide more control.
  165. The preferred operators are `+` (this term *must* be present) and `-`
  166. (this term *must not* be present). All other terms are optional.
  167. For example, this query:
  168. quick brown +fox -news
  169. states that:
  170. * `fox` must be present
  171. * `news` must not be present
  172. * `quick` and `brown` are optional -- their presence increases the relevance
  173. The familiar boolean operators `AND`, `OR` and `NOT` (also written `&&`, `||`
  174. and `!`) are also supported but beware that they do not honor the usual
  175. precedence rules, so parentheses should be used whenever multiple operators are
  176. used together. For instance the previous query could be rewritten as:
  177. `((quick AND fox) OR (brown AND fox) OR fox) AND NOT news`::
  178. This form now replicates the logic from the original query correctly, but
  179. the relevance scoring bears little resemblance to the original.
  180. In contrast, the same query rewritten using the <<query-dsl-match-query,`match` query>>
  181. would look like this:
  182. {
  183. "bool": {
  184. "must": { "match": "fox" },
  185. "should": { "match": "quick brown" },
  186. "must_not": { "match": "news" }
  187. }
  188. }
  189. ====== Grouping
  190. Multiple terms or clauses can be grouped together with parentheses, to form
  191. sub-queries:
  192. (quick OR brown) AND fox
  193. Groups can be used to target a particular field, or to boost the result
  194. of a sub-query:
  195. status:(active OR pending) title:(full text search)^2
  196. ====== Reserved characters
  197. If you need to use any of the characters which function as operators in your
  198. query itself (and not as operators), then you should escape them with
  199. a leading backslash. For instance, to search for `(1+1)=2`, you would
  200. need to write your query as `\(1\+1\)\=2`. When using JSON for the request body, two preceding backslashes (`\\`) are required; the backslash is a reserved escaping character in JSON strings.
  201. [source,console]
  202. ----
  203. GET /my-index-000001/_search
  204. {
  205. "query" : {
  206. "query_string" : {
  207. "query" : "kimchy\\!",
  208. "fields" : ["user.id"]
  209. }
  210. }
  211. }
  212. ----
  213. // TEST[setup:my_index]
  214. The reserved characters are: `+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /`
  215. Failing to escape these special characters correctly could lead to a syntax error which prevents your query from running.
  216. NOTE: `<` and `>` can't be escaped at all. The only way to prevent them from
  217. attempting to create a range query is to remove them from the query string
  218. entirely.
  219. ====== Whitespaces and empty queries
  220. Whitespace is not considered an operator.
  221. If the query string is empty or only contains whitespaces the query will
  222. yield an empty result set.