Skip to content

Commit b962850

Browse files
committed
Add knn query vector builder example over a semantic text field to docs (elastic#130135)
1 parent b389bf2 commit b962850

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
@@ -227,6 +227,39 @@ A sample query can look like below:
227227
```
228228

229229

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

232265
`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)