query-string-query.asciidoc 17 KB

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