query-string-query.asciidoc 17 KB

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