simple-query-string-query.asciidoc 7.9 KB

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