simple-query-string-query.asciidoc 9.2 KB

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