Dense vector query
Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud. Availability This functionality is generally available and ready for production usage. Serverless interfaces and procedures might differ from classic Elastic Stack deployments. The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats. Availability We plan to add this functionality in a future Elastic Stack update. Subject to changes. Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments. This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.
Serverless
Generally available
Stack
Planned
Planned
Performs exact, brute-force scoring of a query vector against every document that has a value for a
dense_vector field. Unlike the approximate
knn query, it does not use an index
structure or an exploration cutoff based on k or num_candidates: every matching document is scored. This is useful when you need
exact scores, want to combine exact vector scoring with other queries, or want to score a dense_vector
field that is not indexed for approximate search (index: false).
By default, scoring uses the original, full-precision vectors, so a quantized
index still produces full-precision scores. See quantized to score against
the quantized representation instead.
PUT my-image-index{ "mappings": { "properties": { "image-vector": { "type": "dense_vector", "dims": 3, "index": true, "similarity": "l2_norm" } } } }
Index your data.
JSON POST my-image-index/_bulk?refresh=true{ "index": { "_id": "1" } } { "image-vector": [1, 5, -20] } { "index": { "_id": "2" } } { "image-vector": [42, 8, -15] } { "index": { "_id": "3" } } { "image-vector": [15, 11, 23] }Run the
dense_vectorquery. Every document with the field is scored.JSON POST my-image-index/_search{ "query": { "dense_vector": { "field": "image-vector", "query_vector": [-5, 9, -12] } } }
field- (Required, string) The name of the
dense_vectorfield to search against. query_vector- (Optional, array of floats or string) The query vector. Must have the same number of dimensions as the
target field. You must provide either
query_vectororquery_vector_builder, but not both. Accepts a float array, or a hex-encoded or base64-encoded string. query_vector_builder- (Optional, object) A configuration object used to convert a query into a
query_vector, for example by running a text embedding model. You must provide eitherquery_vectororquery_vector_builder, but not both. Seequery_vector_builderfor details. similarity_function- (Optional, string) The similarity metric used for scoring, overriding the field's mapped similarity for
this query. One of
l2_norm,dot_product,cosine, ormax_inner_product(see valid similarity values for definitions). Defaults to the field's configured similarity, or tocosinefor a non-indexed (index: false) field, which has no configured similarity. Forbitfields, onlyl2_normis supported. Cannot be combined withquantized: true.
quantized-
(Optional, Boolean) Defaults to
false.When
false(the default), scoring iterates the preserved full-precision vectors, producing raw scores regardless of the field'sindex_options.When
true, scoring uses the codec's scorer, which on a quantized index scores against the lossy quantized representation (faster, lower fidelity). It cannot be combined withsimilarity_function.Notequantized: trueis a best-effort option: it uses quantized vectors when the field is quantized, and falls back to full-precision scoring for non-quantizeddense_vectorfields. During an index type transition (for example,hnswtoint8_hnsw), older segments retain their original codec until merged away, so scores within a single query may mix quantized and full-precision semantics. boost- (Optional, float) Floating point number used to multiply the scores of the query. Defaults to
1.0. _name- (Optional, string) Name to identify the query for named queries.
The dense_vector query can score a field mapped with index: false, whose vectors are stored as doc
values rather than in an approximate-search index. A non-indexed field has no configured similarity, so
scoring uses cosine unless you specify a different metric with
similarity_function:
POST my-image-index/_search{ "query": { "dense_vector": { "field": "image-vector", "query_vector": [-5, 9, -12], "similarity_function": "cosine" } } }