query-string-query.asciidoc 10 KB

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