|
| 1 | +[[esql-for-search]] |
| 2 | +=== Using {esql} for search |
| 3 | + |
| 4 | +preview::[] |
| 5 | + |
| 6 | +This page provides an overview of how to use {esql} for search use cases. |
| 7 | + |
| 8 | +// [TIP] |
| 9 | +// ==== |
| 10 | +// Prefer to get started with a hands-on tutorial? Check out <<esql-search-tutorial>>. |
| 11 | +// ==== |
| 12 | + |
| 13 | +The following table summarizes the key search features available in {esql} and when they were introduced. |
| 14 | + |
| 15 | +[cols="1,1,2", options="header"] |
| 16 | +|=== |
| 17 | +|Feature |Available since |Description |
| 18 | + |
| 19 | +|<<esql-search-functions,Full text search functions>> |
| 20 | +|8.17 |
| 21 | +|Perform basic text searches with <<esql-match, match function>> and <<esql-search-operators,match operator (`:`)>> |
| 22 | + |
| 23 | +|<<esql-for-search-query-string,Query string function>> |
| 24 | +|8.17 |
| 25 | +|Execute complex queries with <<esql-qstr,`qstr`>> using Query String syntax |
| 26 | + |
| 27 | +|<<esql-for-search-scoring,Relevance scoring>> |
| 28 | +|8.18/9.0 |
| 29 | +|Calculate and sort by relevance with `METADATA _score` |
| 30 | + |
| 31 | +|Enhanced match options |
| 32 | +|8.18/9.0 |
| 33 | +|Configure text searches with additional parameters for the `match` function |
| 34 | + |
| 35 | +|<<esql-for-search-kql,Kibana Query Language>> |
| 36 | +|8.18/9.0 |
| 37 | +|Use Kibana Query Language with <<esql-kql,`kql`>> function |
| 38 | + |
| 39 | +|<<esql-for-search-semantic,Semantic search>> |
| 40 | +|8.18/9.0 |
| 41 | +|Perform semantic searches on `semantic_text` field types |
| 42 | + |
| 43 | +|<<esql-for-search-hybrid,Hybrid search>> |
| 44 | +|8.18/9.0 |
| 45 | +|Combine lexical and semantic search approaches with custom weights |
| 46 | +|=== |
| 47 | + |
| 48 | +[[esql-filtering-vs-searching]] |
| 49 | +==== Filtering vs. searching |
| 50 | + |
| 51 | +{esql} can be used for both simple filtering and relevance-based searching: |
| 52 | + |
| 53 | +* **Filtering** removes non-matching documents without calculating relevance scores |
| 54 | +* **Searching** both filters documents and ranks them by how well they match the query |
| 55 | + |
| 56 | +Note that filtering is faster than searching, because it doesn't require score calculations. |
| 57 | + |
| 58 | +[[esql-for-search-scoring]] |
| 59 | +===== Relevance scoring |
| 60 | + |
| 61 | +To get the most relevant results first, you need to use `METADATA _score` and sort by score. For example: |
| 62 | + |
| 63 | +[source,esql] |
| 64 | +---- |
| 65 | +FROM books METADATA _score |
| 66 | +| WHERE match(title, "Shakespeare") OR match(plot, "Shakespeare") |
| 67 | +| SORT _score DESC |
| 68 | +---- |
| 69 | + |
| 70 | +[[esql-for-search-how-scoring-works]] |
| 71 | +===== How `_score` works |
| 72 | + |
| 73 | +When working with relevance scoring in ES|QL: |
| 74 | + |
| 75 | +* If you don't include `METADATA _score` in your query, this only performs filtering operations with no relevance calculation. |
| 76 | +* 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. |
| 77 | +* Filtering operations that are not search functions, like range conditions and exact matches, don't affect the score. |
| 78 | +* 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. |
| 79 | + |
| 80 | +[[esql-for-search-full-text]] |
| 81 | +==== Full text search |
| 82 | + |
| 83 | +[[esql-for-search-match-function-operator]] |
| 84 | +===== Match function and operator |
| 85 | + |
| 86 | +ES|QL offers two syntax options for `match`, which replicate the functionality of <<query-dsl-match-query, `match`>> queries in Query DSL. |
| 87 | + |
| 88 | +Use the compact operator syntax (`:`) for simple text matching with default parameters. |
| 89 | + |
| 90 | +[source,esql] |
| 91 | +---- |
| 92 | +FROM logs | WHERE match(message, "connection error") |
| 93 | +---- |
| 94 | + |
| 95 | +Use the `match()` function syntax when you need to pass additional parameters: |
| 96 | + |
| 97 | +[source,esql] |
| 98 | +---- |
| 99 | +FROM products | WHERE match(name, "laptop", { "boost": 2.0 }) |
| 100 | +---- |
| 101 | + |
| 102 | +These full-text functions address several key limitations that existed for text filtering in {esql}: |
| 103 | + |
| 104 | +* They work directly on multivalued fields, returning results when any value in a multivalued field matches the query |
| 105 | +* 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) |
| 106 | +* They are highly performant, using Lucene index structures rather than pattern matching or regular expressions to locate terms in your data |
| 107 | + |
| 108 | +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]. |
| 109 | + |
| 110 | +[TIP] |
| 111 | +==== |
| 112 | +See <<match-field-params,Match field parameters>> for more advanced options using match. |
| 113 | +==== |
| 114 | + |
| 115 | +[IMPORTANT] |
| 116 | +==== |
| 117 | +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. |
| 118 | +==== |
| 119 | + |
| 120 | +[[esql-for-search-query-string]] |
| 121 | +===== Query string function (`QSTR`) |
| 122 | + |
| 123 | +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. |
| 124 | + |
| 125 | +[source,esql] |
| 126 | +---- |
| 127 | +FROM articles METADATA _score |
| 128 | +| WHERE QSTR("(new york city) OR (big apple)") |
| 129 | +| SORT _score DESC |
| 130 | +| LIMIT 10 |
| 131 | +---- |
| 132 | + |
| 133 | +For complete details, refer to the <<query-dsl-query-string-query, Query DSL `query_string` docs>>. |
| 134 | + |
| 135 | +[[esql-for-search-kql]] |
| 136 | +===== Kibana Query Language function (`KQL`) |
| 137 | + |
| 138 | +Use the <<esql-kql,KQL function>> to use the {kibana-ref}/kuery-query.html[Kibana Query Language] in your ES|QL queries: |
| 139 | + |
| 140 | +[source,esql] |
| 141 | +---- |
| 142 | +FROM logs* |
| 143 | +| WHERE KQL("http.request.method:GET AND agent.type:filebeat") |
| 144 | +---- |
| 145 | + |
| 146 | +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. |
| 147 | + |
| 148 | +[[esql-for-search-semantic]] |
| 149 | +==== Semantic search |
| 150 | + |
| 151 | +You can perform semantic searches over <<semantic-text, `semantic_text`>> field types using the same match syntax as full-text search. |
| 152 | + |
| 153 | +This example uses the match operator `:`: |
| 154 | + |
| 155 | +[source,esql] |
| 156 | +---- |
| 157 | +FROM articles METADATA _score |
| 158 | +| WHERE semantic_content:"What are the impacts of climate change on agriculture?" |
| 159 | +| SORT _score DESC |
| 160 | +---- |
| 161 | + |
| 162 | +This example uses the match function: |
| 163 | + |
| 164 | +[source,esql] |
| 165 | +---- |
| 166 | +FROM articles METADATA _score |
| 167 | +| WHERE match(semantic_content, "What are the impacts of climate change on agriculture?") |
| 168 | +| SORT _score DESC |
| 169 | +---- |
| 170 | + |
| 171 | +[[esql-for-search-hybrid]] |
| 172 | +==== Hybrid search |
| 173 | + |
| 174 | +Combine traditional and semantic search with custom weights: |
| 175 | + |
| 176 | +[source,esql] |
| 177 | +---- |
| 178 | +FROM books METADATA _score |
| 179 | +| WHERE match(semantic_title, "fantasy adventure", { "boost": 0.75 }) |
| 180 | + OR match(title, "fantasy adventure", { "boost": 0.25 }) |
| 181 | +| SORT _score DESC |
| 182 | +---- |
| 183 | + |
| 184 | +[[esql-for-search-limitations]] |
| 185 | +==== Limitations |
| 186 | + |
| 187 | +Refer to <<esql-limitations-full-text-search, {esql} limitations>> for a list of known limitations. |
| 188 | + |
| 189 | +[[esql-for-search-next-steps]] |
| 190 | +==== Next steps |
| 191 | + |
| 192 | +[[esql-for-search-tutorials]] |
| 193 | +===== Tutorials and how-to guides |
| 194 | + |
| 195 | +// TODO * <<esql-search-tutorial>>: Hands-on tutorial for getting started with search tools in {esql} |
| 196 | +* <<semantic-search-semantic-text>>: Learn how to use the `semantic_text` field type |
| 197 | + |
| 198 | +[[esql-for-search-reference]] |
| 199 | +===== Technical reference |
| 200 | + |
| 201 | +* <<esql-search-functions>>: Complete reference for all search functions |
| 202 | +* <<esql-limitations-full-text-search, Limitations>>: Current limitations for search in ES|QL |
| 203 | + |
| 204 | +[[esql-for-search-concepts]] |
| 205 | +===== Background concepts |
| 206 | + |
| 207 | +* <<analysis>>: Learn how text is processed for full-text search |
| 208 | +* <<semantic-search>>: Get an overview of semantic search in {es} |
| 209 | +* <<query-filter-context>>: Understand the difference between query and filter contexts in {es} |
| 210 | + |
| 211 | +[[esql-for-search-blogs]] |
| 212 | +===== Related blog posts |
| 213 | + |
| 214 | +// TODO* https://www.elastic.co/blog/esql-you-know-for-search-scoring-semantic-search[ES|QL, you know for Search]: Introducing scoring and semantic search |
| 215 | +* 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 |
0 commit comments