query-string-query.asciidoc 7.1 KB

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