query-string-query.asciidoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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'. Default to `false`.
  67. It is not allowed to set this option to `false` if `autoGeneratePhraseQueries` is already set to `true`.
  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. Note that this will not
  85. include nested documents, use a nested query to search those documents.
  86. [float]
  87. ==== Multi Field
  88. The `query_string` query can also run against multiple fields. Fields can be
  89. provided via the `"fields"` parameter (example below).
  90. The idea of running the `query_string` query against multiple fields is to
  91. expand each query term to an OR clause like this:
  92. field1:query_term OR field2:query_term | ...
  93. For example, the following query
  94. [source,js]
  95. --------------------------------------------------
  96. GET /_search
  97. {
  98. "query": {
  99. "query_string" : {
  100. "fields" : ["content", "name"],
  101. "query" : "this AND that"
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. // CONSOLE
  107. matches the same words as
  108. [source,js]
  109. --------------------------------------------------
  110. GET /_search
  111. {
  112. "query": {
  113. "query_string": {
  114. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  115. }
  116. }
  117. }
  118. --------------------------------------------------
  119. // CONSOLE
  120. Since several queries are generated from the individual search terms,
  121. combining them can be automatically done using either a `dis_max` query or a
  122. simple `bool` query. For example (the `name` is boosted by 5 using `^5`
  123. notation):
  124. [source,js]
  125. --------------------------------------------------
  126. GET /_search
  127. {
  128. "query": {
  129. "query_string" : {
  130. "fields" : ["content", "name^5"],
  131. "query" : "this AND that OR thus",
  132. "use_dis_max" : true
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // CONSOLE
  138. Simple wildcard can also be used to search "within" specific inner
  139. elements of the document. For example, if we have a `city` object with
  140. several fields (or inner object with fields) in it, we can automatically
  141. search on all "city" fields:
  142. [source,js]
  143. --------------------------------------------------
  144. GET /_search
  145. {
  146. "query": {
  147. "query_string" : {
  148. "fields" : ["city.*"],
  149. "query" : "this AND that OR thus",
  150. "use_dis_max" : true
  151. }
  152. }
  153. }
  154. --------------------------------------------------
  155. // CONSOLE
  156. Another option is to provide the wildcard fields search in the query
  157. string itself (properly escaping the `*` sign), for example:
  158. `city.\*:something`:
  159. [source,js]
  160. --------------------------------------------------
  161. GET /_search
  162. {
  163. "query": {
  164. "query_string" : {
  165. "query" : "city.\\*:(this AND that OR thus)",
  166. "use_dis_max" : true
  167. }
  168. }
  169. }
  170. --------------------------------------------------
  171. // CONSOLE
  172. NOTE: Since `\` (backslash) is a special character in json strings, it needs to
  173. be escaped, hence the two backslashes in the above `query_string`.
  174. When running the `query_string` query against multiple fields, the
  175. following additional parameters are allowed:
  176. [cols="<,<",options="header",]
  177. |=======================================================================
  178. |Parameter |Description
  179. |`use_dis_max` |Should the queries be combined using `dis_max` (set it
  180. to `true`), or a `bool` query (set it to `false`). Defaults to `true`.
  181. |`tie_breaker` |When using `dis_max`, the disjunction max tie breaker.
  182. Defaults to `0`.
  183. |=======================================================================
  184. The fields parameter can also include pattern based field names,
  185. allowing to automatically expand to the relevant fields (dynamically
  186. introduced fields included). For example:
  187. [source,js]
  188. --------------------------------------------------
  189. GET /_search
  190. {
  191. "query": {
  192. "query_string" : {
  193. "fields" : ["content", "name.*^5"],
  194. "query" : "this AND that OR thus",
  195. "use_dis_max" : true
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. // CONSOLE
  201. include::query-string-syntax.asciidoc[]