query-string-query.asciidoc 16 KB

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