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