simple-query-string-query.asciidoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. [[query-dsl-simple-query-string-query]]
  2. === Simple query string query
  3. ++++
  4. <titleabbrev>Simple query string</titleabbrev>
  5. ++++
  6. A query that uses the SimpleQueryParser to parse its context. Unlike the
  7. regular `query_string` query, the `simple_query_string` query will never
  8. throw an exception, and discards invalid parts of the query. Here is
  9. an example:
  10. [source,js]
  11. --------------------------------------------------
  12. GET /_search
  13. {
  14. "query": {
  15. "simple_query_string" : {
  16. "query": "\"fried eggs\" +(eggplant | potato) -frittata",
  17. "fields": ["title^5", "body"],
  18. "default_operator": "and"
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. The `simple_query_string` top level parameters include:
  25. [cols="<,<",options="header",]
  26. |=======================================================================
  27. |Parameter |Description
  28. |`query` |The actual query to be parsed. See below for syntax.
  29. |`fields` |The fields to perform the parsed query against. Defaults to the
  30. `index.query.default_field` index settings, which in turn defaults to `*`. `*`
  31. extracts all fields in the mapping that are eligible to term queries and filters
  32. the metadata fields.
  33. WARNING: There is a limit on the number of fields that can be queried
  34. at once. It is defined by the `indices.query.bool.max_clause_count` <<search-settings>>
  35. which defaults to 1024.
  36. |`default_operator` |The default operator used if no explicit operator
  37. is specified. For example, with a default operator of `OR`, the query
  38. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  39. with default operator of `AND`, the same query is translated to
  40. `capital AND of AND Hungary`. The default value is `OR`.
  41. |`analyzer` |Force the analyzer to use to analyze each term of the query when
  42. creating composite queries.
  43. |`flags` |A set of <<supported-flags,flags>> specifying which features of the
  44. `simple_query_string` to enable. Defaults to `ALL`.
  45. |`analyze_wildcard` | Whether terms of prefix queries should be automatically
  46. analyzed or not. If `true` a best effort will be made to analyze the prefix. However,
  47. some analyzers will be not able to provide a meaningful results
  48. based just on the prefix of a term. Defaults to `false`.
  49. |`lenient` | If set to `true` will cause format based failures
  50. (like providing text to a numeric field) to be ignored.
  51. |`minimum_should_match` | The minimum number of clauses that must match for a
  52. document to be returned. See the
  53. <<query-dsl-minimum-should-match,`minimum_should_match`>> documentation for the
  54. full list of options.
  55. |`quote_field_suffix` | A suffix to append to fields for quoted parts of
  56. the query string. This allows to use a field that has a different analysis chain
  57. for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
  58. comprehensive example.
  59. |`auto_generate_synonyms_phrase_query` |Whether phrase queries should be automatically generated for multi terms synonyms.
  60. Defaults to `true`.
  61. |`all_fields` | deprecated[6.0.0, set `fields` to `*` instead]
  62. Perform the query on all fields detected in the mapping that can
  63. be queried.
  64. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  65. is `0`.
  66. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  67. expand to. Defaults to `50`
  68. |`fuzzy_transpositions` |Set to `false` to disable fuzzy transpositions (`ab` -> `ba`).
  69. Default is `true`.
  70. |=======================================================================
  71. [float]
  72. ===== Simple Query String Syntax
  73. The `simple_query_string` supports the following special characters:
  74. * `+` signifies AND operation
  75. * `|` signifies OR operation
  76. * `-` negates a single token
  77. * `"` wraps a number of tokens to signify a phrase for searching
  78. * `*` at the end of a term signifies a prefix query
  79. * `(` and `)` signify precedence
  80. * `~N` after a word signifies edit distance (fuzziness)
  81. * `~N` after a phrase signifies slop amount
  82. In order to search for any of these special characters, they will need to
  83. be escaped with `\`.
  84. Be aware that this syntax may have a different behavior depending on the
  85. `default_operator` value. For example, consider the following query:
  86. [source,js]
  87. --------------------------------------------------
  88. GET /_search
  89. {
  90. "query": {
  91. "simple_query_string" : {
  92. "fields" : ["content"],
  93. "query" : "foo bar -baz"
  94. }
  95. }
  96. }
  97. --------------------------------------------------
  98. // CONSOLE
  99. You may expect that documents containing only "foo" or "bar" will be returned,
  100. as long as they do not contain "baz", however, due to the `default_operator`
  101. being OR, this really means "match documents that contain "foo" or documents
  102. that contain "bar", or documents that don't contain "baz". If this is unintended
  103. then the query can be switched to `"foo bar +-baz"` which will not return
  104. documents that contain "baz".
  105. [float]
  106. ==== Default Field
  107. When not explicitly specifying the field to search on in the query
  108. string syntax, the `index.query.default_field` will be used to derive
  109. which fields to search on. It defaults to `*` and the query will automatically
  110. attempt to determine the existing fields in the index's mapping that are queryable,
  111. and perform the search on those fields.
  112. [float]
  113. ==== Multi Field
  114. The fields parameter can also include pattern based field names,
  115. allowing to automatically expand to the relevant fields (dynamically
  116. introduced fields included). For example:
  117. [source,js]
  118. --------------------------------------------------
  119. GET /_search
  120. {
  121. "query": {
  122. "simple_query_string" : {
  123. "fields" : ["content", "name.*^5"],
  124. "query" : "foo bar baz"
  125. }
  126. }
  127. }
  128. --------------------------------------------------
  129. // CONSOLE
  130. [float]
  131. [[supported-flags]]
  132. ==== Flags
  133. `simple_query_string` support multiple flags to specify which parsing features
  134. should be enabled. It is specified as a `|`-delimited string with the
  135. `flags` parameter:
  136. [source,js]
  137. --------------------------------------------------
  138. GET /_search
  139. {
  140. "query": {
  141. "simple_query_string" : {
  142. "query" : "foo | bar + baz*",
  143. "flags" : "OR|AND|PREFIX"
  144. }
  145. }
  146. }
  147. --------------------------------------------------
  148. // CONSOLE
  149. The available flags are:
  150. [cols="<,<",options="header",]
  151. |=======================================================================
  152. |Flag |Description
  153. |`ALL` |Enables all parsing features. This is the default.
  154. |`NONE` |Switches off all parsing features.
  155. |`AND` |Enables the `+` AND operator.
  156. |`OR` |Enables the `\|` OR operator.
  157. |`NOT` |Enables the `-` NOT operator.
  158. |`PREFIX` |Enables the `*` Prefix operator.
  159. |`PHRASE` |Enables the `"` quotes operator used to search for phrases.
  160. |`PRECEDENCE` |Enables the `(` and `)` operators to control operator precedence.
  161. |`ESCAPE` |Enables `\` as the escape character.
  162. |`WHITESPACE` |Enables whitespaces as split characters.
  163. |`FUZZY` |Enables the `~N` operator after a word where N is an integer denoting the allowed edit distance for matching (see <<fuzziness>>).
  164. |`SLOP` |Enables the `~N` operator after a phrase where N is an integer denoting the slop amount.
  165. |`NEAR` |Synonymous to `SLOP`.
  166. |=======================================================================
  167. [float]
  168. ==== Synonyms
  169. The `simple_query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  170. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  171. For example, the following synonym: `"ny, new york" would produce:`
  172. `(ny OR ("new york"))`
  173. It is also possible to match multi terms synonyms with conjunctions instead:
  174. [source,js]
  175. --------------------------------------------------
  176. GET /_search
  177. {
  178. "query": {
  179. "simple_query_string" : {
  180. "query" : "ny city",
  181. "auto_generate_synonyms_phrase_query" : false
  182. }
  183. }
  184. }
  185. --------------------------------------------------
  186. // CONSOLE
  187. The example above creates a boolean query:
  188. `(ny OR (new AND york)) city)`
  189. that matches documents with the term `ny` or the conjunction `new AND york`.
  190. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.