query-string-syntax.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. As mentioned in <<query-dsl-query-string-query>>, the `default_field` is searched for the
  14. search terms, but it is possible to specify other fields in the query syntax:
  15. * where the `status` field contains `active`
  16. status:active
  17. * where the `title` field contains `quick` or `brown`.
  18. If you omit the OR operator the default operator will be used
  19. title:(quick OR brown)
  20. title:(quick brown)
  21. * where the `author` field contains the exact phrase `"john smith"`
  22. author:"John Smith"
  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 brown)
  26. * where the field `title` has any non-null value:
  27. _exists_:title
  28. ===== Wildcards
  29. Wildcard searches can be run on individual terms, using `?` to replace
  30. a single character, and `*` to replace zero or more characters:
  31. qu?ck bro*
  32. Be aware that wildcard queries can use an enormous amount of memory and
  33. perform very badly -- just think how many terms need to be queried to
  34. match the query string `"a* b* c*"`.
  35. [WARNING]
  36. =======
  37. Allowing a wildcard at the beginning of a word (eg `"*ing"`) is particularly
  38. heavy, because all terms in the index need to be examined, just in case
  39. they match. Leading wildcards can be disabled by setting
  40. `allow_leading_wildcard` to `false`.
  41. =======
  42. Wildcarded terms are not analyzed by default -- they are lowercased
  43. (`lowercase_expanded_terms` defaults to `true`) but no further analysis
  44. is done, mainly because it is impossible to accurately analyze a word that
  45. is missing some of its letters. However, by setting `analyze_wildcard` to
  46. `true`, an attempt will be made to analyze wildcarded words before searching
  47. the term list for matching terms.
  48. ===== Regular expressions
  49. Regular expression patterns can be embedded in the query string by
  50. wrapping them in forward-slashes (`"/"`):
  51. name:/joh?n(ath[oa]n)/
  52. The supported regular expression syntax is explained in <<regexp-syntax>>.
  53. [WARNING]
  54. =======
  55. The `allow_leading_wildcard` parameter does not have any control over
  56. regular expressions. A query string such as the following would force
  57. Elasticsearch to visit every term in the index:
  58. /.*n/
  59. Use with caution!
  60. =======
  61. ===== Fuzziness
  62. We can search for terms that are
  63. similar to, but not exactly like our search terms, using the ``fuzzy''
  64. operator:
  65. quikc~ brwn~ foks~
  66. This uses the
  67. http://en.wikipedia.org/wiki/Damerau-Levenshtein_distance[Damerau-Levenshtein distance]
  68. to find all terms with a maximum of
  69. two changes, where a change is the insertion, deletion
  70. or substitution of a single character, or transposition of two adjacent
  71. characters.
  72. The default _edit distance_ is `2`, but an edit distance of `1` should be
  73. sufficient to catch 80% of all human misspellings. It can be specified as:
  74. quikc~1
  75. ===== Proximity searches
  76. While a phrase query (eg `"john smith"`) expects all of the terms in exactly
  77. the same order, a proximity query allows the specified words to be further
  78. apart or in a different order. In the same way that fuzzy queries can
  79. specify a maximum edit distance for characters in a word, a proximity search
  80. allows us to specify a maximum edit distance of words in a phrase:
  81. "fox quick"~5
  82. The closer the text in a field is to the original order specified in the
  83. query string, the more relevant that document is considered to be. When
  84. compared to the above example query, the phrase `"quick fox"` would be
  85. considered more relevant than `"quick brown fox"`.
  86. ===== Ranges
  87. Ranges can be specified for date, numeric or string fields. Inclusive ranges
  88. are specified with square brackets `[min TO max]` and exclusive ranges with
  89. curly brackets `{min TO max}`.
  90. * All days in 2012:
  91. date:[2012-01-01 TO 2012-12-31]
  92. * Numbers 1..5
  93. count:[1 TO 5]
  94. * Tags between `alpha` and `omega`, excluding `alpha` and `omega`:
  95. tag:{alpha TO omega}
  96. * Numbers from 10 upwards
  97. count:[10 TO *]
  98. * Dates before 2012
  99. date:{* TO 2012-01-01}
  100. Curly and square brackets can be combined:
  101. * Numbers from 1 up to but not including 5
  102. count:[1 TO 5}
  103. Ranges with one side unbounded can use the following syntax:
  104. age:>10
  105. age:>=10
  106. age:<10
  107. age:<=10
  108. [NOTE]
  109. ====================================================================
  110. To combine an upper and lower bound with the simplified syntax, you
  111. would need to join two clauses with an `AND` operator:
  112. age:(>=10 AND <20)
  113. age:(+>=10 +<20)
  114. ====================================================================
  115. The parsing of ranges in query strings can be complex and error prone. It is
  116. much more reliable to use an explicit <<query-dsl-range-query,`range` query>>.
  117. ===== Boosting
  118. Use the _boost_ operator `^` to make one term more relevant than another.
  119. For instance, if we want to find all documents about foxes, but we are
  120. especially interested in quick foxes:
  121. quick^2 fox
  122. The default `boost` value is 1, but can be any positive floating point number.
  123. Boosts between 0 and 1 reduce relevance.
  124. Boosts can also be applied to phrases or to groups:
  125. "john smith"^2 (foo bar)^4
  126. ===== Boolean operators
  127. By default, all terms are optional, as long as one term matches. A search
  128. for `foo bar baz` will find any document that contains one or more of
  129. `foo` or `bar` or `baz`. We have already discussed the `default_operator`
  130. above which allows you to force all terms to be required, but there are
  131. also _boolean operators_ which can be used in the query string itself
  132. to provide more control.
  133. The preferred operators are `+` (this term *must* be present) and `-`
  134. (this term *must not* be present). All other terms are optional.
  135. For example, this query:
  136. quick brown +fox -news
  137. states that:
  138. * `fox` must be present
  139. * `news` must not be present
  140. * `quick` and `brown` are optional -- their presence increases the relevance
  141. The familiar operators `AND`, `OR` and `NOT` (also written `&&`, `||` and `!`)
  142. are also supported. However, the effects of these operators can be more
  143. complicated than is obvious at first glance. `NOT` takes precedence over
  144. `AND`, which takes precedence over `OR`. While the `+` and `-` only affect
  145. the term to the right of the operator, `AND` and `OR` can affect the terms to
  146. the left and right.
  147. ****
  148. Rewriting the above query using `AND`, `OR` and `NOT` demonstrates the
  149. complexity:
  150. `quick OR brown AND fox AND NOT news`::
  151. This is incorrect, because `brown` is now a required term.
  152. `(quick OR brown) AND fox AND NOT news`::
  153. This is incorrect because at least one of `quick` or `brown` is now required
  154. and the search for those terms would be scored differently from the original
  155. query.
  156. `((quick AND fox) OR (brown AND fox) OR fox) AND NOT news`::
  157. This form now replicates the logic from the original query correctly, but
  158. the relevance scoring bears little resemblance to the original.
  159. In contrast, the same query rewritten using the <<query-dsl-match-query,`match` query>>
  160. would look like this:
  161. {
  162. "bool": {
  163. "must": { "match": "fox" },
  164. "should": { "match": "quick brown" },
  165. "must_not": { "match": "news" }
  166. }
  167. }
  168. ****
  169. ===== Grouping
  170. Multiple terms or clauses can be grouped together with parentheses, to form
  171. sub-queries:
  172. (quick OR brown) AND fox
  173. Groups can be used to target a particular field, or to boost the result
  174. of a sub-query:
  175. status:(active OR pending) title:(full text search)^2
  176. ===== Reserved characters
  177. If you need to use any of the characters which function as operators in your
  178. query itself (and not as operators), then you should escape them with
  179. a leading backslash. For instance, to search for `(1+1)=2`, you would
  180. need to write your query as `\(1\+1\)\=2`.
  181. The reserved characters are: `+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /`
  182. Failing to escape these special characters correctly could lead to a syntax
  183. error which prevents your query from running.
  184. .Watch this space
  185. ****
  186. A space may also be a reserved character. For instance, if you have a
  187. synonym list which converts `"wi fi"` to `"wifi"`, a `query_string` search
  188. for `"wi fi"` would fail. The query string parser would interpret your
  189. query as a search for `"wi OR fi"`, while the token stored in your
  190. index is actually `"wifi"`. Escaping the space will protect it from
  191. being touched by the query string parser: `"wi\ fi"`.
  192. ****
  193. ===== Empty Query
  194. If the query string is empty or only contains whitespaces the query will
  195. yield an empty result set.