simple-query-string-query.asciidoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. `all_fields`::
  63. deprecated:[6.0.0, set `fields` to `*` instead](Optional, boolean) If `true`,
  64. search all searchable fields in the index's field mapping.
  65. `analyze_wildcard`::
  66. (Optional, boolean) If `true`, the query attempts to analyze wildcard terms in
  67. the query string. Defaults to `false`.
  68. `analyzer`::
  69. (Optional, string) <<analysis,Analyzer>> used to convert text in the
  70. query string into tokens. Defaults to the
  71. <<specify-index-time-analyzer,index-time analyzer>> mapped for the
  72. `default_field`. If no analyzer is mapped, the index's default analyzer is used.
  73. `auto_generate_synonyms_phrase_query`::
  74. (Optional, boolean) If `true`, <<query-dsl-match-query-phrase,match phrase>>
  75. queries are automatically created for multi-term synonyms. Defaults to `true`.
  76. See <<simple-query-string-synonyms>> for an example.
  77. `flags`::
  78. (Optional, string) List of enabled operators for the
  79. <<simple-query-string-syntax,simple query string syntax>>. Defaults to `ALL`
  80. (all operators). See <<supported-flags>> for valid values.
  81. `fuzzy_max_expansions`::
  82. (Optional, integer) Maximum number of terms to which the query expands for fuzzy
  83. matching. Defaults to `50`.
  84. `fuzzy_prefix_length`::
  85. (Optional, integer) Number of beginning characters left unchanged for fuzzy
  86. matching. Defaults to `0`.
  87. `fuzzy_transpositions`::
  88. (Optional, boolean) If `true`, edits for fuzzy matching include
  89. transpositions of two adjacent characters (ab → ba). Defaults to `true`.
  90. `lenient`::
  91. (Optional, boolean) If `true`, format-based errors, such as providing a text
  92. value for a <<number,numeric>> field, are ignored. Defaults to `false`.
  93. `minimum_should_match`::
  94. (Optional, string) Minimum number of clauses that must match for a document to
  95. be returned. See the <<query-dsl-minimum-should-match, `minimum_should_match`
  96. parameter>> for valid values and more information.
  97. `quote_field_suffix`::
  98. +
  99. --
  100. (Optional, string) Suffix appended to quoted text in the query string.
  101. You can use this suffix to use a different analysis method for exact matches.
  102. See <<mixing-exact-search-with-stemming>>.
  103. --
  104. [[simple-query-string-query-notes]]
  105. ==== Notes
  106. [[simple-query-string-syntax]]
  107. ===== Simple query string syntax
  108. The `simple_query_string` query supports the following operators:
  109. * `+` signifies AND operation
  110. * `|` signifies OR operation
  111. * `-` negates a single token
  112. * `"` wraps a number of tokens to signify a phrase for searching
  113. * `*` at the end of a term signifies a prefix query
  114. * `(` and `)` signify precedence
  115. * `~N` after a word signifies edit distance (fuzziness)
  116. * `~N` after a phrase signifies slop amount
  117. To use one of these characters literally, escape it with a preceding backslash
  118. (`\`).
  119. The behavior of these operators may differ depending on the `default_operator`
  120. value. For example:
  121. [source,console]
  122. --------------------------------------------------
  123. GET /_search
  124. {
  125. "query": {
  126. "simple_query_string" : {
  127. "fields" : ["content"],
  128. "query" : "foo bar -baz"
  129. }
  130. }
  131. }
  132. --------------------------------------------------
  133. This search is intended to only return documents containing `foo` or `bar` that
  134. also do **not** contain `baz`. However because of a `default_operator` of `OR`,
  135. this search actually returns documents that contain `foo` or `bar` and any
  136. documents that don't contain `baz`. To return documents as intended, change the
  137. query string to `foo bar +-baz`.
  138. [[supported-flags]]
  139. ===== Limit operators
  140. You can use the `flags` parameter to limit the supported operators for the
  141. simple query string syntax.
  142. To explicitly enable only specific operators, use a `|` separator. For example,
  143. a `flags` value of `OR|AND|PREFIX` disables all operators except `OR`, `AND`,
  144. and `PREFIX`.
  145. [source,console]
  146. --------------------------------------------------
  147. GET /_search
  148. {
  149. "query": {
  150. "simple_query_string" : {
  151. "query" : "foo | bar + baz*",
  152. "flags" : "OR|AND|PREFIX"
  153. }
  154. }
  155. }
  156. --------------------------------------------------
  157. [[supported-flags-values]]
  158. ====== Valid values
  159. The available flags are:
  160. `ALL` (Default)::
  161. Enables all optional operators.
  162. `AND`::
  163. Enables the `+` AND operator.
  164. `ESCAPE`::
  165. Enables `\` as an escape character.
  166. `FUZZY`::
  167. Enables the `~N` operator after a word, where `N` is an integer denoting the
  168. allowed edit distance for matching. See <<fuzziness>>.
  169. `NEAR`::
  170. Enables the `~N` operator, after a phrase where `N` is the maximum number of
  171. positions allowed between matching tokens. Synonymous to `SLOP`.
  172. `NONE`::
  173. Disables all operators.
  174. `NOT`::
  175. Enables the `-` NOT operator.
  176. `OR`::
  177. Enables the `\|` OR operator.
  178. `PHRASE`::
  179. Enables the `"` quotes operator used to search for phrases.
  180. `PRECEDENCE`::
  181. Enables the `(` and `)` operators to control operator precedence.
  182. `PREFIX`::
  183. Enables the `*` prefix operator.
  184. `SLOP`::
  185. Enables the `~N` operator, after a phrase where `N` is maximum number of
  186. positions allowed between matching tokens. Synonymous to `NEAR`.
  187. `WHITESPACE`::
  188. Enables whitespace as split characters.
  189. [[simple-query-string-boost]]
  190. ===== Wildcards and per-field boosts in the `fields` parameter
  191. Fields can be specified with wildcards, eg:
  192. [source,console]
  193. --------------------------------------------------
  194. GET /_search
  195. {
  196. "query": {
  197. "simple_query_string" : {
  198. "query": "Will Smith",
  199. "fields": [ "title", "*_name" ] <1>
  200. }
  201. }
  202. }
  203. --------------------------------------------------
  204. <1> Query the `title`, `first_name` and `last_name` fields.
  205. Individual fields can be boosted with the caret (`^`) notation:
  206. [source,console]
  207. --------------------------------------------------
  208. GET /_search
  209. {
  210. "query": {
  211. "simple_query_string" : {
  212. "query" : "this is a test",
  213. "fields" : [ "subject^3", "message" ] <1>
  214. }
  215. }
  216. }
  217. --------------------------------------------------
  218. <1> The `subject` field is three times as important as the `message` field.
  219. [[simple-query-string-synonyms]]
  220. ===== Synonyms
  221. The `simple_query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  222. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  223. For example, the following synonym: `"ny, new york" would produce:`
  224. `(ny OR ("new york"))`
  225. It is also possible to match multi terms synonyms with conjunctions instead:
  226. [source,console]
  227. --------------------------------------------------
  228. GET /_search
  229. {
  230. "query": {
  231. "simple_query_string" : {
  232. "query" : "ny city",
  233. "auto_generate_synonyms_phrase_query" : false
  234. }
  235. }
  236. }
  237. --------------------------------------------------
  238. The example above creates a boolean query:
  239. `(ny OR (new AND york)) city)`
  240. that matches documents with the term `ny` or the conjunction `new AND york`.
  241. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.