search.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[sql-functions-search]]
  4. === Full-Text Search Functions
  5. Search functions should be used when performing full-text search, namely
  6. when the `MATCH` or `QUERY` predicates are being used.
  7. Outside a, so-called, search context, these functions will return default values
  8. such as `0` or `NULL`.
  9. [[sql-functions-search-match]]
  10. ==== `MATCH`
  11. .Synopsis:
  12. [source, sql]
  13. --------------------------------------------------
  14. MATCH(
  15. field_exp, <1>
  16. constant_exp <2>
  17. [, options]) <3>
  18. --------------------------------------------------
  19. *Input*:
  20. <1> field(s) to match
  21. <2> matching text
  22. <3> additional parameters; optional
  23. .Description:
  24. A full-text search option, in the form of a predicate, available in {es-sql} that gives the user control over powerful <<query-dsl-match-query,match>>
  25. and <<query-dsl-multi-match-query,multi_match>> {es} queries.
  26. The first parameter is the field or fields to match against. In case it receives one value only, {es-sql} will use a `match` query to perform the search:
  27. [source, sql]
  28. ----
  29. include-tagged::{sql-specs}/docs/docs.csv-spec[simpleMatch]
  30. ----
  31. However, it can also receive a list of fields and their corresponding optional `boost` value. In this case, {es-sql} will use a
  32. `multi_match` query to match the documents:
  33. [source, sql]
  34. ----
  35. include-tagged::{sql-specs}/docs/docs.csv-spec[multiFieldsMatch]
  36. ----
  37. NOTE: The `multi_match` query in {es} has the option of <<query-dsl-multi-match-query,per-field boosting>> that gives preferential weight
  38. (in terms of scoring) to fields being searched in, using the `^` character. In the example above, the `name` field has a greater weight in
  39. the final score than the `author` field when searching for `frank dune` text in both of them.
  40. Both options above can be used in combination with the optional third parameter of the `MATCH()` predicate, where one can specify
  41. additional configuration parameters (separated by semicolon `;`) for either `match` or `multi_match` queries. For example:
  42. [source, sql]
  43. ----
  44. include-tagged::{sql-specs}/docs/docs.csv-spec[optionalParamsForMatch]
  45. ----
  46. NOTE: The allowed optional parameters for a single-field `MATCH()` variant (for the `match` {es} query) are: `analyzer`, `auto_generate_synonyms_phrase_query`,
  47. `lenient`, `fuzziness`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
  48. `max_expansions`, `prefix_length`.
  49. NOTE: The allowed optional parameters for a multi-field `MATCH()` variant (for the `multi_match` {es} query) are: `analyzer`, `auto_generate_synonyms_phrase_query`,
  50. `lenient`, `fuzziness`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
  51. `max_expansions`, `prefix_length`, `slop`, `tie_breaker`, `type`.
  52. [[sql-functions-search-query]]
  53. ==== `QUERY`
  54. .Synopsis:
  55. [source, sql]
  56. --------------------------------------------------
  57. QUERY(
  58. constant_exp <1>
  59. [, options]) <2>
  60. --------------------------------------------------
  61. *Input*:
  62. <1> query text
  63. <2> additional parameters; optional
  64. .Description:
  65. Just like `MATCH`, `QUERY` is a full-text search predicate that gives the user control over the <<query-dsl-query-string-query,query_string>> query in {es}.
  66. The first parameter is basically the input that will be passed as is to the `query_string` query, which means that anything that `query_string`
  67. accepts in its `query` field can be used here as well:
  68. [source, sql]
  69. ----
  70. include-tagged::{sql-specs}/docs/docs.csv-spec[simpleQueryQuery]
  71. ----
  72. A more advanced example, showing more of the features that `query_string` supports, of course possible with {es-sql}:
  73. [source, sql]
  74. ----
  75. include-tagged::{sql-specs}/docs/docs.csv-spec[advancedQueryQuery]
  76. ----
  77. The query above uses the `_exists_` query to select documents that have values in the `author` field, a range query for `page_count` and
  78. regex and fuzziness queries for the `name` field.
  79. If one needs to customize various configuration options that `query_string` exposes, this can be done using the second _optional_ parameter.
  80. Multiple settings can be specified separated by a semicolon `;`:
  81. [source, sql]
  82. ----
  83. include-tagged::{sql-specs}/docs/docs.csv-spec[optionalParameterQuery]
  84. ----
  85. NOTE: The allowed optional parameters for `QUERY()` are: `allow_leading_wildcard`, `analyze_wildcard`, `analyzer`,
  86. `auto_generate_synonyms_phrase_query`, `default_field`, `default_operator`, `enable_position_increments`,
  87. `escape`, `fuzziness`, `fuzzy_max_expansions`, `fuzzy_prefix_length`, `fuzzy_rewrite`, `fuzzy_transpositions`,
  88. `lenient`, `max_determinized_states`, `minimum_should_match`, `phrase_slop`, `rewrite`, `quote_analyzer`,
  89. `quote_field_suffix`, `tie_breaker`, `time_zone`, `type`.
  90. [[sql-functions-search-score]]
  91. ==== `SCORE`
  92. .Synopsis:
  93. [source, sql]
  94. --------------------------------------------------
  95. SCORE()
  96. --------------------------------------------------
  97. *Input*: _none_
  98. *Output*: `double` numeric value
  99. .Description:
  100. Returns the {defguide}/relevance-intro.html[relevance] of a given input to the executed query.
  101. The higher score, the more relevant the data.
  102. NOTE: When doing multiple text queries in the `WHERE` clause then, their scores will be
  103. combined using the same rules as {es}'s
  104. <<query-dsl-bool-query,bool query>>.
  105. Typically `SCORE` is used for ordering the results of a query based on their relevance:
  106. [source, sql]
  107. ----
  108. include-tagged::{sql-specs}/docs/docs.csv-spec[orderByScore]
  109. ----
  110. However, it is perfectly fine to return the score without sorting by it:
  111. [source, sql]
  112. ----
  113. include-tagged::{sql-specs}/docs/docs.csv-spec[scoreWithMatch]
  114. ----