simple-query-string-query.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. [[query-dsl-simple-query-string-query]]
  2. === Simple query string query
  3. ++++
  4. <titleabbrev>Simple query string</titleabbrev>
  5. ++++
  6. Returns documents based on a provided query string, using a parser with a
  7. limited but fault-tolerant syntax.
  8. This query uses a <<simple-query-string-syntax,simple syntax>> to parse and
  9. split the provided query string into terms based on special operators. The query
  10. then <<analysis,analyzes>> each term independently before returning matching
  11. documents.
  12. While its syntax is more limited than the
  13. <<query-dsl-query-string-query,`query_string` query>>, the `simple_query_string`
  14. query does not return errors for invalid syntax. Instead, it ignores any invalid
  15. parts of the query string.
  16. [[simple-query-string-query-ex-request]]
  17. ==== Example request
  18. [source,console]
  19. --------------------------------------------------
  20. GET /_search
  21. {
  22. "query": {
  23. "simple_query_string" : {
  24. "query": "\"fried eggs\" +(eggplant | potato) -frittata",
  25. "fields": ["title^5", "body"],
  26. "default_operator": "and"
  27. }
  28. }
  29. }
  30. --------------------------------------------------
  31. [[simple-query-string-top-level-params]]
  32. ==== Top-level parameters for `simple_query_string`
  33. `query`::
  34. (Required, string) Query string you wish to parse and use for search. See <<simple-query-string-syntax>>.
  35. `fields`::
  36. +
  37. --
  38. (Optional, array of strings) Array of fields you wish to search.
  39. This field accepts wildcard expressions. You also can boost relevance scores for
  40. matches to particular fields using a caret (`^`) notation. See
  41. <<simple-query-string-boost>> for examples.
  42. Defaults to the `index.query.default_field` index setting, which has a default
  43. value of `*`. The `*` value extracts all fields that are eligible to term
  44. queries and filters the metadata fields. All extracted fields are then combined
  45. to build a query if no `prefix` is specified.
  46. WARNING: There is a limit on the number of fields that can be queried at once.
  47. It is defined by the `indices.query.bool.max_clause_count`
  48. <<search-settings,search setting>>, which defaults to `1024`.
  49. --
  50. `default_operator`::
  51. +
  52. --
  53. (Optional, string) Default boolean logic used to interpret text in the query
  54. string if no operators are specified. Valid values are:
  55. `OR` (Default)::
  56. For example, a query string of `capital of Hungary` is interpreted as `capital
  57. OR of OR Hungary`.
  58. `AND`::
  59. For example, a query string of `capital of Hungary` is interpreted as `capital
  60. AND of AND Hungary`.
  61. --
  62. `analyze_wildcard`::
  63. (Optional, boolean) If `true`, the query attempts to analyze wildcard terms in
  64. the query string. Defaults to `false`.
  65. `analyzer`::
  66. (Optional, string) <<analysis,Analyzer>> used to convert text in the
  67. query string into tokens. Defaults to the
  68. <<specify-index-time-analyzer,index-time analyzer>> mapped for the
  69. `default_field`. If no analyzer is mapped, the index's default analyzer is used.
  70. `auto_generate_synonyms_phrase_query`::
  71. (Optional, boolean) If `true`, <<query-dsl-match-query-phrase,match phrase>>
  72. queries are automatically created for multi-term synonyms. Defaults to `true`.
  73. See <<simple-query-string-synonyms>> for an example.
  74. `flags`::
  75. (Optional, string) List of enabled operators for the
  76. <<simple-query-string-syntax,simple query string syntax>>. Defaults to `ALL`
  77. (all operators). See <<supported-flags>> for valid values.
  78. `fuzzy_max_expansions`::
  79. (Optional, integer) Maximum number of terms to which the query expands for fuzzy
  80. matching. Defaults to `50`.
  81. `fuzzy_prefix_length`::
  82. (Optional, integer) Number of beginning characters left unchanged for fuzzy
  83. matching. Defaults to `0`.
  84. `fuzzy_transpositions`::
  85. (Optional, boolean) If `true`, edits for fuzzy matching include
  86. transpositions of two adjacent characters (ab → ba). Defaults to `true`.
  87. `lenient`::
  88. (Optional, boolean) If `true`, format-based errors, such as providing a text
  89. value for a <<number,numeric>> field, are ignored. Defaults to `false`.
  90. `minimum_should_match`::
  91. (Optional, string) Minimum number of clauses that must match for a document to
  92. be returned. See the <<query-dsl-minimum-should-match, `minimum_should_match`
  93. parameter>> for valid values and more information.
  94. `quote_field_suffix`::
  95. +
  96. --
  97. (Optional, string) Suffix appended to quoted text in the query string.
  98. You can use this suffix to use a different analysis method for exact matches.
  99. See <<mixing-exact-search-with-stemming>>.
  100. --
  101. [[simple-query-string-query-notes]]
  102. ==== Notes
  103. [[simple-query-string-syntax]]
  104. ===== Simple query string syntax
  105. The `simple_query_string` query supports the following operators:
  106. * `+` signifies AND operation
  107. * `|` signifies OR operation
  108. * `-` negates a single token
  109. * `"` wraps a number of tokens to signify a phrase for searching
  110. * `*` at the end of a term signifies a prefix query
  111. * `(` and `)` signify precedence
  112. * `~N` after a word signifies edit distance (fuzziness)
  113. * `~N` after a phrase signifies slop amount
  114. To use one of these characters literally, escape it with a preceding backslash
  115. (`\`).
  116. The behavior of these operators may differ depending on the `default_operator`
  117. value. For example:
  118. [source,console]
  119. --------------------------------------------------
  120. GET /_search
  121. {
  122. "query": {
  123. "simple_query_string": {
  124. "fields": [ "content" ],
  125. "query": "foo bar -baz"
  126. }
  127. }
  128. }
  129. --------------------------------------------------
  130. This search is intended to only return documents containing `foo` or `bar` that
  131. also do **not** contain `baz`. However because of a `default_operator` of `OR`,
  132. this search actually returns documents that contain `foo` or `bar` and any
  133. documents that don't contain `baz`. To return documents as intended, change the
  134. query string to `foo bar +-baz`.
  135. [[supported-flags]]
  136. ===== Limit operators
  137. You can use the `flags` parameter to limit the supported operators for the
  138. simple query string syntax.
  139. To explicitly enable only specific operators, use a `|` separator. For example,
  140. a `flags` value of `OR|AND|PREFIX` disables all operators except `OR`, `AND`,
  141. and `PREFIX`.
  142. [source,console]
  143. --------------------------------------------------
  144. GET /_search
  145. {
  146. "query": {
  147. "simple_query_string": {
  148. "query": "foo | bar + baz*",
  149. "flags": "OR|AND|PREFIX"
  150. }
  151. }
  152. }
  153. --------------------------------------------------
  154. [[supported-flags-values]]
  155. ====== Valid values
  156. The available flags are:
  157. `ALL` (Default)::
  158. Enables all optional operators.
  159. `AND`::
  160. Enables the `+` AND operator.
  161. `ESCAPE`::
  162. Enables `\` as an escape character.
  163. `FUZZY`::
  164. Enables the `~N` operator after a word, where `N` is an integer denoting the
  165. allowed edit distance for matching. See <<fuzziness>>.
  166. `NEAR`::
  167. Enables the `~N` operator, after a phrase where `N` is the maximum number of
  168. positions allowed between matching tokens. Synonymous to `SLOP`.
  169. `NONE`::
  170. Disables all operators.
  171. `NOT`::
  172. Enables the `-` NOT operator.
  173. `OR`::
  174. Enables the `\|` OR operator.
  175. `PHRASE`::
  176. Enables the `"` quotes operator used to search for phrases.
  177. `PRECEDENCE`::
  178. Enables the `(` and `)` operators to control operator precedence.
  179. `PREFIX`::
  180. Enables the `*` prefix operator.
  181. `SLOP`::
  182. Enables the `~N` operator, after a phrase where `N` is maximum number of
  183. positions allowed between matching tokens. Synonymous to `NEAR`.
  184. `WHITESPACE`::
  185. Enables whitespace as split characters.
  186. [[simple-query-string-boost]]
  187. ===== Wildcards and per-field boosts in the `fields` parameter
  188. Fields can be specified with wildcards, eg:
  189. [source,console]
  190. --------------------------------------------------
  191. GET /_search
  192. {
  193. "query": {
  194. "simple_query_string" : {
  195. "query": "Will Smith",
  196. "fields": [ "title", "*_name" ] <1>
  197. }
  198. }
  199. }
  200. --------------------------------------------------
  201. <1> Query the `title`, `first_name` and `last_name` fields.
  202. Individual fields can be boosted with the caret (`^`) notation:
  203. [source,console]
  204. --------------------------------------------------
  205. GET /_search
  206. {
  207. "query": {
  208. "simple_query_string" : {
  209. "query" : "this is a test",
  210. "fields" : [ "subject^3", "message" ] <1>
  211. }
  212. }
  213. }
  214. --------------------------------------------------
  215. <1> The `subject` field is three times as important as the `message` field.
  216. [[simple-query-string-synonyms]]
  217. ===== Synonyms
  218. The `simple_query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  219. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  220. For example, the following synonym: `"ny, new york" would produce:`
  221. `(ny OR ("new york"))`
  222. It is also possible to match multi terms synonyms with conjunctions instead:
  223. [source,console]
  224. --------------------------------------------------
  225. GET /_search
  226. {
  227. "query": {
  228. "simple_query_string" : {
  229. "query" : "ny city",
  230. "auto_generate_synonyms_phrase_query" : false
  231. }
  232. }
  233. }
  234. --------------------------------------------------
  235. The example above creates a boolean query:
  236. `(ny OR (new AND york)) city)`
  237. that matches documents with the term `ny` or the conjunction `new AND york`.
  238. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.