query-string-query.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. [[query-dsl-query-string-query]]
  2. === Query String Query
  3. A query that uses a query parser in order to parse its content. Here is
  4. an example:
  5. [source,js]
  6. --------------------------------------------------
  7. GET /_search
  8. {
  9. "query": {
  10. "query_string" : {
  11. "default_field" : "content",
  12. "query" : "this AND that OR thus"
  13. }
  14. }
  15. }
  16. --------------------------------------------------
  17. // CONSOLE
  18. The `query_string` query parses the input and splits text around operators.
  19. Each textual part is analyzed independently of each other. For instance the following query:
  20. [source,js]
  21. --------------------------------------------------
  22. GET /_search
  23. {
  24. "query": {
  25. "query_string" : {
  26. "default_field" : "content",
  27. "query" : "(new york city) OR (big apple)" <1>
  28. }
  29. }
  30. }
  31. --------------------------------------------------
  32. // CONSOLE
  33. <1> will be split into `new york city` and `big apple` and each part is then
  34. analyzed independently by the analyzer configured for the field.
  35. WARNING: Whitespaces are not considered operators, this means that `new york city`
  36. will be passed "as is" to the analyzer configured for the field. If the field is a `keyword`
  37. field the analyzer will create a single term `new york city` and the query builder will
  38. use this term in the query. If you want to query each term separately you need to add explicit
  39. operators around the terms (e.g. `new AND york AND city`).
  40. When multiple fields are provided it is also possible to modify how the different
  41. field queries are combined inside each textual part using the `type` parameter.
  42. The possible modes are described <<multi-match-types, here>> and the default is `best_fields`.
  43. The `query_string` top level parameters include:
  44. [cols="<,<",options="header",]
  45. |=======================================================================
  46. |Parameter |Description
  47. |`query` |The actual query to be parsed. See <<query-string-syntax>>.
  48. |`default_field` |The default field for query terms if no prefix field is
  49. specified. Defaults to the `index.query.default_field` index settings, which in
  50. turn defaults to `*`. `*` extracts all fields in the mapping that are eligible
  51. to term queries and filters the metadata fields. All extracted fields are then
  52. combined to build a query when no prefix field is provided.
  53. WARNING: There is a limit on the number of fields that can be queried
  54. at once. It is defined by the `indices.query.bool.max_clause_count` <<search-settings>>
  55. which defaults to 1024.
  56. |`default_operator` |The default operator used if no explicit operator
  57. is specified. For example, with a default operator of `OR`, the query
  58. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  59. with default operator of `AND`, the same query is translated to
  60. `capital AND of AND Hungary`. The default value is `OR`.
  61. |`analyzer` |The analyzer name used to analyze the query string.
  62. |`quote_analyzer` |The name of the analyzer that is used to analyze
  63. quoted phrases in the query string. For those parts, it overrides other
  64. analyzers that are set using the `analyzer` parameter or the
  65. <<search-quote-analyzer,`search_quote_analyzer`>> setting.
  66. |`allow_leading_wildcard` |When set, `*` or `?` are allowed as the first
  67. character. Defaults to `true`.
  68. |`enable_position_increments` |Set to `true` to enable position
  69. increments in result queries. Defaults to `true`.
  70. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  71. expand to. Defaults to `50`
  72. |`fuzziness` |Set the fuzziness for fuzzy queries. Defaults
  73. to `AUTO`. See <<fuzziness>> for allowed settings.
  74. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  75. is `0`.
  76. |`fuzzy_transpositions` |Set to `false` to disable fuzzy transpositions (`ab` -> `ba`).
  77. Default is `true`.
  78. |`phrase_slop` |Sets the default slop for phrases. If zero, then exact
  79. phrase matches are required. Default value is `0`.
  80. |`boost` |Sets the boost value of the query. Defaults to `1.0`.
  81. |`analyze_wildcard` |By default, wildcards terms in a query string are
  82. not analyzed. By setting this value to `true`, a best effort will be
  83. made to analyze those as well.
  84. |`max_determinized_states` |Limit on how many automaton states regexp
  85. queries are allowed to create. This protects against too-difficult
  86. (e.g. exponentially hard) regexps. Defaults to 10000.
  87. |`minimum_should_match` |A value controlling how many "should" clauses
  88. in the resulting boolean query should match. It can be an absolute value
  89. (`2`), a percentage (`30%`) or a
  90. <<query-dsl-minimum-should-match,combination of
  91. both>>.
  92. |`lenient` |If set to `true` will cause format based failures (like
  93. providing text to a numeric field) to be ignored.
  94. |`time_zone` | Time Zone to be applied to any range query related to dates. See also
  95. http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html[JODA timezone].
  96. |`quote_field_suffix` | A suffix to append to fields for quoted parts of
  97. the query string. This allows to use a field that has a different analysis chain
  98. for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
  99. comprehensive example.
  100. |`auto_generate_synonyms_phrase_query` |Whether phrase queries should be automatically generated for multi terms synonyms.
  101. Defaults to `true`.
  102. |=======================================================================
  103. When a multi term query is being generated, one can control how it gets
  104. rewritten using the
  105. <<query-dsl-multi-term-rewrite,rewrite>>
  106. parameter.
  107. [float]
  108. ==== Default Field
  109. When not explicitly specifying the field to search on in the query
  110. string syntax, the `index.query.default_field` will be used to derive
  111. which field to search on. If the `index.query.default_field` is not specified,
  112. the `query_string` will automatically attempt to determine the existing fields in the index's
  113. mapping that are queryable, and perform the search on those fields.
  114. This will not include nested documents, use a nested query to search those documents.
  115. NOTE: For mappings with a large number of fields, searching across all queryable
  116. fields in the mapping could be expensive.
  117. [float]
  118. ==== Multi Field
  119. The `query_string` query can also run against multiple fields. Fields can be
  120. provided via the `fields` parameter (example below).
  121. The idea of running the `query_string` query against multiple fields is to
  122. expand each query term to an OR clause like this:
  123. field1:query_term OR field2:query_term | ...
  124. For example, the following query
  125. [source,js]
  126. --------------------------------------------------
  127. GET /_search
  128. {
  129. "query": {
  130. "query_string" : {
  131. "fields" : ["content", "name"],
  132. "query" : "this AND that"
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // CONSOLE
  138. matches the same words as
  139. [source,js]
  140. --------------------------------------------------
  141. GET /_search
  142. {
  143. "query": {
  144. "query_string": {
  145. "query": "(content:this OR name:this) AND (content:that OR name:that)"
  146. }
  147. }
  148. }
  149. --------------------------------------------------
  150. // CONSOLE
  151. Since several queries are generated from the individual search terms,
  152. combining them is automatically done using a `dis_max` query with a `tie_breaker`.
  153. For example (the `name` is boosted by 5 using `^5` notation):
  154. [source,js]
  155. --------------------------------------------------
  156. GET /_search
  157. {
  158. "query": {
  159. "query_string" : {
  160. "fields" : ["content", "name^5"],
  161. "query" : "this AND that OR thus",
  162. "tie_breaker" : 0
  163. }
  164. }
  165. }
  166. --------------------------------------------------
  167. // CONSOLE
  168. Simple wildcard can also be used to search "within" specific inner
  169. elements of the document. For example, if we have a `city` object with
  170. several fields (or inner object with fields) in it, we can automatically
  171. search on all "city" fields:
  172. [source,js]
  173. --------------------------------------------------
  174. GET /_search
  175. {
  176. "query": {
  177. "query_string" : {
  178. "fields" : ["city.*"],
  179. "query" : "this AND that OR thus"
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. // CONSOLE
  185. Another option is to provide the wildcard fields search in the query
  186. string itself (properly escaping the `*` sign), for example:
  187. `city.\*:something`:
  188. [source,js]
  189. --------------------------------------------------
  190. GET /_search
  191. {
  192. "query": {
  193. "query_string" : {
  194. "query" : "city.\\*:(this AND that OR thus)"
  195. }
  196. }
  197. }
  198. --------------------------------------------------
  199. // CONSOLE
  200. NOTE: Since `\` (backslash) is a special character in json strings, it needs to
  201. be escaped, hence the two backslashes in the above `query_string`.
  202. When running the `query_string` query against multiple fields, the
  203. following additional parameters are allowed:
  204. [cols="<,<",options="header",]
  205. |=======================================================================
  206. |Parameter |Description
  207. |`type` |How the fields should be combined to build the text query.
  208. See <<multi-match-types, types>> for a complete example.
  209. Defaults to `best_fields`
  210. |`tie_breaker` |The disjunction max tie breaker for multi fields.
  211. Defaults to `0`
  212. |=======================================================================
  213. The fields parameter can also include pattern based field names,
  214. allowing to automatically expand to the relevant fields (dynamically
  215. introduced fields included). For example:
  216. [source,js]
  217. --------------------------------------------------
  218. GET /_search
  219. {
  220. "query": {
  221. "query_string" : {
  222. "fields" : ["content", "name.*^5"],
  223. "query" : "this AND that OR thus"
  224. }
  225. }
  226. }
  227. --------------------------------------------------
  228. // CONSOLE
  229. [float]
  230. ==== Synonyms
  231. The `query_string` query supports multi-terms synonym expansion with the <<analysis-synonym-graph-tokenfilter,
  232. synonym_graph>> token filter. When this filter is used, the parser creates a phrase query for each multi-terms synonyms.
  233. For example, the following synonym: `ny, new york` would produce:
  234. `(ny OR ("new york"))`
  235. It is also possible to match multi terms synonyms with conjunctions instead:
  236. [source,js]
  237. --------------------------------------------------
  238. GET /_search
  239. {
  240. "query": {
  241. "query_string" : {
  242. "default_field": "title",
  243. "query" : "ny city",
  244. "auto_generate_synonyms_phrase_query" : false
  245. }
  246. }
  247. }
  248. --------------------------------------------------
  249. // CONSOLE
  250. The example above creates a boolean query:
  251. `(ny OR (new AND york)) city`
  252. that matches documents with the term `ny` or the conjunction `new AND york`.
  253. By default the parameter `auto_generate_synonyms_phrase_query` is set to `true`.
  254. [float]
  255. ==== Minimum should match
  256. The `query_string` splits the query around each operator to create a boolean
  257. query for the entire input. You can use `minimum_should_match` to control how
  258. many "should" clauses in the resulting query should match.
  259. [source,js]
  260. --------------------------------------------------
  261. GET /_search
  262. {
  263. "query": {
  264. "query_string": {
  265. "fields": [
  266. "title"
  267. ],
  268. "query": "this that thus",
  269. "minimum_should_match": 2
  270. }
  271. }
  272. }
  273. --------------------------------------------------
  274. // CONSOLE
  275. The example above creates a boolean query:
  276. `(title:this title:that title:thus)~2`
  277. that matches documents with at least two of the terms `this`, `that` or `thus`
  278. in the single field `title`.
  279. [float]
  280. ===== Multi Field
  281. [source,js]
  282. --------------------------------------------------
  283. GET /_search
  284. {
  285. "query": {
  286. "query_string": {
  287. "fields": [
  288. "title",
  289. "content"
  290. ],
  291. "query": "this that thus",
  292. "minimum_should_match": 2
  293. }
  294. }
  295. }
  296. --------------------------------------------------
  297. // CONSOLE
  298. The example above creates a boolean query:
  299. `((content:this content:that content:thus) | (title:this title:that title:thus))`
  300. that matches documents with the disjunction max over the fields `title` and
  301. `content`. Here the `minimum_should_match` parameter can't be applied.
  302. [source,js]
  303. --------------------------------------------------
  304. GET /_search
  305. {
  306. "query": {
  307. "query_string": {
  308. "fields": [
  309. "title",
  310. "content"
  311. ],
  312. "query": "this OR that OR thus",
  313. "minimum_should_match": 2
  314. }
  315. }
  316. }
  317. --------------------------------------------------
  318. // CONSOLE
  319. Adding explicit operators forces each term to be considered as a separate clause.
  320. The example above creates a boolean query:
  321. `((content:this | title:this) (content:that | title:that) (content:thus | title:thus))~2`
  322. that matches documents with at least two of the three "should" clauses, each of
  323. them made of the disjunction max over the fields for each term.
  324. [float]
  325. ===== Cross Field
  326. [source,js]
  327. --------------------------------------------------
  328. GET /_search
  329. {
  330. "query": {
  331. "query_string": {
  332. "fields": [
  333. "title",
  334. "content"
  335. ],
  336. "query": "this OR that OR thus",
  337. "type": "cross_fields",
  338. "minimum_should_match": 2
  339. }
  340. }
  341. }
  342. --------------------------------------------------
  343. // CONSOLE
  344. The `cross_fields` value in the `type` field indicates that fields that have the
  345. same analyzer should be grouped together when the input is analyzed.
  346. The example above creates a boolean query:
  347. `(blended(terms:[field2:this, field1:this]) blended(terms:[field2:that, field1:that]) blended(terms:[field2:thus, field1:thus]))~2`
  348. that matches documents with at least two of the three per-term blended queries.
  349. include::query-string-syntax.asciidoc[]