query-string-query.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. [[query-dsl-query-string-query]]
  2. === Query string query
  3. ++++
  4. <titleabbrev>Query string</titleabbrev>
  5. ++++
  6. Returns documents based on a provided query string, using a parser with a strict
  7. syntax.
  8. This query uses a <<query-string-syntax,syntax>> to parse and split the provided
  9. query string based on operators, such as `AND` or `NOT`. The query
  10. then <<analysis,analyzes>> each split text independently before returning
  11. matching documents.
  12. You can use the `query_string` query to create a complex search that includes
  13. wildcard characters, searches across multiple fields, and more. While versatile,
  14. the query is strict and returns an error if the query string includes any
  15. invalid syntax.
  16. [WARNING]
  17. ====
  18. Because it returns an error for any invalid syntax, we don't recommend using
  19. the `query_string` query for search boxes.
  20. If you don't need to support a query syntax, consider using the
  21. <<query-dsl-match-query, `match`>> query. If you need the features of a query
  22. syntax, use the <<query-dsl-simple-query-string-query,`simple_query_string`>>
  23. query, which is less strict.
  24. ====
  25. [[query-string-query-ex-request]]
  26. ==== Example request
  27. When running the following search, the `query_string` query splits `(new york
  28. city) OR (big apple)` into two parts: `new york city` and `big apple`. The
  29. `content` field's analyzer then independently converts each part into tokens
  30. before returning matching documents. Because the query syntax does not use
  31. whitespace as an operator, `new york city` is passed as-is to the analyzer.
  32. [source,console]
  33. --------------------------------------------------
  34. GET /_search
  35. {
  36. "query": {
  37. "query_string" : {
  38. "query" : "(new york city) OR (big apple)",
  39. "default_field" : "content"
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. [[query-string-top-level-params]]
  45. ==== Top-level parameters for `query_string`
  46. `query`::
  47. (Required, string) Query string you wish to parse and use for search. See
  48. <<query-string-syntax>>.
  49. `default_field`::
  50. +
  51. --
  52. (Optional, string) Default field you wish to search if no field is provided in
  53. the query string.
  54. Defaults to the `index.query.default_field` index setting, which has a default
  55. value of `*`. The `*` value extracts all fields that are eligible to term
  56. queries and filters the metadata fields. All extracted fields are then combined
  57. to build a query if no `prefix` is specified.
  58. WARNING: There is a limit on the number of fields that can be queried at once.
  59. It is defined by the `indices.query.bool.max_clause_count`
  60. <<search-settings,search setting>>, which defaults to 1024.
  61. --
  62. `allow_leading_wildcard`::
  63. (Optional, boolean) If `true`, the wildcard characters `*` and `?` are allowed
  64. as the first character of the query string. Defaults to `true`.
  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 <<query-string-synonyms>> for an example.
  77. `boost`::
  78. +
  79. --
  80. (Optional, float) Floating point number used to decrease or increase the
  81. <<relevance-scores,relevance scores>> of the query. Defaults to `1.0`.
  82. Boost values are relative to the default value of `1.0`. A boost value between
  83. `0` and `1.0` decreases the relevance score. A value greater than `1.0`
  84. increases the relevance score.
  85. --
  86. `default_operator`::
  87. +
  88. --
  89. (Optional, string) Default boolean logic used to interpret text in the query
  90. string if no operators are specified. Valid values are:
  91. `OR` (Default)::
  92. For example, a query string of `capital of Hungary` is interpreted as `capital
  93. OR of OR Hungary`.
  94. `AND`::
  95. For example, a query string of `capital of Hungary` is interpreted as `capital
  96. AND of AND Hungary`.
  97. --
  98. `enable_position_increments`::
  99. (Optional, boolean) If `true`, enable position increments in queries constructed
  100. from a `query_string` search. Defaults to `true`.
  101. `fields`::
  102. +
  103. --
  104. (Optional, array of strings) Array of fields you wish to search.
  105. You can use this parameter query to search across multiple fields. See
  106. <<query-string-multi-field>>.
  107. --
  108. `fuzziness`::
  109. (Optional, string) Maximum edit distance allowed for matching. See <<fuzziness>>
  110. for valid values and more information.
  111. `fuzzy_max_expansions`::
  112. (Optional, integer) Maximum number of terms to which the query expands for fuzzy
  113. matching. Defaults to `50`.
  114. `fuzzy_prefix_length`::
  115. (Optional, integer) Number of beginning characters left unchanged for fuzzy
  116. matching. Defaults to `0`.
  117. `fuzzy_transpositions`::
  118. (Optional, boolean) If `true`, edits for fuzzy matching include
  119. transpositions of two adjacent characters (ab → ba). Defaults to `true`.
  120. `lenient`::
  121. (Optional, boolean) If `true`, format-based errors, such as providing a text
  122. value for a <<number,numeric>> field, are ignored. Defaults to `false`.
  123. `max_determinized_states`::
  124. +
  125. --
  126. (Optional, integer) Maximum number of
  127. https://en.wikipedia.org/wiki/Deterministic_finite_automaton[automaton states]
  128. required for the query. Default is `10000`.
  129. {es} uses https://lucene.apache.org/core/[Apache Lucene] internally to parse
  130. regular expressions. Lucene converts each regular expression to a finite
  131. automaton containing a number of determinized states.
  132. You can use this parameter to prevent that conversion from unintentionally
  133. consuming too many resources. You may need to increase this limit to run complex
  134. regular expressions.
  135. --
  136. `minimum_should_match`::
  137. (Optional, string) Minimum number of clauses that must match for a document to
  138. be returned. See the <<query-dsl-minimum-should-match, `minimum_should_match`
  139. parameter>> for valid values and more information. See
  140. <<query-string-min-should-match>> for an example.
  141. `quote_analyzer`::
  142. +
  143. --
  144. (Optional, string) <<analysis,Analyzer>> used to convert quoted text in the
  145. query string into tokens. Defaults to the
  146. <<search-quote-analyzer,`search_quote_analyzer`>> mapped for the
  147. `default_field`.
  148. For quoted text, this parameter overrides the analyzer specified in the
  149. `analyzer` parameter.
  150. --
  151. `phrase_slop`::
  152. (Optional, integer) Maximum number of positions allowed between matching tokens
  153. for phrases. Defaults to `0`. If `0`, exact phrase matches are required.
  154. Transposed terms have a slop of `2`.
  155. `quote_field_suffix`::
  156. +
  157. --
  158. (Optional, string) Suffix appended to quoted text in the query string.
  159. You can use this suffix to use a different analysis method for exact matches.
  160. See <<mixing-exact-search-with-stemming>>.
  161. --
  162. `rewrite`::
  163. (Optional, string) Method used to rewrite the query. For valid values and more
  164. information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
  165. `time_zone`::
  166. +
  167. --
  168. (Optional, string)
  169. https://en.wikipedia.org/wiki/List_of_UTC_time_offsets[Coordinated Universal
  170. Time (UTC) offset] or
  171. https://en.wikipedia.org/wiki/List_of_tz_database_time_zones[IANA time zone]
  172. used to convert `date` values in the query string to UTC.
  173. Valid values are ISO 8601 UTC offsets, such as `+01:00` or -`08:00`, and IANA
  174. time zone IDs, such as `America/Los_Angeles`.
  175. [NOTE]
  176. ====
  177. The `time_zone` parameter does **not** affect the <<date-math,date math>> value
  178. of `now`. `now` is always the current system time in UTC. However, the
  179. `time_zone` parameter does convert dates calculated using `now` and
  180. <<date-math,date math rounding>>. For example, the `time_zone` parameter will
  181. convert a value of `now/d`.
  182. ====
  183. --
  184. [[query-string-query-notes]]
  185. ==== Notes
  186. include::query-string-syntax.asciidoc[]
  187. [[query-string-nested]]
  188. ====== Avoid using the `query_string` query for nested documents
  189. `query_string` searches do not return <<nested,nested>> documents. To search
  190. nested documents, use the <<query-dsl-nested-query, `nested` query>>.
  191. [[query-string-multi-field]]
  192. ====== Search multiple fields
  193. You can use the `fields` parameter to perform a `query_string` search across
  194. multiple fields.
  195. The idea of running the `query_string` query against multiple fields is to
  196. expand each query term to an OR clause like this:
  197. ```
  198. field1:query_term OR field2:query_term | ...
  199. ```
  200. For example, the following query
  201. [source,console]
  202. --------------------------------------------------
  203. GET /_search
  204. {
  205. "query": {
  206. "query_string" : {
  207. "fields" : ["content", "name"],
  208. "query" : "this AND that"
  209. }
  210. }
  211. }
  212. --------------------------------------------------
  213. matches the same words as
  214. [source,console]
  215. --------------------------------------------------
  216. GET /_search
  217. {
  218. "query": {
  219. "query_string": {
  220. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  221. }
  222. }
  223. }
  224. --------------------------------------------------
  225. Since several queries are generated from the individual search terms,
  226. combining them is automatically done using a `dis_max` query with a `tie_breaker`.
  227. For example (the `name` is boosted by 5 using `^5` notation):
  228. [source,console]
  229. --------------------------------------------------
  230. GET /_search
  231. {
  232. "query": {
  233. "query_string" : {
  234. "fields" : ["content", "name^5"],
  235. "query" : "this AND that OR thus",
  236. "tie_breaker" : 0
  237. }
  238. }
  239. }
  240. --------------------------------------------------
  241. Simple wildcard can also be used to search "within" specific inner
  242. elements of the document. For example, if we have a `city` object with
  243. several fields (or inner object with fields) in it, we can automatically
  244. search on all "city" fields:
  245. [source,console]
  246. --------------------------------------------------
  247. GET /_search
  248. {
  249. "query": {
  250. "query_string" : {
  251. "fields" : ["city.*"],
  252. "query" : "this AND that OR thus"
  253. }
  254. }
  255. }
  256. --------------------------------------------------
  257. Another option is to provide the wildcard fields search in the query
  258. string itself (properly escaping the `*` sign), for example:
  259. `city.\*:something`:
  260. [source,console]
  261. --------------------------------------------------
  262. GET /_search
  263. {
  264. "query": {
  265. "query_string" : {
  266. "query" : "city.\\*:(this AND that OR thus)"
  267. }
  268. }
  269. }
  270. --------------------------------------------------
  271. NOTE: Since `\` (backslash) is a special character in json strings, it needs to
  272. be escaped, hence the two backslashes in the above `query_string`.
  273. The fields parameter can also include pattern based field names,
  274. allowing to automatically expand to the relevant fields (dynamically
  275. introduced fields included). For example:
  276. [source,console]
  277. --------------------------------------------------
  278. GET /_search
  279. {
  280. "query": {
  281. "query_string" : {
  282. "fields" : ["content", "name.*^5"],
  283. "query" : "this AND that OR thus"
  284. }
  285. }
  286. }
  287. --------------------------------------------------
  288. [[query-string-multi-field-parms]]
  289. ====== Additional parameters for multiple field searches
  290. When running the `query_string` query against multiple fields, the
  291. following additional parameters are supported.
  292. `type`::
  293. +
  294. --
  295. (Optional, string) Determines how the query matches and scores documents. Valid
  296. values are:
  297. `best_fields` (Default)::
  298. Finds documents which match any field and uses the highest
  299. <<relevance-scores,`_score`>> from any matching field. See
  300. <<type-best-fields>>.
  301. `bool_prefix`::
  302. Creates a `match_bool_prefix` query on each field and combines the `_score` from
  303. each field. See <<type-bool-prefix>>.
  304. `cross_fields`::
  305. Treats fields with the same `analyzer` as though they were one big field. Looks
  306. for each word in **any** field. See <<type-cross-fields>>.
  307. `most_fields`::
  308. Finds documents which match any field and combines the `_score` from each field.
  309. See <<type-most-fields>>.
  310. `phrase`::
  311. Runs a `match_phrase` query on each field and uses the `_score` from the best
  312. field. See <<type-phrase>>.
  313. `phrase_prefix`::
  314. Runs a `match_phrase_prefix` query on each field and uses the `_score` from the
  315. best field. See <<type-phrase>>.
  316. NOTE:
  317. Additional top-level `multi_match` parameters may be available based on the
  318. <<multi-match-types,`type`>> value.
  319. --
  320. [[query-string-synonyms]]
  321. ===== Synonyms and the `query_string` query
  322. The `query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  323. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  324. For example, the following synonym: `ny, new york` would produce:
  325. `(ny OR ("new york"))`
  326. It is also possible to match multi terms synonyms with conjunctions instead:
  327. [source,console]
  328. --------------------------------------------------
  329. GET /_search
  330. {
  331. "query": {
  332. "query_string" : {
  333. "default_field": "title",
  334. "query" : "ny city",
  335. "auto_generate_synonyms_phrase_query" : false
  336. }
  337. }
  338. }
  339. --------------------------------------------------
  340. The example above creates a boolean query:
  341. `(ny OR (new AND york)) city`
  342. that matches documents with the term `ny` or the conjunction `new AND york`.
  343. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.
  344. [[query-string-min-should-match]]
  345. ===== How `minimum_should_match` works
  346. The `query_string` splits the query around each operator to create a boolean
  347. query for the entire input. You can use `minimum_should_match` to control how
  348. many "should" clauses in the resulting query should match.
  349. [source,console]
  350. --------------------------------------------------
  351. GET /_search
  352. {
  353. "query": {
  354. "query_string": {
  355. "fields": [
  356. "title"
  357. ],
  358. "query": "this that thus",
  359. "minimum_should_match": 2
  360. }
  361. }
  362. }
  363. --------------------------------------------------
  364. The example above creates a boolean query:
  365. `(title:this title:that title:thus)~2`
  366. that matches documents with at least two of the terms `this`, `that` or `thus`
  367. in the single field `title`.
  368. [[query-string-min-should-match-multi]]
  369. ===== How `minimum_should_match` works for multiple fields
  370. [source,console]
  371. --------------------------------------------------
  372. GET /_search
  373. {
  374. "query": {
  375. "query_string": {
  376. "fields": [
  377. "title",
  378. "content"
  379. ],
  380. "query": "this that thus",
  381. "minimum_should_match": 2
  382. }
  383. }
  384. }
  385. --------------------------------------------------
  386. The example above creates a boolean query:
  387. `((content:this content:that content:thus) | (title:this title:that title:thus))`
  388. that matches documents with the disjunction max over the fields `title` and
  389. `content`. Here the `minimum_should_match` parameter can't be applied.
  390. [source,console]
  391. --------------------------------------------------
  392. GET /_search
  393. {
  394. "query": {
  395. "query_string": {
  396. "fields": [
  397. "title",
  398. "content"
  399. ],
  400. "query": "this OR that OR thus",
  401. "minimum_should_match": 2
  402. }
  403. }
  404. }
  405. --------------------------------------------------
  406. Adding explicit operators forces each term to be considered as a separate clause.
  407. The example above creates a boolean query:
  408. `((content:this | title:this) (content:that | title:that) (content:thus | title:thus))~2`
  409. that matches documents with at least two of the three "should" clauses, each of
  410. them made of the disjunction max over the fields for each term.
  411. [[query-string-min-should-match-cross]]
  412. ===== How `minimum_should_match` works for cross-field searches
  413. A `cross_fields` value in the `type` field indicates fields with the same
  414. analyzer are grouped together when the input is analyzed.
  415. [source,console]
  416. --------------------------------------------------
  417. GET /_search
  418. {
  419. "query": {
  420. "query_string": {
  421. "fields": [
  422. "title",
  423. "content"
  424. ],
  425. "query": "this OR that OR thus",
  426. "type": "cross_fields",
  427. "minimum_should_match": 2
  428. }
  429. }
  430. }
  431. --------------------------------------------------
  432. The example above creates a boolean query:
  433. `(blended(terms:[field2:this, field1:this]) blended(terms:[field2:that, field1:that]) blended(terms:[field2:thus, field1:thus]))~2`
  434. that matches documents with at least two of the three per-term blended queries.