query-string-query.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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` top level parameters include:
  19. [cols="<,<",options="header",]
  20. |=======================================================================
  21. |Parameter |Description
  22. |`query` |The actual query to be parsed. See <<query-string-syntax>>.
  23. |`default_field` |The default field for query terms if no prefix field
  24. is specified. Defaults to the `index.query.default_field` index
  25. settings, which in turn defaults to `_all`.
  26. |`default_operator` |The default operator used if no explicit operator
  27. is specified. For example, with a default operator of `OR`, the query
  28. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  29. with default operator of `AND`, the same query is translated to
  30. `capital AND of AND Hungary`. The default value is `OR`.
  31. |`analyzer` |The analyzer name used to analyze the query string.
  32. |`allow_leading_wildcard` |When set, `*` or `?` are allowed as the first
  33. character. Defaults to `true`.
  34. |`lowercase_expanded_terms` |Whether terms of wildcard, prefix, fuzzy,
  35. and range queries are to be automatically lower-cased or not (since they
  36. are not analyzed). Default it `true`.
  37. |`enable_position_increments` |Set to `true` to enable position
  38. increments in result queries. Defaults to `true`.
  39. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  40. expand to. Defaults to `50`
  41. |`fuzziness` |Set the fuzziness for fuzzy queries. Defaults
  42. to `AUTO`. See <<fuzziness>> for allowed settings.
  43. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  44. is `0`.
  45. |`phrase_slop` |Sets the default slop for phrases. If zero, then exact
  46. phrase matches are required. Default value is `0`.
  47. |`boost` |Sets the boost value of the query. Defaults to `1.0`.
  48. |`analyze_wildcard` |By default, wildcards terms in a query string are
  49. not analyzed. By setting this value to `true`, a best effort will be
  50. made to analyze those as well.
  51. |`auto_generate_phrase_queries` |Defaults to `false`.
  52. |`max_determinized_states` |Limit on how many automaton states regexp
  53. queries are allowed to create. This protects against too-difficult
  54. (e.g. exponentially hard) regexps. Defaults to 10000.
  55. |`minimum_should_match` |A value controlling how many "should" clauses
  56. in the resulting boolean query should match. It can be an absolute value
  57. (`2`), a percentage (`30%`) or a
  58. <<query-dsl-minimum-should-match,combination of
  59. both>>.
  60. |`lenient` |If set to `true` will cause format based failures (like
  61. providing text to a numeric field) to be ignored.
  62. |`locale` | Locale that should be used for string conversions.
  63. Defaults to `ROOT`.
  64. |`time_zone` | Time Zone to be applied to any range query related to dates. See also
  65. http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html[JODA timezone].
  66. |=======================================================================
  67. When a multi term query is being generated, one can control how it gets
  68. rewritten using the
  69. <<query-dsl-multi-term-rewrite,rewrite>>
  70. parameter.
  71. [float]
  72. ==== Default Field
  73. When not explicitly specifying the field to search on in the query
  74. string syntax, the `index.query.default_field` will be used to derive
  75. which field to search on. It defaults to `_all` field.
  76. So, if `_all` field is disabled, it might make sense to change it to set
  77. a different default field.
  78. [float]
  79. ==== Multi Field
  80. The `query_string` query can also run against multiple fields. Fields can be
  81. provided via the `"fields"` parameter (example below).
  82. The idea of running the `query_string` query against multiple fields is to
  83. expand each query term to an OR clause like this:
  84. field1:query_term OR field2:query_term | ...
  85. For example, the following query
  86. [source,js]
  87. --------------------------------------------------
  88. GET /_search
  89. {
  90. "query": {
  91. "query_string" : {
  92. "fields" : ["content", "name"],
  93. "query" : "this AND that"
  94. }
  95. }
  96. }
  97. --------------------------------------------------
  98. // CONSOLE
  99. matches the same words as
  100. [source,js]
  101. --------------------------------------------------
  102. GET /_search
  103. {
  104. "query": {
  105. "query_string": {
  106. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. // CONSOLE
  112. Since several queries are generated from the individual search terms,
  113. combining them can be automatically done using either a `dis_max` query or a
  114. simple `bool` query. For example (the `name` is boosted by 5 using `^5`
  115. notation):
  116. [source,js]
  117. --------------------------------------------------
  118. GET /_search
  119. {
  120. "query": {
  121. "query_string" : {
  122. "fields" : ["content", "name^5"],
  123. "query" : "this AND that OR thus",
  124. "use_dis_max" : true
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. // CONSOLE
  130. Simple wildcard can also be used to search "within" specific inner
  131. elements of the document. For example, if we have a `city` object with
  132. several fields (or inner object with fields) in it, we can automatically
  133. search on all "city" fields:
  134. [source,js]
  135. --------------------------------------------------
  136. GET /_search
  137. {
  138. "query": {
  139. "query_string" : {
  140. "fields" : ["city.*"],
  141. "query" : "this AND that OR thus",
  142. "use_dis_max" : true
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. // CONSOLE
  148. Another option is to provide the wildcard fields search in the query
  149. string itself (properly escaping the `*` sign), for example:
  150. `city.\*:something`.
  151. When running the `query_string` query against multiple fields, the
  152. following additional parameters are allowed:
  153. [cols="<,<",options="header",]
  154. |=======================================================================
  155. |Parameter |Description
  156. |`use_dis_max` |Should the queries be combined using `dis_max` (set it
  157. to `true`), or a `bool` query (set it to `false`). Defaults to `true`.
  158. |`tie_breaker` |When using `dis_max`, the disjunction max tie breaker.
  159. Defaults to `0`.
  160. |=======================================================================
  161. The fields parameter can also include pattern based field names,
  162. allowing to automatically expand to the relevant fields (dynamically
  163. introduced fields included). For example:
  164. [source,js]
  165. --------------------------------------------------
  166. GET /_search
  167. {
  168. "query": {
  169. "query_string" : {
  170. "fields" : ["content", "name.*^5"],
  171. "query" : "this AND that OR thus",
  172. "use_dis_max" : true
  173. }
  174. }
  175. }
  176. --------------------------------------------------
  177. // CONSOLE
  178. include::query-string-syntax.asciidoc[]