query-string-query.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. {
  8. "query_string" : {
  9. "default_field" : "content",
  10. "query" : "this AND that OR thus"
  11. }
  12. }
  13. --------------------------------------------------
  14. The `query_string` top level parameters include:
  15. [cols="<,<",options="header",]
  16. |=======================================================================
  17. |Parameter |Description
  18. |`query` |The actual query to be parsed.
  19. |`default_field` |The default field for query terms if no prefix field
  20. is specified. Defaults to the `index.query.default_field` index
  21. settings, which in turn defaults to `_all`.
  22. |`default_operator` |The default operator used if no explicit operator
  23. is specified. For example, with a default operator of `OR`, the query
  24. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  25. with default operator of `AND`, the same query is translated to
  26. `capital AND of AND Hungary`. The default value is `OR`.
  27. |`analyzer` |The analyzer name used to analyze the query string.
  28. |`allow_leading_wildcard` |When set, `*` or `?` are allowed as the first
  29. character. Defaults to `true`.
  30. |`lowercase_expanded_terms` |Whether terms of wildcard, prefix, fuzzy,
  31. and range queries are to be automatically lower-cased or not (since they
  32. are not analyzed). Default it `true`.
  33. |`enable_position_increments` |Set to `true` to enable position
  34. increments in result queries. Defaults to `true`.
  35. |`fuzzy_max_expansions` |Controls the number of terms fuzzy queries will
  36. expand to. Defaults to `50`
  37. |`fuzzy_min_sim` |Set the minimum similarity for fuzzy queries. Defaults
  38. to `0.5`
  39. |`fuzzy_prefix_length` |Set the prefix length for fuzzy queries. Default
  40. is `0`.
  41. |`phrase_slop` |Sets the default slop for phrases. If zero, then exact
  42. phrase matches are required. Default value is `0`.
  43. |`boost` |Sets the boost value of the query. Defaults to `1.0`.
  44. |`analyze_wildcard` |By default, wildcards terms in a query string are
  45. not analyzed. By setting this value to `true`, a best effort will be
  46. made to analyze those as well.
  47. |`auto_generate_phrase_queries` |Default to `false`.
  48. |`minimum_should_match` |A value controlling how many "should" clauses
  49. in the resulting boolean query should match. It can be an absolute value
  50. (`2`), a percentage (`30%`) or a
  51. <<query-dsl-minimum-should-match,combination of
  52. both>>.
  53. |`lenient` |If set to `true` will cause format based failures (like
  54. providing text to a numeric field) to be ignored.
  55. |=======================================================================
  56. When a multi term query is being generated, one can control how it gets
  57. rewritten using the
  58. <<query-dsl-multi-term-rewrite,rewrite>>
  59. parameter.
  60. [float]
  61. ==== Default Field
  62. When not explicitly specifying the field to search on in the query
  63. string syntax, the `index.query.default_field` will be used to derive
  64. which field to search on. It defaults to `_all` field.
  65. So, if `_all` field is disabled, it might make sense to change it to set
  66. a different default field.
  67. [float]
  68. ==== Multi Field
  69. The `query_string` query can also run against multiple fields. The idea
  70. of running the `query_string` query against multiple fields is by
  71. internally creating several queries for the same query string, each with
  72. `default_field` that match the fields provided. Since several queries
  73. are generated, combining them can be automatically done either using a
  74. `dis_max` query or a simple `bool` query. For example (the `name` is
  75. boosted by 5 using `^5` notation):
  76. [source,js]
  77. --------------------------------------------------
  78. {
  79. "query_string" : {
  80. "fields" : ["content", "name^5"],
  81. "query" : "this AND that OR thus",
  82. "use_dis_max" : true
  83. }
  84. }
  85. --------------------------------------------------
  86. Simple wildcard can also be used to search "within" specific inner
  87. elements of the document. For example, if we have a `city` object with
  88. several fields (or inner object with fields) in it, we can automatically
  89. search on all "city" fields:
  90. [source,js]
  91. --------------------------------------------------
  92. {
  93. "query_string" : {
  94. "fields" : ["city.*"],
  95. "query" : "this AND that OR thus",
  96. "use_dis_max" : true
  97. }
  98. }
  99. --------------------------------------------------
  100. Another option is to provide the wildcard fields search in the query
  101. string itself (properly escaping the `*` sign), for example:
  102. `city.\*:something`.
  103. When running the `query_string` query against multiple fields, the
  104. following additional parameters are allowed:
  105. [cols="<,<",options="header",]
  106. |=======================================================================
  107. |Parameter |Description
  108. |`use_dis_max` |Should the queries be combined using `dis_max` (set it
  109. to `true`), or a `bool` query (set it to `false`). Defaults to `true`.
  110. |`tie_breaker` |When using `dis_max`, the disjunction max tie breaker.
  111. Defaults to `0`.
  112. |=======================================================================
  113. The fields parameter can also include pattern based field names,
  114. allowing to automatically expand to the relevant fields (dynamically
  115. introduced fields included). For example:
  116. [source,js]
  117. --------------------------------------------------
  118. {
  119. "query_string" : {
  120. "fields" : ["content", "name.*^5"],
  121. "query" : "this AND that OR thus",
  122. "use_dis_max" : true
  123. }
  124. }
  125. --------------------------------------------------
  126. [[Syntax_Extension]]
  127. [float]
  128. ==== Syntax Extension
  129. There are several syntax extensions to the Lucene query language.
  130. [float]
  131. ===== missing / exists
  132. The `_exists_` and `_missing_` syntax allows to control docs that have
  133. fields that exists within them (have a value) and missing. The syntax
  134. is: `_exists_:field1`, `_missing_:field` and can be used anywhere a
  135. query string is used.