Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion explore-analyze/query-filter/languages/querydsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mapped_urls:
- https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
---

# QueryDSL
# Query DSL

$$$filter-context$$$

Expand Down
39 changes: 37 additions & 2 deletions solutions/search/hybrid-semantic-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ POST _tasks/<task_id>/_cancel

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

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

::::{tab-set}
:group: query-type

:::{tab-item} Query DSL
:sync: retrievers

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.

```console
GET semantic-embeddings/_search
Expand Down Expand Up @@ -141,7 +149,7 @@ GET semantic-embeddings/_search
4. The `semantic_text` field is used to perform the semantic search.


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

```console-result
{
Expand Down Expand Up @@ -202,3 +210,30 @@ After performing the hybrid search, the query will return the top 10 documents t
}
}
```
:::

:::{tab-item} ES|QL
:sync: esql

The ES|QL approach uses a combination of the match operator `:` and the match function `match()` to perform hybrid search.

```console
POST /_query?format=txt
{
"query": """
FROM semantic-embeddings METADATA _score <1>
| WHERE content: "muscle soreness running?" OR match(semantic_text, "How to avoid muscle soreness while running?", { "boost": 0.75 }) <2> <3>
| SORT _score DESC <4>
| LIMIT 1000
"""
}
```
1. The `METADATA _score` clause is used to return the score of each document
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
3. Semantic search using the `match()` function on the `semantic_text` field with a boost of `0.75`
4. Sorts by descending score and limits to 1000 results
:::
::::



Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,56 @@ POST _tasks/<task_id>/_cancel

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

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

```console
::::{tab-set}
:group: query-type

:::{tab-item} Query DSL
:sync: dsl

The Query DSL approach uses the `semantic` query type with the `semantic_text` field:

```esql
GET semantic-embeddings/_search
{
"query": {
"semantic": {
"field": "content", <1>
"query": "How to avoid muscle soreness while running?" <2>
"query": "What causes muscle soreness after running?" <2>
}
}
}
```

1. The `semantic_text` field on which you want to perform the search.
2. The query text.
:::

:::{tab-item} ES|QL
:sync: esql

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.


As a result, you receive the top 10 documents that are closest in meaning to the query from the `semantic-embedding` index.
```console
POST /_query?format=txt
{
"query": """
FROM semantic-embeddings METADATA _score <1>
| WHERE content: "How to avoid muscle soreness while running?" <2>
| SORT _score DESC <3>
| LIMIT 1000 <4>
"""
}
```
1. The `METADATA _score` clause is used to return the score of each document
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
3. Sorts by descending score to display the most relevant results first
4. Limits the results to 1000 documents

:::
::::


## Further examples and reading [semantic-text-further-examples]
Expand Down
Loading