query-string-query.asciidoc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. |`all_fields` | Perform the query on all fields detected in the mapping that can
  69. be queried. Will be used by default when the `_all` field is disabled and no
  70. `default_field` is specified (either in the index settings or in the request
  71. body) and no `fields` are specified.
  72. |=======================================================================
  73. When a multi term query is being generated, one can control how it gets
  74. rewritten using the
  75. <<query-dsl-multi-term-rewrite,rewrite>>
  76. parameter.
  77. [float]
  78. ==== Default Field
  79. When not explicitly specifying the field to search on in the query
  80. string syntax, the `index.query.default_field` will be used to derive
  81. which field to search on. It defaults to `_all` field.
  82. If the `_all` field is disabled, the `query_string` query will automatically
  83. attempt to determine the existing fields in the index's mapping that are
  84. queryable, and perform the search on those fields.
  85. [float]
  86. ==== Multi Field
  87. The `query_string` query can also run against multiple fields. Fields can be
  88. provided via the `"fields"` parameter (example below).
  89. The idea of running the `query_string` query against multiple fields is to
  90. expand each query term to an OR clause like this:
  91. field1:query_term OR field2:query_term | ...
  92. For example, the following query
  93. [source,js]
  94. --------------------------------------------------
  95. GET /_search
  96. {
  97. "query": {
  98. "query_string" : {
  99. "fields" : ["content", "name"],
  100. "query" : "this AND that"
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. // CONSOLE
  106. matches the same words as
  107. [source,js]
  108. --------------------------------------------------
  109. GET /_search
  110. {
  111. "query": {
  112. "query_string": {
  113. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // CONSOLE
  119. Since several queries are generated from the individual search terms,
  120. combining them can be automatically done using either a `dis_max` query or a
  121. simple `bool` query. For example (the `name` is boosted by 5 using `^5`
  122. notation):
  123. [source,js]
  124. --------------------------------------------------
  125. GET /_search
  126. {
  127. "query": {
  128. "query_string" : {
  129. "fields" : ["content", "name^5"],
  130. "query" : "this AND that OR thus",
  131. "use_dis_max" : true
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. // CONSOLE
  137. Simple wildcard can also be used to search "within" specific inner
  138. elements of the document. For example, if we have a `city` object with
  139. several fields (or inner object with fields) in it, we can automatically
  140. search on all "city" fields:
  141. [source,js]
  142. --------------------------------------------------
  143. GET /_search
  144. {
  145. "query": {
  146. "query_string" : {
  147. "fields" : ["city.*"],
  148. "query" : "this AND that OR thus",
  149. "use_dis_max" : true
  150. }
  151. }
  152. }
  153. --------------------------------------------------
  154. // CONSOLE
  155. Another option is to provide the wildcard fields search in the query
  156. string itself (properly escaping the `*` sign), for example:
  157. `city.\*:something`.
  158. When running the `query_string` query against multiple fields, the
  159. following additional parameters are allowed:
  160. [cols="<,<",options="header",]
  161. |=======================================================================
  162. |Parameter |Description
  163. |`use_dis_max` |Should the queries be combined using `dis_max` (set it
  164. to `true`), or a `bool` query (set it to `false`). Defaults to `true`.
  165. |`tie_breaker` |When using `dis_max`, the disjunction max tie breaker.
  166. Defaults to `0`.
  167. |=======================================================================
  168. The fields parameter can also include pattern based field names,
  169. allowing to automatically expand to the relevant fields (dynamically
  170. introduced fields included). For example:
  171. [source,js]
  172. --------------------------------------------------
  173. GET /_search
  174. {
  175. "query": {
  176. "query_string" : {
  177. "fields" : ["content", "name.*^5"],
  178. "query" : "this AND that OR thus",
  179. "use_dis_max" : true
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // CONSOLE
  185. include::query-string-syntax.asciidoc[]