query-string-query.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. [[query-dsl-query-string-query]]
  2. === Query String Query
  3. A query that uses a query parser in order to parse its content. Here is
  4. an example:
  5. [source,js]
  6. --------------------------------------------------
  7. GET /_search
  8. {
  9. "query": {
  10. "query_string" : {
  11. "default_field" : "content",
  12. "query" : "this AND that OR thus"
  13. }
  14. }
  15. }
  16. --------------------------------------------------
  17. // CONSOLE
  18. The `query_string` query parses the input and splits text around operators.
  19. Each textual part is analyzed independently of each other. For instance the following query:
  20. [source,js]
  21. --------------------------------------------------
  22. GET /_search
  23. {
  24. "query": {
  25. "query_string" : {
  26. "default_field" : "content",
  27. "query" : "field:new york AND city"
  28. }
  29. }
  30. }
  31. --------------------------------------------------
  32. // CONSOLE
  33. ... will be splitted in `new`, `york` and `city` and each part is analyzed independently.
  34. When multiple fields are provided it is also possible to modify how the different
  35. field queries are combined inside each textual part using the `type` parameter.
  36. The possible modes are described <<multi-match-types, here>> and the default is `best_fields`.
  37. The `query_string` top level parameters include:
  38. [cols="<,<",options="header",]
  39. |=======================================================================
  40. |Parameter |Description
  41. |`query` |The actual query to be parsed. See <<query-string-syntax>>.
  42. |`default_field` |The default field for query terms if no prefix field is
  43. specified. Defaults to the `index.query.default_field` index settings, which in
  44. turn defaults to `*`. `*` extracts all fields in the mapping that are eligible
  45. to term queries and filters the metadata fields. All extracted fields are then
  46. combined to build a query when no prefix field is provided. There is a limit of
  47. no more than 1024 fields being queried at once.
  48. |`default_operator` |The default operator used if no explicit operator
  49. is specified. For example, with a default operator of `OR`, the query
  50. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  51. with default operator of `AND`, the same query is translated to
  52. `capital AND of AND Hungary`. The default value is `OR`.
  53. |`analyzer` |The analyzer name used to analyze the query string.
  54. |`quote_analyzer` |The name of the analyzer that is used to analyze
  55. quoted phrases in the query string. For those parts, it overrides other
  56. analyzers that are set using the `analyzer` parameter or the
  57. <<search-quote-analyzer,`search_quote_analyzer`>> setting.
  58. |`allow_leading_wildcard` |When set, `*` or `?` are allowed as the first
  59. character. Defaults to `true`.
  60. |`enable_position_increments` |Set to `true` to enable position
  61. increments in result queries. Defaults to `true`.
  62. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  63. expand to. Defaults to `50`
  64. |`fuzziness` |Set the fuzziness for fuzzy queries. Defaults
  65. to `AUTO`. See <<fuzziness>> for allowed settings.
  66. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  67. is `0`.
  68. |`fuzzy_transpositions` |Set to `false` to disable fuzzy transpositions (`ab` -> `ba`).
  69. Default is `true`.
  70. |`phrase_slop` |Sets the default slop for phrases. If zero, then exact
  71. phrase matches are required. Default value is `0`.
  72. |`boost` |Sets the boost value of the query. Defaults to `1.0`.
  73. |`auto_generate_phrase_queries` |Defaults to `false`.
  74. |`analyze_wildcard` |By default, wildcards terms in a query string are
  75. not analyzed. By setting this value to `true`, a best effort will be
  76. made to analyze those as well.
  77. |`max_determinized_states` |Limit on how many automaton states regexp
  78. queries are allowed to create. This protects against too-difficult
  79. (e.g. exponentially hard) regexps. Defaults to 10000.
  80. |`minimum_should_match` |A value controlling how many "should" clauses
  81. in the resulting boolean query should match. It can be an absolute value
  82. (`2`), a percentage (`30%`) or a
  83. <<query-dsl-minimum-should-match,combination of
  84. both>>.
  85. |`lenient` |If set to `true` will cause format based failures (like
  86. providing text to a numeric field) to be ignored.
  87. |`time_zone` | Time Zone to be applied to any range query related to dates. See also
  88. http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html[JODA timezone].
  89. |`quote_field_suffix` | A suffix to append to fields for quoted parts of
  90. the query string. This allows to use a field that has a different analysis chain
  91. for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
  92. comprehensive example.
  93. |`auto_generate_synonyms_phrase_query` |Whether phrase queries should be automatically generated for multi terms synonyms.
  94. Defaults to `true`.
  95. |`all_fields` | deprecated[6.0.0, set `default_field` to `*` instead]
  96. Perform the query on all fields detected in the mapping that can
  97. be queried.
  98. |=======================================================================
  99. When a multi term query is being generated, one can control how it gets
  100. rewritten using the
  101. <<query-dsl-multi-term-rewrite,rewrite>>
  102. parameter.
  103. [float]
  104. ==== Default Field
  105. When not explicitly specifying the field to search on in the query
  106. string syntax, the `index.query.default_field` will be used to derive
  107. which field to search on. If the `index.query.default_field` is not specified,
  108. the `query_string` will automatically attempt to determine the existing fields in the index's
  109. mapping that are queryable, and perform the search on those fields. Note that this will not
  110. include nested documents, use a nested query to search those documents.
  111. [float]
  112. ==== Multi Field
  113. The `query_string` query can also run against multiple fields. Fields can be
  114. provided via the `"fields"` parameter (example below).
  115. The idea of running the `query_string` query against multiple fields is to
  116. expand each query term to an OR clause like this:
  117. field1:query_term OR field2:query_term | ...
  118. For example, the following query
  119. [source,js]
  120. --------------------------------------------------
  121. GET /_search
  122. {
  123. "query": {
  124. "query_string" : {
  125. "fields" : ["content", "name"],
  126. "query" : "this AND that"
  127. }
  128. }
  129. }
  130. --------------------------------------------------
  131. // CONSOLE
  132. matches the same words as
  133. [source,js]
  134. --------------------------------------------------
  135. GET /_search
  136. {
  137. "query": {
  138. "query_string": {
  139. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  140. }
  141. }
  142. }
  143. --------------------------------------------------
  144. // CONSOLE
  145. Since several queries are generated from the individual search terms,
  146. combining them is automatically done using a `dis_max` query with a tie_breaker.
  147. For example (the `name` is boosted by 5 using `^5` notation):
  148. [source,js]
  149. --------------------------------------------------
  150. GET /_search
  151. {
  152. "query": {
  153. "query_string" : {
  154. "fields" : ["content", "name^5"],
  155. "query" : "this AND that OR thus",
  156. "tie_breaker" : 0
  157. }
  158. }
  159. }
  160. --------------------------------------------------
  161. // CONSOLE
  162. Simple wildcard can also be used to search "within" specific inner
  163. elements of the document. For example, if we have a `city` object with
  164. several fields (or inner object with fields) in it, we can automatically
  165. search on all "city" fields:
  166. [source,js]
  167. --------------------------------------------------
  168. GET /_search
  169. {
  170. "query": {
  171. "query_string" : {
  172. "fields" : ["city.*"],
  173. "query" : "this AND that OR thus"
  174. }
  175. }
  176. }
  177. --------------------------------------------------
  178. // CONSOLE
  179. Another option is to provide the wildcard fields search in the query
  180. string itself (properly escaping the `*` sign), for example:
  181. `city.\*:something`:
  182. [source,js]
  183. --------------------------------------------------
  184. GET /_search
  185. {
  186. "query": {
  187. "query_string" : {
  188. "query" : "city.\\*:(this AND that OR thus)"
  189. }
  190. }
  191. }
  192. --------------------------------------------------
  193. // CONSOLE
  194. NOTE: Since `\` (backslash) is a special character in json strings, it needs to
  195. be escaped, hence the two backslashes in the above `query_string`.
  196. When running the `query_string` query against multiple fields, the
  197. following additional parameters are allowed:
  198. [cols="<,<",options="header",]
  199. |=======================================================================
  200. |Parameter |Description
  201. |`type` |How the fields should be combined to build the text query.
  202. See <<multi-match-types, types>> for a complete example.
  203. Defaults to `best_fields`
  204. |=======================================================================
  205. [cols="<,<",options="header",]
  206. |=======================================================================
  207. |Parameter |Description
  208. |`tie_breaker` |The disjunction max tie breaker for multi fields.
  209. Defaults to `0`
  210. |=======================================================================
  211. The fields parameter can also include pattern based field names,
  212. allowing to automatically expand to the relevant fields (dynamically
  213. introduced fields included). For example:
  214. [source,js]
  215. --------------------------------------------------
  216. GET /_search
  217. {
  218. "query": {
  219. "query_string" : {
  220. "fields" : ["content", "name.*^5"],
  221. "query" : "this AND that OR thus"
  222. }
  223. }
  224. }
  225. --------------------------------------------------
  226. // CONSOLE
  227. [float]
  228. ==== Synonyms
  229. The `query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  230. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  231. For example, the following synonym: `"ny, new york" would produce:`
  232. `(ny OR ("new york"))`
  233. It is also possible to match multi terms synonyms with conjunctions instead:
  234. [source,js]
  235. --------------------------------------------------
  236. GET /_search
  237. {
  238. "query": {
  239. "query_string" : {
  240. "default_field": "title",
  241. "query" : "ny city",
  242. "auto_generate_synonyms_phrase_query" : false
  243. }
  244. }
  245. }
  246. --------------------------------------------------
  247. // CONSOLE
  248. The example above creates a boolean query:
  249. `(ny OR (new AND york)) city)`
  250. that matches documents with the term `ny` or the conjunction `new AND york`.
  251. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.
  252. include::query-string-syntax.asciidoc[]