query-string-query.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. {
  8. "query_string" : {
  9. "default_field" : "content",
  10. "query" : "this AND that OR thus"
  11. }
  12. }
  13. --------------------------------------------------
  14. The `query_string` top level parameters include:
  15. [cols="<,<",options="header",]
  16. |=======================================================================
  17. |Parameter |Description
  18. |`query` |The actual query to be parsed. See <<query-string-syntax>>.
  19. |`default_field` |The default field for query terms if no prefix field
  20. is specified. Defaults to the `index.query.default_field` index
  21. settings, which in turn defaults to `_all`.
  22. |`default_operator` |The default operator used if no explicit operator
  23. is specified. For example, with a default operator of `OR`, the query
  24. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  25. with default operator of `AND`, the same query is translated to
  26. `capital AND of AND Hungary`. The default value is `OR`.
  27. |`analyzer` |The analyzer name used to analyze the query string.
  28. |`allow_leading_wildcard` |When set, `*` or `?` are allowed as the first
  29. character. Defaults to `true`.
  30. |`lowercase_expanded_terms` |Whether terms of wildcard, prefix, fuzzy,
  31. and range queries are to be automatically lower-cased or not (since they
  32. are not analyzed). Default it `true`.
  33. |`enable_position_increments` |Set to `true` to enable position
  34. increments in result queries. Defaults to `true`.
  35. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  36. expand to. Defaults to `50`
  37. |`fuzziness` |Set the fuzziness for fuzzy queries. Defaults
  38. to `AUTO`. See <<fuzziness>> for allowed settings.
  39. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  40. is `0`.
  41. |`phrase_slop` |Sets the default slop for phrases. If zero, then exact
  42. phrase matches are required. Default value is `0`.
  43. |`boost` |Sets the boost value of the query. Defaults to `1.0`.
  44. |`analyze_wildcard` |By default, wildcards terms in a query string are
  45. not analyzed. By setting this value to `true`, a best effort will be
  46. made to analyze those as well.
  47. |`auto_generate_phrase_queries` |Defaults to `false`.
  48. |`max_determinized_states` |Limit on how many automaton states regexp
  49. queries are allowed to create. This protects against too-difficult
  50. (e.g. exponentially hard) regexps. Defaults to 10000.
  51. |`minimum_should_match` |A value controlling how many "should" clauses
  52. in the resulting boolean query should match. It can be an absolute value
  53. (`2`), a percentage (`30%`) or a
  54. <<query-dsl-minimum-should-match,combination of
  55. both>>.
  56. |`lenient` |If set to `true` will cause format based failures (like
  57. providing text to a numeric field) to be ignored.
  58. |`locale` | Locale that should be used for string conversions.
  59. Defaults to `ROOT`.
  60. |`time_zone` | Time Zone to be applied to any range query related to dates. See also
  61. http://joda-time.sourceforge.net/api-release/org/joda/time/DateTimeZone.html[JODA timezone].
  62. |=======================================================================
  63. When a multi term query is being generated, one can control how it gets
  64. rewritten using the
  65. <<query-dsl-multi-term-rewrite,rewrite>>
  66. parameter.
  67. [float]
  68. ==== Default Field
  69. When not explicitly specifying the field to search on in the query
  70. string syntax, the `index.query.default_field` will be used to derive
  71. which field to search on. It defaults to `_all` field.
  72. So, if `_all` field is disabled, it might make sense to change it to set
  73. a different default field.
  74. [float]
  75. ==== Multi Field
  76. The `query_string` query can also run against multiple fields. Fields can be
  77. provided via the `"fields"` parameter (example below).
  78. The idea of running the `query_string` query against multiple fields is to
  79. expand each query term to an OR clause like this:
  80. field1:query_term OR field2:query_term | ...
  81. For example, the following query
  82. [source,js]
  83. --------------------------------------------------
  84. {
  85. "query_string" : {
  86. "fields" : ["content", "name"],
  87. "query" : "this AND that"
  88. }
  89. }
  90. --------------------------------------------------
  91. matches the same words as
  92. [source,js]
  93. --------------------------------------------------
  94. {
  95. "query_string": {
  96. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  97. }
  98. }
  99. --------------------------------------------------
  100. Since several queries are generated from the individual search terms,
  101. combining them can be automatically done using either a `dis_max` query or a
  102. simple `bool` query. For example (the `name` is boosted by 5 using `^5`
  103. notation):
  104. [source,js]
  105. --------------------------------------------------
  106. {
  107. "query_string" : {
  108. "fields" : ["content", "name^5"],
  109. "query" : "this AND that OR thus",
  110. "use_dis_max" : true
  111. }
  112. }
  113. --------------------------------------------------
  114. Simple wildcard can also be used to search "within" specific inner
  115. elements of the document. For example, if we have a `city` object with
  116. several fields (or inner object with fields) in it, we can automatically
  117. search on all "city" fields:
  118. [source,js]
  119. --------------------------------------------------
  120. {
  121. "query_string" : {
  122. "fields" : ["city.*"],
  123. "query" : "this AND that OR thus",
  124. "use_dis_max" : true
  125. }
  126. }
  127. --------------------------------------------------
  128. Another option is to provide the wildcard fields search in the query
  129. string itself (properly escaping the `*` sign), for example:
  130. `city.\*:something`.
  131. When running the `query_string` query against multiple fields, the
  132. following additional parameters are allowed:
  133. [cols="<,<",options="header",]
  134. |=======================================================================
  135. |Parameter |Description
  136. |`use_dis_max` |Should the queries be combined using `dis_max` (set it
  137. to `true`), or a `bool` query (set it to `false`). Defaults to `true`.
  138. |`tie_breaker` |When using `dis_max`, the disjunction max tie breaker.
  139. Defaults to `0`.
  140. |=======================================================================
  141. The fields parameter can also include pattern based field names,
  142. allowing to automatically expand to the relevant fields (dynamically
  143. introduced fields included). For example:
  144. [source,js]
  145. --------------------------------------------------
  146. {
  147. "query_string" : {
  148. "fields" : ["content", "name.*^5"],
  149. "query" : "this AND that OR thus",
  150. "use_dis_max" : true
  151. }
  152. }
  153. --------------------------------------------------
  154. include::query-string-syntax.asciidoc[]