search.asciidoc 5.8 KB

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