You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After reindexing the data into the `semantic-embeddings` index, you can perform hybrid search by using [reciprocal rank fusion (RRF)](elasticsearch://reference/elasticsearch/rest-apis/reciprocal-rank-fusion.md). RRF is a technique that merges the rankings from both semantic and lexical queries, giving more weight to results that rank high in either search. This ensures that the final results are balanced and relevant.
105
+
After reindexing the data into the `semantic-embeddings` index, you can perform hybrid search to combine semantic and lexical search results. Choose between [retrievers](retrievers-overview.md) or [{{esql}}](/explore-analyze/query-filter/languages/esql.md) syntax to execute the query.
106
+
107
+
::::{tab-set}
108
+
:group: query-type
109
+
110
+
:::{tab-item} Query DSL
111
+
:sync: retrievers
112
+
113
+
This example uses [retrievers syntax](retrievers-overview.md) with [reciprocal rank fusion (RRF)](elasticsearch://reference/elasticsearch/rest-apis/reciprocal-rank-fusion.md). RRF is a technique that merges the rankings from both semantic and lexical queries, giving more weight to results that rank high in either search. This ensures that the final results are balanced and relevant.
106
114
107
115
```console
108
116
GET semantic-embeddings/_search
@@ -141,7 +149,7 @@ GET semantic-embeddings/_search
141
149
4. The `semantic_text` field is used to perform the semantic search.
142
150
143
151
144
-
After performing the hybrid search, the query will return the top 10 documents that match both semantic and lexical search criteria. The results include detailed information about each document:
152
+
After performing the hybrid search, the query will return the combined top 10 documents for both semantic and lexical search criteria. The results include detailed information about each document.
145
153
146
154
```console-result
147
155
{
@@ -202,3 +210,30 @@ After performing the hybrid search, the query will return the top 10 documents t
202
210
}
203
211
}
204
212
```
213
+
:::
214
+
215
+
:::{tab-item} ES|QL
216
+
:sync: esql
217
+
218
+
The ES|QL approach uses a combination of the match operator `:` and the match function `match()` to perform hybrid search.
219
+
220
+
```console
221
+
POST /_query?format=txt
222
+
{
223
+
"query": """
224
+
FROM semantic-embeddings METADATA _score <1>
225
+
| WHERE content: "muscle soreness running?" OR match(semantic_text, "How to avoid muscle soreness while running?", { "boost": 0.75 }) <2> <3>
226
+
| SORT _score DESC <4>
227
+
| LIMIT 1000
228
+
"""
229
+
}
230
+
```
231
+
1. The `METADATA _score` clause is used to return the score of each document
232
+
2. The [match (`:`) operator](elasticsearch://reference/query-languages/esql/esql-functions-operators.md#esql-search-operators) is used on the `content` field for standard keyword matching
233
+
3. Semantic search using the `match()` function on the `semantic_text` field with a boost of `0.75`
234
+
4. Sorts by descending score and limits to 1000 results
After the data set has been enriched with the embeddings, you can query the data using semantic search. Provide the `semantic_text` field name and the query text in a `semantic` query type. The {{infer}} endpoint used to generate the embeddings for the `semantic_text` field will be used to process the query text.
104
+
After the data has been indexed with the embeddings, you can query the data using semantic search. Choose between [Query DSL](/explore-analyze/query-filter/languages/querydsl.md) or [{{esql}}](/explore-analyze/query-filter/languages/esql.md) syntax to execute the query.
105
105
106
-
```console
106
+
::::{tab-set}
107
+
:group: query-type
108
+
109
+
:::{tab-item} Query DSL
110
+
:sync: dsl
111
+
112
+
The Query DSL approach uses the `semantic` query type with the `semantic_text` field:
113
+
114
+
```esql
107
115
GET semantic-embeddings/_search
108
116
{
109
117
"query": {
110
118
"semantic": {
111
119
"field": "content", <1>
112
-
"query": "How to avoid muscle soreness while running?" <2>
120
+
"query": "What causes muscle soreness after running?" <2>
113
121
}
114
122
}
115
123
}
116
124
```
117
125
118
126
1. The `semantic_text` field on which you want to perform the search.
119
127
2. The query text.
128
+
:::
129
+
130
+
:::{tab-item} ES|QL
131
+
:sync: esql
132
+
133
+
The ES|QL approach uses the [match (`:`) operator](elasticsearch://reference/query-languages/esql/esql-functions-operators.md#esql-search-operators), which automatically detects the `semantic_text` field and performs the search on it. The query uses `METADATA _score` to sort by `_score` in descending order.
120
134
121
135
122
-
As a result, you receive the top 10 documents that are closest in meaning to the query from the `semantic-embedding` index.
136
+
```console
137
+
POST /_query?format=txt
138
+
{
139
+
"query": """
140
+
FROM semantic-embeddings METADATA _score <1>
141
+
| WHERE content: "How to avoid muscle soreness while running?" <2>
142
+
| SORT _score DESC <3>
143
+
| LIMIT 1000 <4>
144
+
"""
145
+
}
146
+
```
147
+
1. The `METADATA _score` clause is used to return the score of each document
148
+
2. The [match (`:`) operator](elasticsearch://reference/query-languages/esql/esql-functions-operators.md#esql-search-operators) is used on the `content` field for standard keyword matching
149
+
3. Sorts by descending score to display the most relevant results first
150
+
4. Limits the results to 1000 documents
151
+
152
+
:::
153
+
::::
123
154
124
155
125
156
## Further examples and reading [semantic-text-further-examples]
0 commit comments