Skip to content

Commit 2d38751

Browse files
authored
Add knn query vector builder example over a semantic text field to docs (#130135)
1 parent 201a8c9 commit 2d38751

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/reference/query-languages/query-dsl/query-dsl-knn-query.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,39 @@ A sample query can look like below:
229229

230230
Note that nested `knn` only supports `score_mode=max`.
231231

232+
## Knn query on a semantic_text field [knn-query-with-semantic-text]
233+
234+
Elasticsearch supports knn queries over a [
235+
`semantic_text` field](/reference/elasticsearch/mapping-reference/semantic-text.md).
236+
237+
Here is an example using the `query_vector_builder`:
238+
239+
```json
240+
{
241+
"query": {
242+
"knn": {
243+
"field": "inference_field",
244+
"k": 10,
245+
"num_candidates": 100,
246+
"query_vector_builder": {
247+
"text_embedding": {
248+
"model_text": "test"
249+
}
250+
}
251+
}
252+
},
253+
"_source": {
254+
"exclude": "inference_field.inference.chunks"
255+
}
256+
}
257+
```
258+
259+
Note that for `semantic_text` fields, the `model_id` does not have to be
260+
provided as it can be inferred from the `semantic_text` field mapping.
261+
262+
Knn search using query vectors over `semantic_text` fields is also supported,
263+
with no change to the API.
264+
232265
## Knn query with aggregations [knn-query-aggregations]
233266

234267
`knn` query calculates aggregations on top `k` documents from each shard. Thus, the final results from aggregations contain `k * number_of_shards` documents. This is different from the [top level knn section](docs-content://solutions/search/vector/knn.md) where aggregations are calculated on the global top `k` nearest documents.

0 commit comments

Comments
 (0)