Skip to content

Commit 6e929fa

Browse files
committed
[Docs] kNN vector rescoring for quantized vectors (elastic#118425)
1 parent f3a481a commit 6e929fa

File tree

6 files changed

+187
-85
lines changed

6 files changed

+187
-85
lines changed

docs/reference/mapping/types/dense-vector.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ The three following quantization strategies are supported:
121121
* `bbq` - experimental:[] Better binary quantization which reduces each dimension to a single bit precision. This reduces the memory footprint by 96% (or 32x) at a larger cost of accuracy. Generally, oversampling during query time and reranking can help mitigate the accuracy loss.
122122

123123

124-
When using a quantized format, you may want to oversample and rescore the results to improve accuracy. See <<dense-vector-knn-search-reranking, oversampling and rescoring>> for more information.
124+
When using a quantized format, you may want to oversample and rescore the results to improve accuracy. See <<dense-vector-knn-search-rescoring, oversampling and rescoring>> for more information.
125125

126126
To use a quantized index, you can set your index type to `int8_hnsw`, `int4_hnsw`, or `bbq_hnsw`. When indexing `float` vectors, the current default
127127
index type is `int8_hnsw`.
128128

129+
Quantized vectors can use <<dense-vector-knn-search-rescoring,oversampling and rescoring>> to improve accuracy on approximate kNN search results.
130+
129131
NOTE: Quantization will continue to keep the raw float vector values on disk for reranking, reindexing, and quantization improvements over the lifetime of the data.
130132
This means disk usage will increase by ~25% for `int8`, ~12.5% for `int4`, and ~3.1% for `bbq` due to the overhead of storing the quantized and raw vectors.
131133

docs/reference/query-dsl/knn-query.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ documents are then scored according to <<dense-vector-similarity, `similarity`>>
137137
and the provided `boost` is applied.
138138
--
139139

140+
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=knn-rescore-vector]
141+
142+
140143
`boost`::
141144
+
142145
--

docs/reference/rest-api/common-parms.asciidoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,3 +1346,27 @@ tag::rrf-filter[]
13461346
Applies the specified <<query-dsl-bool-query, boolean query filter>> to all of the specified sub-retrievers,
13471347
according to each retriever's specifications.
13481348
end::rrf-filter[]
1349+
1350+
tag::knn-rescore-vector[]
1351+
1352+
`rescore_vector`::
1353+
+
1354+
--
1355+
(Optional, object) Functionality in preview:[]. Apply oversampling and rescoring to quantized vectors.
1356+
1357+
NOTE: Rescoring only makes sense for quantized vectors; when <<dense-vector-quantization,quantization>> is not used, the original vectors are used for scoring.
1358+
Rescore option will be ignored for non-quantized `dense_vector` fields.
1359+
1360+
`oversample`::
1361+
(Required, float)
1362+
+
1363+
Applies the specified oversample factor to `k` on the approximate kNN search.
1364+
The approximate kNN search will:
1365+
1366+
* Retrieve `num_candidates` candidates per shard.
1367+
* From these candidates, the top `k * oversample` candidates per shard will be rescored using the original vectors.
1368+
* The top `k` rescored candidates will be returned.
1369+
1370+
See <<dense-vector-knn-search-rescoring,oversampling and rescoring quantized vectors>> for details.
1371+
--
1372+
end::knn-rescore-vector[]

docs/reference/search/retriever.asciidoc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=knn-filter]
233233
+
234234
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=knn-similarity]
235235

236+
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=knn-rescore-vector]
237+
236238
===== Restrictions
237239

238240
The parameters `query_vector` and `query_vector_builder` cannot be used together.
@@ -571,15 +573,15 @@ This examples demonstrates how to deploy the Elastic Rerank model and use it to
571573

572574
Follow these steps:
573575

