simple-query-string-query.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. [[query-dsl-simple-query-string-query]]
  2. === Simple Query String Query
  3. A query that uses the SimpleQueryParser to parse its context. Unlike the
  4. regular `query_string` query, the `simple_query_string` query will never
  5. throw an exception, and discards invalid parts of the query. Here is
  6. an example:
  7. [source,js]
  8. --------------------------------------------------
  9. GET /_search
  10. {
  11. "query": {
  12. "simple_query_string" : {
  13. "query": "\"fried eggs\" +(eggplant | potato) -frittata",
  14. "analyzer": "snowball",
  15. "fields": ["body^5","_all"],
  16. "default_operator": "and"
  17. }
  18. }
  19. }
  20. --------------------------------------------------
  21. // CONSOLE
  22. The `simple_query_string` top level parameters include:
  23. [cols="<,<",options="header",]
  24. |=======================================================================
  25. |Parameter |Description
  26. |`query` |The actual query to be parsed. See below for syntax.
  27. |`fields` |The fields to perform the parsed query against. Defaults to the
  28. `index.query.default_field` index settings, which in turn defaults to `_all`.
  29. |`default_operator` |The default operator used if no explicit operator
  30. is specified. For example, with a default operator of `OR`, the query
  31. `capital of Hungary` is translated to `capital OR of OR Hungary`, and
  32. with default operator of `AND`, the same query is translated to
  33. `capital AND of AND Hungary`. The default value is `OR`.
  34. |`analyzer` |The analyzer used to analyze each term of the query when
  35. creating composite queries.
  36. |`flags` |Flags specifying which features of the `simple_query_string` to
  37. enable. Defaults to `ALL`.
  38. |`analyze_wildcard` | Whether terms of prefix queries should be automatically
  39. analyzed or not. If `true` a best effort will be made to analyze the prefix. However,
  40. some analyzers will be not able to provide a meaningful results
  41. based just on the prefix of a term. Defaults to `false`.
  42. |`lenient` | If set to `true` will cause format based failures
  43. (like providing text to a numeric field) to be ignored.
  44. |`minimum_should_match` | The minimum number of clauses that must match for a
  45. document to be returned. See the
  46. <<query-dsl-minimum-should-match,`minimum_should_match`>> documentation for the
  47. full list of options.
  48. |`quote_field_suffix` | A suffix to append to fields for quoted parts of
  49. the query string. This allows to use a field that has a different analysis chain
  50. for exact matching. Look <<mixing-exact-search-with-stemming,here>> for a
  51. comprehensive example.
  52. |`all_fields` | Perform the query on all fields detected in the mapping that can
  53. be queried. Will be used by default when the `_all` field is disabled and no
  54. `default_field` is specified index settings, and no `fields` are specified.
  55. |=======================================================================
  56. [float]
  57. ===== Simple Query String Syntax
  58. The `simple_query_string` supports the following special characters:
  59. * `+` signifies AND operation
  60. * `|` signifies OR operation
  61. * `-` negates a single token
  62. * `"` wraps a number of tokens to signify a phrase for searching
  63. * `*` at the end of a term signifies a prefix query
  64. * `(` and `)` signify precedence
  65. * `~N` after a word signifies edit distance (fuzziness)
  66. * `~N` after a phrase signifies slop amount
  67. In order to search for any of these special characters, they will need to
  68. be escaped with `\`.
  69. Be aware that this syntax may have a different behavior depending on the
  70. `default_operator` value. For example, consider the following query:
  71. [source,js]
  72. --------------------------------------------------
  73. GET /_search
  74. {
  75. "query": {
  76. "simple_query_string" : {
  77. "fields" : ["content"],
  78. "query" : "foo bar -baz"
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. // CONSOLE
  84. You may expect that documents containing only "foo" or "bar" will be returned,
  85. as long as they do not contain "baz", however, due to the `default_operator`
  86. being OR, this really means "match documents that contain "foo" or documents
  87. that contain "bar", or documents that don't contain "baz". If this is unintended
  88. then the query can be switched to `"foo bar +-baz"` which will not return
  89. documents that contain "baz".
  90. [float]
  91. ==== Default Field
  92. When not explicitly specifying the field to search on in the query
  93. string syntax, the `index.query.default_field` will be used to derive
  94. which field to search on. It defaults to `_all` field.
  95. If the `_all` field is disabled and no `fields` are specified in the request`,
  96. the `simple_query_string` query will automatically attempt to determine the
  97. existing fields in the index's mapping that are queryable, and perform the
  98. search on those fields.
  99. [float]
  100. ==== Multi Field
  101. The fields parameter can also include pattern based field names,
  102. allowing to automatically expand to the relevant fields (dynamically
  103. introduced fields included). For example:
  104. [source,js]
  105. --------------------------------------------------
  106. GET /_search
  107. {
  108. "query": {
  109. "simple_query_string" : {
  110. "fields" : ["content", "name.*^5"],
  111. "query" : "foo bar baz"
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. // CONSOLE
  117. [float]
  118. ==== Flags
  119. `simple_query_string` support multiple flags to specify which parsing features
  120. should be enabled. It is specified as a `|`-delimited string with the
  121. `flags` parameter:
  122. [source,js]
  123. --------------------------------------------------
  124. GET /_search
  125. {
  126. "query": {
  127. "simple_query_string" : {
  128. "query" : "foo | bar + baz*",
  129. "flags" : "OR|AND|PREFIX"
  130. }
  131. }
  132. }
  133. --------------------------------------------------
  134. // CONSOLE
  135. The available flags are: `ALL`, `NONE`, `AND`, `OR`, `NOT`, `PREFIX`, `PHRASE`,
  136. `PRECEDENCE`, `ESCAPE`, `WHITESPACE`, `FUZZY`, `NEAR`, and `SLOP`.