query-string-query.asciidoc 16 KB

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