574-
. Create an inference endpoint for the `rerank` task using the <<put-inference-api, Create {infer} API>>.
576+
. Create an inference endpoint for the `rerank` task using the <<put-inference-api, Create {infer} API>>.
575577
+
576578
[source,console]
577579
----
578580
PUT _inference/rerank/my-elastic-rerank
579581
{
580582
"service": "elasticsearch",
581583
"service_settings": {
582-
"model_id": ".rerank-v1",
584+
"model_id": ".rerank-v1",
583585
"num_threads": 1,
584586
"adaptive_allocations": { <1>
585587
"enabled": true,
@@ -590,7 +592,7 @@ PUT _inference/rerank/my-elastic-rerank
590592
}
591593
----
592594
// TEST[skip:uses ML]
593-
<1> {ml-docs}/ml-nlp-auto-scale.html#nlp-model-adaptive-allocations[Adaptive allocations] will be enabled with the minimum of 1 and the maximum of 10 allocations.
595+
<1> {ml-docs}/ml-nlp-auto-scale.html#nlp-model-adaptive-allocations[Adaptive allocations] will be enabled with the minimum of 1 and the maximum of 10 allocations.
594596
+
595597
. Define a `text_similarity_rerank` retriever:
596598
+

docs/reference/search/search-your-data/knn-search.asciidoc

Lines changed: 150 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ What if you wanted to filter by some top-level document metadata? You can do thi
781781

782782

783783
NOTE: `filter` will always be over the top-level document metadata. This means you cannot filter based on `nested`
784-
field metadata.
784+
field metadata.
785785

786786
[source,console]
787787
----
@@ -1066,100 +1066,77 @@ NOTE: Approximate kNN search always uses the
10661066
the global top `k` matches across shards. You cannot set the
10671067
`search_type` explicitly when running kNN search.
10681068

1069+
10691070
[discrete]
1070-
[[exact-knn]]
1071-
=== Exact kNN
1071+
[[dense-vector-knn-search-rescoring]]
1072+
==== Oversampling and rescoring for quantized vectors
10721073

1073-
To run an exact kNN search, use a `script_score` query with a vector function.
1074+
When using <<dense-vector-quantization,quantized vectors>> for kNN search, you can optionally rescore results to balance performance and accuracy, by doing:
10741075

1075-
. Explicitly map one or more `dense_vector` fields. If you don't intend to use
1076-
the field for approximate kNN, set the `index` mapping option to `false`. This
1077-
can significantly improve indexing speed.
1078-
+
1079-
[source,console]
1080-
----
1081-
PUT product-index
1082-
{
1083-
"mappings": {
1084-
"properties": {
1085-
"product-vector": {
1086-
"type": "dense_vector",
1087-
"dims": 5,
1088-
"index": false
1089-
},
1090-
"price": {
1091-
"type": "long"
1092-
}
1093-
}
1094-
}
1095-
}
1096-
----
1076+
* *Oversampling*: Retrieve more candidates per shard.
1077+
* *Rescoring*: Use the original vector values for re-calculating the score on the oversampled candidates.
10971078

1098-
. Index your data.
1099-
+
1100-
[source,console]
1101-
----
1102-
POST product-index/_bulk?refresh=true
1103-
{ "index": { "_id": "1" } }
1104-
{ "product-vector": [230.0, 300.33, -34.8988, 15.555, -200.0], "price": 1599 }
1105-
{ "index": { "_id": "2" } }
1106-
{ "product-vector": [-0.5, 100.0, -13.0, 14.8, -156.0], "price": 799 }
1107-
{ "index": { "_id": "3" } }
1108-
{ "product-vector": [0.5, 111.3, -13.0, 14.8, -156.0], "price": 1099 }
1109-
...
1110-
----
1111-
//TEST[continued]
1112-
//TEST[s/\.\.\.//]
1079+
As the non-quantized, original vectors are used to calculate the final score on the top results, rescoring combines:
1080+
1081+
* The performance and memory gains of approximate retrieval using quantized vectors for retrieving the top candidates.
1082+
* The accuracy of using the original vectors for rescoring the top candidates.
1083+
1084+
All forms of quantization will result in some accuracy loss and as the quantization level increases the accuracy loss will also increase.
1085+
Generally, we have found that:
1086+
1087+
* `int8` requires minimal if any rescoring
1088+
* `int4` requires some rescoring for higher accuracy and larger recall scenarios. Generally, oversampling by 1.5x-2x recovers most of the accuracy loss.
1089+
* `bbq` requires rescoring except on exceptionally large indices or models specifically designed for quantization. We have found that between 3x-5x oversampling is generally sufficient. But for fewer dimensions or vectors that do not quantize well, higher oversampling may be required.
1090+
1091+
You can use the `rescore_vector` preview:[] option to automatically perform reranking.
1092+
When a rescore `oversample` parameter is specified, the approximate kNN search will:
1093+
1094+
* Retrieve `num_candidates` candidates per shard.
1095+
* From these candidates, the top `k * oversample` candidates per shard will be rescored using the original vectors.
1096+
* The top `k` rescored candidates will be returned.
1097+
1098+
Here is an example of using the `rescore_vector` option with the `oversample` parameter:
11131099

1114-
. Use the <<search-search,search API>> to run a `script_score` query containing
1115-
a <<vector-functions,vector function>>.
1116-
+
1117-
TIP: To limit the number of matched documents passed to the vector function, we
1118-
recommend you specify a filter query in the `script_score.query` parameter. If
1119-
needed, you can use a <<query-dsl-match-all-query,`match_all` query>> in this
1120-
parameter to match all documents. However, matching all documents can
1121-
significantly increase search latency.
1122-
+
11231100
[source,console]
11241101
----
1125-
POST product-index/_search
1102+
POST image-index/_search
11261103
{
1127-
"query": {
1128-
"script_score": {
1129-
"query" : {
1130-
"bool" : {
1131-
"filter" : {
1132-
"range" : {
1133-
"price" : {
1134-
"gte": 1000
1135-
}
1136-
}
1137-
}
1138-
}
1139-
},
1140-
"script": {
1141-
"source": "cosineSimilarity(params.queryVector, 'product-vector') + 1.0",
1142-
"params": {
1143-
"queryVector": [-0.5, 90.0, -10, 14.8, -156.0]
1144-
}
1145-
}
1104+
"knn": {
1105+
"field": "image-vector",
1106+
"query_vector": [-5, 9, -12],
1107+
"k": 10,
1108+
"num_candidates": 100,
1109+
"rescore_vector": {
1110+
"oversample": 2.0
11461111
}
1147-
}
1112+
},
1113+
"fields": [ "title", "file-type" ]
11481114
}
11491115
----
11501116
//TEST[continued]
1117+
// TEST[s/"k": 10/"k": 3/]
1118+
// TEST[s/"num_candidates": 100/"num_candidates": 3/]
1119+
1120+
This example will:
1121+
1122+
* Search using approximate kNN for the top 100 candidates.
1123+
* Rescore the top 20 candidates (`oversample * k`) per shard using the original, non quantized vectors.
1124+
* Return the top 10 (`k`) rescored candidates.
1125+
* Merge the rescored canddidates from all shards, and return the top 10 (`k`) results.
11511126

11521127
[discrete]
1153-
[[dense-vector-knn-search-reranking]]
1154-
==== Oversampling and rescoring for quantized vectors
1128+
[[dense-vector-knn-search-rescoring-rescore-additional]]
1129+
===== Additional rescoring techniques
11551130

1156-
All forms of quantization will result in some accuracy loss and as the quantization level increases the accuracy loss will also increase.
1157-
Generally, we have found that:
1158-
- `int8` requires minimal if any rescoring
1159-
- `int4` requires some rescoring for higher accuracy and larger recall scenarios. Generally, oversampling by 1.5x-2x recovers most of the accuracy loss.
1160-
- `bbq` requires rescoring except on exceptionally large indices or models specifically designed for quantization. We have found that between 3x-5x oversampling is generally sufficient. But for fewer dimensions or vectors that do not quantize well, higher oversampling may be required.
1131+
The following sections provide additional ways of rescoring:
1132+
1133+
[discrete]
1134+
[[dense-vector-knn-search-rescoring-rescore-section]]
1135+
====== Use the `rescore` section for top-level kNN search
1136+
1137+
You can use this option when you don't want to rescore on each shard, but on the top results from all shards.
11611138

1162-
There are two main ways to oversample and rescore. The first is to utilize the <<rescore, rescore section>> in the `_search` request.
1139+
Use the <<rescore, rescore section>> in the `_search` request to rescore the top results from a kNN search.
11631140

11641141
Here is an example using the top level `knn` search with oversampling and using `rescore` to rerank the results:
11651142

@@ -1208,8 +1185,16 @@ gathering 20 nearest neighbors according to quantized scoring and rescoring with
12081185
<5> The weight of the original query, here we simply throw away the original score
12091186
<6> The weight of the rescore query, here we only use the rescore query
12101187

1211-
The second way is to score per shard with the <<query-dsl-knn-query, knn query>> and <<query-dsl-script-score-query, script_score query >>. Generally, this means that there will be more rescoring per shard, but this
1212-
can increase overall recall at the cost of compute.
1188+
1189+
[discrete]
1190+
[[dense-vector-knn-search-rescoring-script-score]]
1191+
====== Use a `script_score` query to rescore per shard
1192+
1193+
You can use this option when you want to rescore on each shard and want more fine-grained control on the rescoring
1194+
than the `rescore_vector` option provides.
1195+
1196+
Use rescore per shard with the <<query-dsl-knn-query, knn query>> and <<query-dsl-script-score-query, script_score query >>.
1197+
Generally, this means that there will be more rescoring per shard, but this can increase overall recall at the cost of compute.
12131198

12141199
[source,console]
12151200
--------------------------------------------------
@@ -1241,3 +1226,87 @@ POST /my-index/_search
12411226
<3> The number of candidates to use for the initial approximate `knn` search. This will search using the quantized vectors
12421227
and return the top 20 candidates per shard to then be scored
12431228
<4> The script to score the results. Script score will interact directly with the originally provided float32 vector.
1229+
1230+
1231+
[discrete]
1232+
[[exact-knn]]
1233+
=== Exact kNN
1234+
1235+
To run an exact kNN search, use a `script_score` query with a vector function.
1236+
1237+
. Explicitly map one or more `dense_vector` fields. If you don't intend to use
1238+
the field for approximate kNN, set the `index` mapping option to `false`. This
1239+
can significantly improve indexing speed.
1240+
+
1241+
[source,console]
1242+
----
1243+
PUT product-index
1244+
{
1245+
"mappings": {
1246+
"properties": {
1247+
"product-vector": {
1248+
"type": "dense_vector",
1249+
"dims": 5,
1250+
"index": false
1251+
},
1252+
"price": {
1253+
"type": "long"
1254+
}
1255+
}
1256+
}
1257+
}
1258+
----
1259+
1260+
. Index your data.
1261+
+
1262+
[source,console]
1263+
----
1264+
POST product-index/_bulk?refresh=true
1265+
{ "index": { "_id": "1" } }
1266+
{ "product-vector": [230.0, 300.33, -34.8988, 15.555, -200.0], "price": 1599 }
1267+
{ "index": { "_id": "2" } }
1268+
{ "product-vector": [-0.5, 100.0, -13.0, 14.8, -156.0], "price": 799 }
1269+
{ "index": { "_id": "3" } }
1270+
{ "product-vector": [0.5, 111.3, -13.0, 14.8, -156.0], "price": 1099 }
1271+
...
1272+
----
1273+
//TEST[continued]
1274+
//TEST[s/\.\.\.//]
1275+
1276+
. Use the <<search-search,search API>> to run a `script_score` query containing
1277+
a <<vector-functions,vector function>>.
1278+
+
1279+
TIP: To limit the number of matched documents passed to the vector function, we
1280+
recommend you specify a filter query in the `script_score.query` parameter. If
1281+
needed, you can use a <<query-dsl-match-all-query,`match_all` query>> in this
1282+
parameter to match all documents. However, matching all documents can
1283+
significantly increase search latency.
1284+
+
1285+
[source,console]
1286+
----
1287+
POST product-index/_search
1288+
{
1289+
"query": {
1290+
"script_score": {
1291+
"query" : {
1292+
"bool" : {
1293+
"filter" : {
1294+
"range" : {
1295+
"price" : {
1296+
"gte": 1000
1297+
}
1298+
}
1299+
}
1300+
}
1301+
},
1302+
"script": {
1303+
"source": "cosineSimilarity(params.queryVector, 'product-vector') + 1.0",
1304+
"params": {
1305+
"queryVector": [-0.5, 90.0, -10, 14.8, -156.0]
1306+
}
1307+
}
1308+
}
1309+
}
1310+
}
1311+
----
1312+
//TEST[continued]

docs/reference/search/search.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ not both. Refer to <<knn-semantic-search>> to learn more.
534534
(Optional, float)
535535
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=knn-similarity]
536536
537+
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=knn-rescore-vector]
538+
537539
====
538540

539541
[[search-api-min-score]]

0 commit comments

Comments
 (0)