1
0

query-string-query.asciidoc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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` query parses the input and splits text around logical operators.
  19. Each textual part is analyzed independently of each other. For instance the following query:
  20. [source,js]
  21. --------------------------------------------------
  22. GET /_search
  23. {
  24. "query": {
  25. "query_string" : {
  26. "default_field" : "content",
  27. "query" : "(new york) AND ny"
  28. }
  29. }
  30. }
  31. --------------------------------------------------
  32. // CONSOLE
  33. ... will be splitted in `new york` and `ny` and each part is analyzed independently.
  34. When multiple fields are provided it is also possible to modify how the different
  35. field queries are combined inside each textual part using the `type` parameter.
  36. The possible modes are described <<multi-match-types, here>> and the default is `best_fields`.
  37. The `query_string` top level parameters include:
  38. [cols="<,<",options="header",]
  39. |=======================================================================
  40. |Parameter |Description
  41. |`query` |The actual query to be parsed. See <<query-string-syntax>>.
  42. |`default_field` |The default field for query terms if no prefix field
  43. is specified. Defaults to the `index.query.default_field` index
  44. settings, which in turn defaults to `_all`.
  45. |`default_operator` |The default operator used if no explicit operator
  46. is specified. For example, with a default operator of `OR`, the query
  47. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  48. with default operator of `AND`, the same query is translated to
  49. `capital AND of AND Hungary`. The default value is `OR`.
  50. |`analyzer` |The analyzer name used to analyze the query string.
  51. |`allow_leading_wildcard` |When set, `*` or `?` are allowed as the first
  52. character. Defaults to `true`.
  53. |`enable_position_increments` |Set to `true` to enable position
  54. increments in result queries. Defaults to `true`.
  55. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  56. expand to. Defaults to `50`
  57. |`fuzziness` |Set the fuzziness for fuzzy queries. Defaults
  58. to `AUTO`. See <<fuzziness>> for allowed settings.
  59. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  60. is `0`.
  61. |`phrase_slop` |Sets the default slop for phrases. If zero, then exact
  62. phrase matches are required. Default value is `0`.
  63. |`boost` |Sets the boost value of the query. Defaults to `1.0`.
  64. |`auto_generate_phrase_queries` |Defaults to `false`.
  65. |`analyze_wildcard` |By default, wildcards terms in a query string are
  66. not analyzed. By setting this value to `true`, a best effort will be
  67. made to analyze those as well.
  68. |`max_determinized_states` |Limit on how many automaton states regexp
  69. queries are allowed to create. This protects against too-difficult
  70. (e.g. exponentially hard) regexps. Defaults to 10000.
  71. |`minimum_should_match` |A value controlling how many "should" clauses
  72. in the resulting boolean query should match. It can be an absolute value
  73. (`2`), a percentage (`30%`) or a
  74. <<query-dsl-minimum-should-match,combination of
  75. both>>.
  76. |`lenient` |If set to `true` will cause format based failures (like
  77. providing text to a numeric field) to be ignored.
  78. |`time_zone` | Time Zone to be applied to any range query related to dates. See also
  79. http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html[JODA timezone].
  80. |`quote_field_suffix` | A suffix to append to fields for quoted parts of
  81. the query string. This allows to use a field that has a different analysis chain
  82. for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
  83. comprehensive example.
  84. |`all_fields` | Perform the query on all fields detected in the mapping that can
  85. be queried. Will be used by default when the `_all` field is disabled and no
  86. `default_field` is specified (either in the index settings or in the request
  87. body) and no `fields` are specified.
  88. |=======================================================================
  89. When a multi term query is being generated, one can control how it gets
  90. rewritten using the
  91. <<query-dsl-multi-term-rewrite,rewrite>>
  92. parameter.
  93. [float]
  94. ==== Default Field
  95. When not explicitly specifying the field to search on in the query
  96. string syntax, the `index.query.default_field` will be used to derive
  97. which field to search on. It defaults to `_all` field.
  98. If the `_all` field is disabled, the `query_string` query will automatically
  99. attempt to determine the existing fields in the index's mapping that are
  100. queryable, and perform the search on those fields. Note that this will not
  101. include nested documents, use a nested query to search those documents.
  102. [float]
  103. ==== Multi Field
  104. The `query_string` query can also run against multiple fields. Fields can be
  105. provided via the `"fields"` parameter (example below).
  106. The idea of running the `query_string` query against multiple fields is to
  107. expand each query term to an OR clause like this:
  108. field1:query_term OR field2:query_term | ...
  109. For example, the following query
  110. [source,js]
  111. --------------------------------------------------
  112. GET /_search
  113. {
  114. "query": {
  115. "query_string" : {
  116. "fields" : ["content", "name"],
  117. "query" : "this AND that"
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // CONSOLE
  123. matches the same words as
  124. [source,js]
  125. --------------------------------------------------
  126. GET /_search
  127. {
  128. "query": {
  129. "query_string": {
  130. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  131. }
  132. }
  133. }
  134. --------------------------------------------------
  135. // CONSOLE
  136. Since several queries are generated from the individual search terms,
  137. combining them is automatically done using a `dis_max` query with a tie_breaker.
  138. For example (the `name` is boosted by 5 using `^5` notation):
  139. [source,js]
  140. --------------------------------------------------
  141. GET /_search
  142. {
  143. "query": {
  144. "query_string" : {
  145. "fields" : ["content", "name^5"],
  146. "query" : "this AND that OR thus",
  147. "tie_breaker" : 0
  148. }
  149. }
  150. }
  151. --------------------------------------------------
  152. // CONSOLE
  153. Simple wildcard can also be used to search "within" specific inner
  154. elements of the document. For example, if we have a `city` object with
  155. several fields (or inner object with fields) in it, we can automatically
  156. search on all "city" fields:
  157. [source,js]
  158. --------------------------------------------------
  159. GET /_search
  160. {
  161. "query": {
  162. "query_string" : {
  163. "fields" : ["city.*"],
  164. "query" : "this AND that OR thus"
  165. }
  166. }
  167. }
  168. --------------------------------------------------
  169. // CONSOLE
  170. Another option is to provide the wildcard fields search in the query
  171. string itself (properly escaping the `*` sign), for example:
  172. `city.\*:something`:
  173. [source,js]
  174. --------------------------------------------------
  175. GET /_search
  176. {
  177. "query": {
  178. "query_string" : {
  179. "query" : "city.\\*:(this AND that OR thus)"
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // CONSOLE
  185. NOTE: Since `\` (backslash) is a special character in json strings, it needs to
  186. be escaped, hence the two backslashes in the above `query_string`.
  187. When running the `query_string` query against multiple fields, the
  188. following additional parameters are allowed:
  189. [cols="<,<",options="header",]
  190. |=======================================================================
  191. |Parameter |Description
  192. |`type` |How the fields should be combined to build the text query.
  193. See <<multi-match-types, types>> for a complete example.
  194. Defaults to `best_fields`
  195. |=======================================================================
  196. [cols="<,<",options="header",]
  197. |=======================================================================
  198. |Parameter |Description
  199. |`tie_breaker` |The disjunction max tie breaker for multi fields.
  200. Defaults to `0`
  201. |=======================================================================
  202. The fields parameter can also include pattern based field names,
  203. allowing to automatically expand to the relevant fields (dynamically
  204. introduced fields included). For example:
  205. [source,js]
  206. --------------------------------------------------
  207. GET /_search
  208. {
  209. "query": {
  210. "query_string" : {
  211. "fields" : ["content", "name.*^5"],
  212. "query" : "this AND that OR thus"
  213. }
  214. }
  215. }
  216. --------------------------------------------------
  217. // CONSOLE
  218. include::query-string-syntax.asciidoc[]