search.asciidoc 5.4 KB

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