esql-for-search.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. [[esql-for-search]]
  2. === Using {esql} for search
  3. preview::[]
  4. This page provides an overview of how to use {esql} for search use cases.
  5. [TIP]
  6. ====
  7. Prefer to get started with a hands-on tutorial? Check out <<esql-search-tutorial>>.
  8. ====
  9. The following table summarizes the key search features available in {esql} and when they were introduced.
  10. [cols="1,1,2", options="header"]
  11. |===
  12. |Feature |Available since |Description
  13. |<<esql-search-functions,Full text search functions>>
  14. |8.17
  15. |Perform basic text searches with <<esql-match, match function>> and <<esql-search-operators,match operator (`:`)>>
  16. |<<esql-for-search-query-string,Query string function>>
  17. |8.17
  18. |Execute complex queries with <<esql-qstr,`qstr`>> using Query String syntax
  19. |<<esql-for-search-scoring,Relevance scoring>>
  20. |8.18/9.0
  21. |Calculate and sort by relevance with `METADATA _score`
  22. |Enhanced match options
  23. |8.18/9.0
  24. |Configure text searches with additional parameters for the `match` function
  25. |<<esql-for-search-kql,Kibana Query Language>>
  26. |8.18/9.0
  27. |Use Kibana Query Language with <<esql-kql,`kql`>> function
  28. |<<esql-for-search-semantic,Semantic search>>
  29. |8.18/9.0
  30. |Perform semantic searches on `semantic_text` field types
  31. |<<esql-for-search-hybrid,Hybrid search>>
  32. |8.18/9.0
  33. |Combine lexical and semantic search approaches with custom weights
  34. |===
  35. [[esql-filtering-vs-searching]]
  36. ==== Filtering vs. searching
  37. {esql} can be used for both simple filtering and relevance-based searching:
  38. * **Filtering** removes non-matching documents without calculating relevance scores
  39. * **Searching** both filters documents and ranks them by how well they match the query
  40. Note that filtering is faster than searching, because it doesn't require score calculations.
  41. [[esql-for-search-scoring]]
  42. ===== Relevance scoring
  43. To get the most relevant results first, you need to use `METADATA _score` and sort by score. For example:
  44. [source,esql]
  45. ----
  46. FROM books METADATA _score
  47. | WHERE match(title, "Shakespeare") OR match(plot, "Shakespeare")
  48. | SORT _score DESC
  49. ----
  50. [[esql-for-search-how-scoring-works]]
  51. ===== How `_score` works
  52. When working with relevance scoring in ES|QL:
  53. * If you don't include `METADATA _score` in your query, this only performs filtering operations with no relevance calculation.
  54. * When you include `METADATA _score`, any search function included in `WHERE` conditions contribute to the relevance score. This means that every occurrence of `MATCH`, `QSTR` and `KQL` will affect the score.
  55. * Filtering operations that are not search functions, like range conditions and exact matches, don't affect the score.
  56. * Including `METADATA _score` doesn't automatically sort your results by relevance. You must explicitly use `SORT _score DESC` or `SORT _score ASC` to order your results by relevance.
  57. [[esql-for-search-full-text]]
  58. ==== Full text search
  59. [[esql-for-search-match-function-operator]]
  60. ===== Match function and operator
  61. ES|QL offers two syntax options for `match`, which replicate the functionality of <<query-dsl-match-query, `match`>> queries in Query DSL.
  62. Use the compact operator syntax (`:`) for simple text matching with default parameters.
  63. [source,esql]
  64. ----
  65. FROM logs | WHERE match(message, "connection error")
  66. ----
  67. Use the `match()` function syntax when you need to pass additional parameters:
  68. [source,esql]
  69. ----
  70. FROM products | WHERE match(name, "laptop", { "boost": 2.0 })
  71. ----
  72. These full-text functions address several key limitations that existed for text filtering in {esql}:
  73. * They work directly on multivalued fields, returning results when any value in a multivalued field matches the query
  74. * They leverage analyzers, ensuring the query is analyzed with the same process as the indexed data (enabling case-insensitive matching, ASCII folding, stopword removal, and synonym support)
  75. * They are highly performant, using Lucene index structures rather than pattern matching or regular expressions to locate terms in your data
  76. Refer to this blog for more context: https://www.elastic.co/search-labs/blog/filtering-in-esql-full-text-search-match-qstr[Introducing full text filtering in ES|QL].
  77. [TIP]
  78. ====
  79. See <<match-field-params,Match field parameters>> for more advanced options using match.
  80. ====
  81. [IMPORTANT]
  82. ====
  83. These queries match documents but don't automatically sort by relevance. To get the most relevant results first, you need to use `METADATA _score` and sort by score. See <<esql-for-search-scoring,Relevance scoring>> for more information.
  84. ====
  85. [[esql-for-search-query-string]]
  86. ===== Query string function (`QSTR`)
  87. The <<esql-qstr,`qstr` function>> provides the same functionality as the Query DSL's `query_string` query. This is for advanced use cases, such as wildcard searches, searches across multiple fields, and more.
  88. [source,esql]
  89. ----
  90. FROM articles METADATA _score
  91. | WHERE QSTR("(new york city) OR (big apple)")
  92. | SORT _score DESC
  93. | LIMIT 10
  94. ----
  95. For complete details, refer to the <<query-dsl-query-string-query, Query DSL `query_string` docs>>.
  96. [[esql-for-search-kql]]
  97. ===== Kibana Query Language function (`KQL`)
  98. Use the <<esql-kql,KQL function>> to use the {kibana-ref}/kuery-query.html[Kibana Query Language] in your ES|QL queries:
  99. [source,esql]
  100. ----
  101. FROM logs*
  102. | WHERE KQL("http.request.method:GET AND agent.type:filebeat")
  103. ----
  104. The `kql` function is useful when transitioning queries from Kibana's Discover, Dashboard, or other interfaces that use KQL. This will allow you to gradually migrate queries to ES|QL without needing to rewrite them all at once.
  105. [[esql-for-search-semantic]]
  106. ==== Semantic search
  107. You can perform semantic searches over <<semantic-text, `semantic_text`>> field types using the same match syntax as full-text search.
  108. This example uses the match operator `:`:
  109. [source,esql]
  110. ----
  111. FROM articles METADATA _score
  112. | WHERE semantic_content:"What are the impacts of climate change on agriculture?"
  113. | SORT _score DESC
  114. ----
  115. This example uses the match function:
  116. [source,esql]
  117. ----
  118. FROM articles METADATA _score
  119. | WHERE match(semantic_content, "What are the impacts of climate change on agriculture?")
  120. | SORT _score DESC
  121. ----
  122. [[esql-for-search-hybrid]]
  123. ==== Hybrid search
  124. Combine traditional and semantic search with custom weights:
  125. [source,esql]
  126. ----
  127. FROM books METADATA _score
  128. | WHERE match(semantic_title, "fantasy adventure", { "boost": 0.75 })
  129. OR match(title, "fantasy adventure", { "boost": 0.25 })
  130. | SORT _score DESC
  131. ----
  132. [[esql-for-search-limitations]]
  133. ==== Limitations
  134. Refer to <<esql-limitations-full-text-search, {esql} limitations>> for a list of known limitations.
  135. [[esql-for-search-next-steps]]
  136. ==== Next steps
  137. [[esql-for-search-tutorials]]
  138. ===== Tutorials and how-to guides
  139. * <<esql-search-tutorial>>: Hands-on tutorial for getting started with search tools in {esql}
  140. * <<semantic-search-semantic-text>>: Learn how to use the `semantic_text` field type
  141. [[esql-for-search-reference]]
  142. ===== Technical reference
  143. * <<esql-search-functions>>: Complete reference for all search functions
  144. * <<esql-limitations-full-text-search, Limitations>>: Current limitations for search in ES|QL
  145. [[esql-for-search-concepts]]
  146. ===== Background concepts
  147. * <<analysis>>: Learn how text is processed for full-text search
  148. * <<semantic-search>>: Get an overview of semantic search in {es}
  149. * <<query-filter-context>>: Understand the difference between query and filter contexts in {es}
  150. [[esql-for-search-blogs]]
  151. ===== Related blog posts
  152. * https://www.elastic.co/search-labs/blog/esql-introducing-scoring-semantic-search[ES|QL, you know for Search]: Introducing scoring and semantic search
  153. * https://www.elastic.co/search-labs/blog/filtering-in-esql-full-text-search-match-qstr[Introducing full text filtering in ES|QL]: Overview of text filtering capabilities