query-string-query.asciidoc 7.1 KB

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