text-query.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. [[query-dsl-text-query]]
  2. === Text Query
  3. `text` query has been deprecated (effectively renamed) to `match` query
  4. since `0.19.9`, please use it. `text` is still supported.
  5. A family of `text` queries that accept text, analyzes it, and constructs
  6. a query out of it. For example:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "text" : {
  11. "message" : "this is a test"
  12. }
  13. }
  14. --------------------------------------------------
  15. Note, even though the name is text, it also supports exact matching
  16. (`term` like) on numeric values and dates.
  17. Note, `message` is the name of a field, you can substitute the name of
  18. any field (including `_all`) instead.
  19. [float]
  20. [float]
  21. ==== Types of Text Queries
  22. [float]
  23. [float]
  24. ===== boolean
  25. The default `text` query is of type `boolean`. It means that the text
  26. provided is analyzed and the analysis process constructs a boolean query
  27. from the provided text. The `operator` flag can be set to `or` or `and`
  28. to control the boolean clauses (defaults to `or`).
  29. The `analyzer` can be set to control which analyzer will perform the
  30. analysis process on the text. It default to the field explicit mapping
  31. definition, or the default search analyzer.
  32. `fuzziness` can be set to a value (depending on the relevant type, for
  33. string types it should be a value between `0.0` and `1.0`) to constructs
  34. fuzzy queries for each term analyzed. The `prefix_length` and
  35. `max_expansions` can be set in this case to control the fuzzy process.
  36. Here is an example when providing additional parameters (note the slight
  37. change in structure, `message` is the field name):
  38. [source,js]
  39. --------------------------------------------------
  40. {
  41. "text" : {
  42. "message" : {
  43. "query" : "this is a test",
  44. "operator" : "and"
  45. }
  46. }
  47. }
  48. --------------------------------------------------
  49. [float]
  50. [float]
  51. ===== phrase
  52. The `text_phrase` query analyzes the text and creates a `phrase` query
  53. out of the analyzed text. For example:
  54. [source,js]
  55. --------------------------------------------------
  56. {
  57. "text_phrase" : {
  58. "message" : "this is a test"
  59. }
  60. }
  61. --------------------------------------------------
  62. Since `text_phrase` is only a `type` of a `text` query, it can also be
  63. used in the following manner:
  64. [source,js]
  65. --------------------------------------------------
  66. {
  67. "text" : {
  68. "message" : {
  69. "query" : "this is a test",
  70. "type" : "phrase"
  71. }
  72. }
  73. }
  74. --------------------------------------------------
  75. A phrase query maintains order of the terms up to a configurable `slop`
  76. (which defaults to 0).
  77. The `analyzer` can be set to control which analyzer will perform the
  78. analysis process on the text. It default to the field explicit mapping
  79. definition, or the default search analyzer, for example:
  80. [source,js]
  81. --------------------------------------------------
  82. {
  83. "text_phrase" : {
  84. "message" : {
  85. "query" : "this is a test",
  86. "analyzer" : "my_analyzer"
  87. }
  88. }
  89. }
  90. --------------------------------------------------
  91. [float]
  92. [float]
  93. ===== text_phrase_prefix
  94. The `text_phrase_prefix` is the same as `text_phrase`, expect it allows
  95. for prefix matches on the last term in the text. For example:
  96. [source,js]
  97. --------------------------------------------------
  98. {
  99. "text_phrase_prefix" : {
  100. "message" : "this is a test"
  101. }
  102. }
  103. --------------------------------------------------
  104. Or:
  105. [source,js]
  106. --------------------------------------------------
  107. {
  108. "text" : {
  109. "message" : {
  110. "query" : "this is a test",
  111. "type" : "phrase_prefix"
  112. }
  113. }
  114. }
  115. --------------------------------------------------
  116. It accepts the same parameters as the phrase type. In addition, it also
  117. accepts a `max_expansions` parameter that can control to how many
  118. prefixes the last term will be expanded. It is highly recommended to set
  119. it to an acceptable value to control the execution time of the query.
  120. For example:
  121. [source,js]
  122. --------------------------------------------------
  123. {
  124. "text_phrase_prefix" : {
  125. "message" : {
  126. "query" : "this is a test",
  127. "max_expansions" : 10
  128. }
  129. }
  130. }
  131. --------------------------------------------------
  132. [float]
  133. [float]
  134. ==== Comparison to query_string / field
  135. The text family of queries does not go through a "query parsing"
  136. process. It does not support field name prefixes, wildcard characters,
  137. or other "advance" features. For this reason, chances of it failing are
  138. very small / non existent, and it provides an excellent behavior when it
  139. comes to just analyze and run that text as a query behavior (which is
  140. usually what a text search box does). Also, the `phrase_prefix` can
  141. provide a great "as you type" behavior to automatically load search
  142. results.