Skip to content

Commit 7ddd0fb

Browse files
authored
[Search] Add ESQL syntax to semantic and hybrid search tutorials (#821)
1 parent 584e1c7 commit 7ddd0fb

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

explore-analyze/query-filter/languages/querydsl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mapped_urls:
77
- https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
88
---
99

10-
# QueryDSL
10+
# Query DSL
1111

1212
$$$filter-context$$$
1313

solutions/search/hybrid-semantic-text.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ POST _tasks/<task_id>/_cancel
102102

103103
## Perform hybrid search [hybrid-search-perform-search]
104104

105-
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.
106114

107115
```console
108116
GET semantic-embeddings/_search
@@ -141,7 +149,7 @@ GET semantic-embeddings/_search
141149
4. The `semantic_text` field is used to perform the semantic search.
142150

143151

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.
145153

146154
```console-result
147155
{
@@ -202,3 +210,30 @@ After performing the hybrid search, the query will return the top 10 documents t
202210
}
203211
}
204212
```
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
235+
:::
236+
::::
237+
238+
239+

solutions/search/semantic-search/semantic-search-semantic-text.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,56 @@ POST _tasks/<task_id>/_cancel
101101

102102
## Semantic search [semantic-text-semantic-search]
103103

104-
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.
105105

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
107115
GET semantic-embeddings/_search
108116
{
109117
"query": {
110118
"semantic": {
111119
"field": "content", <1>
112-
"query": "How to avoid muscle soreness while running?" <2>
120+
"query": "What causes muscle soreness after running?" <2>
113121
}
114122
}
115123
}
116124
```
117125

118126
1. The `semantic_text` field on which you want to perform the search.
119127
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.
120134

121135

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+
::::
123154

124155

125156
## Further examples and reading [semantic-text-further-examples]

0 commit comments

Comments
 (0)