simple-query-string-query.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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
  29. and filters the metadata fields.
  30. |`default_operator` |The default operator used if no explicit operator
  31. is specified. For example, with a default operator of `OR`, the query
  32. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  33. with default operator of `AND`, the same query is translated to
  34. `capital AND of AND Hungary`. The default value is `OR`.
  35. |`analyzer` |Force the analyzer to use to analyze each term of the query when
  36. creating composite queries.
  37. |`flags` |Flags specifying which features of the `simple_query_string` to
  38. enable. Defaults to `ALL`.
  39. |`analyze_wildcard` | Whether terms of prefix queries should be automatically
  40. analyzed or not. If `true` a best effort will be made to analyze the prefix. However,
  41. some analyzers will be not able to provide a meaningful results
  42. based just on the prefix of a term. Defaults to `false`.
  43. |`lenient` | If set to `true` will cause format based failures
  44. (like providing text to a numeric field) to be ignored.
  45. |`minimum_should_match` | The minimum number of clauses that must match for a
  46. document to be returned. See the
  47. <<query-dsl-minimum-should-match,`minimum_should_match`>> documentation for the
  48. full list of options.
  49. |`quote_field_suffix` | A suffix to append to fields for quoted parts of
  50. the query string. This allows to use a field that has a different analysis chain
  51. for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
  52. comprehensive example.
  53. |`auto_generate_synonyms_phrase_query` |Whether phrase queries should be automatically generated for multi terms synonyms.
  54. Defaults to `true`.
  55. |`all_fields` | deprecated[6.0.0, set `fields` to `*` instead]
  56. Perform the query on all fields detected in the mapping that can
  57. be queried.
  58. |=======================================================================
  59. [float]
  60. ===== Simple Query String Syntax
  61. The `simple_query_string` supports the following special characters:
  62. * `+` signifies AND operation
  63. * `|` signifies OR operation
  64. * `-` negates a single token
  65. * `"` wraps a number of tokens to signify a phrase for searching
  66. * `*` at the end of a term signifies a prefix query
  67. * `(` and `)` signify precedence
  68. * `~N` after a word signifies edit distance (fuzziness)
  69. * `~N` after a phrase signifies slop amount
  70. In order to search for any of these special characters, they will need to
  71. be escaped with `\`.
  72. Be aware that this syntax may have a different behavior depending on the
  73. `default_operator` value. For example, consider the following query:
  74. [source,js]
  75. --------------------------------------------------
  76. GET /_search
  77. {
  78. "query": {
  79. "simple_query_string" : {
  80. "fields" : ["content"],
  81. "query" : "foo bar -baz"
  82. }
  83. }
  84. }
  85. --------------------------------------------------
  86. // CONSOLE
  87. You may expect that documents containing only "foo" or "bar" will be returned,
  88. as long as they do not contain "baz", however, due to the `default_operator`
  89. being OR, this really means "match documents that contain "foo" or documents
  90. that contain "bar", or documents that don't contain "baz". If this is unintended
  91. then the query can be switched to `"foo bar +-baz"` which will not return
  92. documents that contain "baz".
  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 fields to search on. It defaults to `*` and the query will automatically
  98. attempt to determine the existing fields in the index's mapping that are queryable,
  99. and perform the search on those fields.
  100. [float]
  101. ==== Multi Field
  102. The fields parameter can also include pattern based field names,
  103. allowing to automatically expand to the relevant fields (dynamically
  104. introduced fields included). For example:
  105. [source,js]
  106. --------------------------------------------------
  107. GET /_search
  108. {
  109. "query": {
  110. "simple_query_string" : {
  111. "fields" : ["content", "name.*^5"],
  112. "query" : "foo bar baz"
  113. }
  114. }
  115. }
  116. --------------------------------------------------
  117. // CONSOLE
  118. [float]
  119. ==== Flags
  120. `simple_query_string` support multiple flags to specify which parsing features
  121. should be enabled. It is specified as a `|`-delimited string with the
  122. `flags` parameter:
  123. [source,js]
  124. --------------------------------------------------
  125. GET /_search
  126. {
  127. "query": {
  128. "simple_query_string" : {
  129. "query" : "foo | bar + baz*",
  130. "flags" : "OR|AND|PREFIX"
  131. }
  132. }
  133. }
  134. --------------------------------------------------
  135. // CONSOLE
  136. The available flags are: `ALL`, `NONE`, `AND`, `OR`, `NOT`, `PREFIX`, `PHRASE`,
  137. `PRECEDENCE`, `ESCAPE`, `WHITESPACE`, `FUZZY`, `NEAR`, and `SLOP`.
  138. [float]
  139. ==== Synonyms
  140. The `simple_query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  141. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  142. For example, the following synonym: `"ny, new york" would produce:`
  143. `(ny OR ("new york"))`
  144. It is also possible to match multi terms synonyms with conjunctions instead:
  145. [source,js]
  146. --------------------------------------------------
  147. GET /_search
  148. {
  149. "query": {
  150. "simple_query_string" : {
  151. "query" : "ny city",
  152. "auto_generate_synonyms_phrase_query" : false
  153. }
  154. }
  155. }
  156. --------------------------------------------------
  157. // CONSOLE
  158. The example above creates a boolean query:
  159. `(ny OR (new AND york)) city)`
  160. that matches documents with the term `ny` or the conjunction `new AND york`.
  161. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.