search.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. [role="xpack"]
  2. [[sql-functions-search]]
  3. === Full-Text Search Functions
  4. Search functions should be used when performing full-text search, namely
  5. when the `MATCH` or `QUERY` predicates are being used.
  6. Outside a, so-called, search context, these functions will return default values
  7. such as `0` or `NULL`.
  8. {es-sql} optimizes all queries executed against {es} depending on the scoring needs.
  9. Using <<_track_scores,`track_scores`>> on the search request or <<sort-search-results,`_doc` sorting>> that
  10. disables scores calculation, {es-sql} instructs {es} not to compute scores when these are not needed.
  11. For example, every time a `SCORE()` function is encountered in the SQL query, the scores are computed.
  12. [[sql-functions-search-match]]
  13. ==== `MATCH`
  14. .Synopsis:
  15. [source, sql]
  16. --------------------------------------------------
  17. MATCH(
  18. field_exp, <1>
  19. constant_exp <2>
  20. [, options]) <3>
  21. --------------------------------------------------
  22. *Input*:
  23. <1> field(s) to match
  24. <2> matching text
  25. <3> additional parameters; optional
  26. *Description*: 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>>
  27. and <<query-dsl-multi-match-query,multi_match>> {es} queries.
  28. 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:
  29. [source, sql]
  30. ----
  31. include-tagged::{sql-specs}/docs/docs.csv-spec[simpleMatch]
  32. ----
  33. However, it can also receive a list of fields and their corresponding optional `boost` value. In this case, {es-sql} will use a
  34. `multi_match` query to match the documents:
  35. [source, sql]
  36. ----
  37. include-tagged::{sql-specs}/docs/docs.csv-spec[multiFieldsMatch]
  38. ----
  39. NOTE: The `multi_match` query in {es} has the option of <<query-dsl-multi-match-query,per-field boosting>> that gives preferential weight
  40. (in terms of scoring) to fields being searched in, using the `^` character. In the example above, the `name` field has a greater weight in
  41. the final score than the `author` field when searching for `frank dune` text in both of them.
  42. Both options above can be used in combination with the optional third parameter of the `MATCH()` predicate, where one can specify
  43. additional configuration parameters (separated by semicolon `;`) for either `match` or `multi_match` queries. For example:
  44. [source, sql]
  45. ----
  46. include-tagged::{sql-specs}/docs/docs.csv-spec[optionalParamsForMatch]
  47. ----
  48. NOTE: The allowed optional parameters for a single-field `MATCH()` variant (for the `match` {es} query) are: `analyzer`, `auto_generate_synonyms_phrase_query`,
  49. `lenient`, `fuzziness`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
  50. `max_expansions`, `prefix_length`.
  51. NOTE: The allowed optional parameters for a multi-field `MATCH()` variant (for the `multi_match` {es} query) are: `analyzer`, `auto_generate_synonyms_phrase_query`,
  52. `lenient`, `fuzziness`, `fuzzy_transpositions`, `fuzzy_rewrite`, `minimum_should_match`, `operator`,
  53. `max_expansions`, `prefix_length`, `slop`, `tie_breaker`, `type`.
  54. [[sql-functions-search-query]]
  55. ==== `QUERY`
  56. .Synopsis:
  57. [source, sql]
  58. --------------------------------------------------
  59. QUERY(
  60. constant_exp <1>
  61. [, options]) <2>
  62. --------------------------------------------------
  63. *Input*:
  64. <1> query text
  65. <2> additional parameters; optional
  66. *Description*: 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}.
  67. 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`
  68. accepts in its `query` field can be used here as well:
  69. [source, sql]
  70. ----
  71. include-tagged::{sql-specs}/docs/docs.csv-spec[simpleQueryQuery]
  72. ----
  73. A more advanced example, showing more of the features that `query_string` supports, of course possible with {es-sql}:
  74. [source, sql]
  75. ----
  76. include-tagged::{sql-specs}/docs/docs.csv-spec[advancedQueryQuery]
  77. ----
  78. The query above uses the `_exists_` query to select documents that have values in the `author` field, a range query for `page_count` and
  79. regex and fuzziness queries for the `name` field.
  80. If one needs to customize various configuration options that `query_string` exposes, this can be done using the second _optional_ parameter.
  81. Multiple settings can be specified separated by a semicolon `;`:
  82. [source, sql]
  83. ----
  84. include-tagged::{sql-specs}/docs/docs.csv-spec[optionalParameterQuery]
  85. ----
  86. NOTE: The allowed optional parameters for `QUERY()` are: `allow_leading_wildcard`, `analyze_wildcard`, `analyzer`,
  87. `auto_generate_synonyms_phrase_query`, `default_field`, `default_operator`, `enable_position_increments`,
  88. `escape`, `fuzziness`, `fuzzy_max_expansions`, `fuzzy_prefix_length`, `fuzzy_rewrite`, `fuzzy_transpositions`,
  89. `lenient`, `max_determinized_states`, `minimum_should_match`, `phrase_slop`, `rewrite`, `quote_analyzer`,
  90. `quote_field_suffix`, `tie_breaker`, `time_zone`, `type`.
  91. [[sql-functions-search-score]]
  92. ==== `SCORE`
  93. .Synopsis:
  94. [source, sql]
  95. --------------------------------------------------
  96. SCORE()
  97. --------------------------------------------------
  98. *Input*: _none_
  99. *Output*: `double` numeric value
  100. *Description*: 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. ----