Tune vector search accuracy and speed: DiskBBQ

Updated at:

The vector indexes of Agentic ES are built on the disk-native quantized index bbq_disk (DiskBBQ). At query time, it first performs a fast coarse scan over the 1-bit compressed vector clusters on disk (significantly reducing memory usage and disk reads), then reads the original floating-point vectors of the selected candidates for exact rescoring (fine ranking). By tuning visit_percentage and rescore_vector.oversample, you can strike the best balance between recall and query latency for your needs.

Note

All API examples in this topic provide CONSOLE / CURL / GO / PYTHON / JAVASCRIPT / JAVA code tabs (CONSOLE requests can be copied directly into Kibana Dev Tools), reusing the Quick start's demo-knowledge-chunks collection and its sample data.

1. How it works and core tuning parameters

You can think of the DiskBBQ query process as finding the k most relevant books in a library:

  1. Step 1, fast coarse scan (controlled by visit_percentage or num_candidates): first look at the compressed catalog (the 1-bit compressed vector clusters on disk) to quickly shortlist the most likely relevant books, greatly reducing disk reads.

  2. Step 2, exact rescoring (controlled by rescore_vector.oversample): read the original floating-point vectors of the shortlisted candidates, compute exact distances one by one, and finally return the most accurate top k results. For example, to return k = 10 results with the default oversample = 10.0, the system first coarse-scans 10 × 10 = 100 candidates, rescores them exactly, then truncates to the top 10.

Beyond the basic k and num_candidates parameters, DiskBBQ provides two parameters that directly control the "coarse-scan scope" and the "rescoring precision":

Parameter

Stage

Default

Description

visit_percentage

(mapping: default_visit_percentage)

Coarse scan (proportion of vector clusters scanned)

0.0 (0 to 100)

0: retrieval uses num_candidates; when > 0, vector clusters are scanned by that percentage — a larger value yields higher recall and higher latency.

rescore_vector.oversample

Fine ranking (multiplier for rescoring original vectors)

10.0

Rescores k × oversample candidates exactly; queries support 0 (disabled) or >= 1.0 (the mapping cap is 10.0).

Two key effective rules:

  • Parameter precedence: per-query parameters take precedence over mapping defaults; when the effective visit_percentage > 0, num_candidates no longer determines the scan ratio (passing an explicit 0 in the query switches back to num_candidates mode).

  • Lower bound on candidate size: when visit_percentage = 0 and rescoring is enabled, the actual candidate size is max(num_candidates, k × oversample). Therefore, to reduce latency, lower oversample and make sure num_candidates is not greater than k × oversample (or do not pass num_candidates explicitly) — merely reducing num_candidates below k × oversample has no effect, and when an excessively large num_candidates is passed explicitly, lowering only oversample reduces the number of rescored items but does not shrink the coarse-scan scope.

Tuning reference for common scenarios:

Business need

Recommended starting configuration

Expected effect

Balanced (default starting point)

Do not pass visit_percentage and keep the default oversample = 10.0

Balances low latency with relatively high recall; suitable as a starting point for most workloads.

Pursue high recall (knowledge-base Q&A, legal search)

Increase visit_percentage (e.g. 5.0 → 12.0), and if needed configure oversample = 15.0

Widens the coarse-scan and rescoring scope and reduces missed recall, with a moderate latency increase (visit_percentage = 12; for measured results see Performance reference).

Pursue ultra-low latency (high-concurrency first-pass filtering for recommendations)

Lower rescore_vector.oversample (e.g. 3.0-5.0) and either omit or constrain num_candidates <= k × oversample

Shrinks the disk reads and rescoring work of both stages and lowers P99 latency, with recall dropping accordingly.

Note

The top-level knn, retriever.knn, and the retriever.rrf retriever's nested knn clause all support passing visit_percentage and rescore_vector. For vector fields and basic kNN syntax, also refer to the Elasticsearch documentation: Dense vector and kNN search.

2. Explicitly specify visit_percentage and rescore_vector

CONSOLE

POST /demo-knowledge-chunks/_search?_slice=kb-a&allow_partial_search_results=false
{
  "knn": {
    "field": "embedding",
    "query_vector": [0.80, 0.12, 0.05, 0.03],
    "k": 5,
    "visit_percentage": 10.0,
    "rescore_vector": {
      "oversample": 15.0
    },
    "filter": { "term": { "category": "after-sales" } }
  },
  "_source": ["title", "category"]
}

CURL

curl -u "$ES_USER:$ES_PASSWORD" -X POST \
  "$ES_ENDPOINT/demo-knowledge-chunks/_search?_slice=kb-a&allow_partial_search_results=false" \
  -H 'Content-Type: application/json' \
  --data-binary @- <<'JSON'
{
  "knn": {
    "field": "embedding",
    "query_vector": [0.80, 0.12, 0.05, 0.03],
    "k": 5,
    "visit_percentage": 10.0,
    "rescore_vector": {
      "oversample": 15.0
    },
    "filter": { "term": { "category": "after-sales" } }
  },
  "_source": ["title", "category"]
}
JSON

GO

res, err := client.Search().
	Index("demo-knowledge-chunks").
	Routing("kb-a").
	AllowPartialSearchResults(false).
	Knn(&types.KnnSearch{
		Field:           "embedding",
		QueryVector:     []float32{0.80, 0.12, 0.05, 0.03},
		K:               some.Int(5),
		VisitPercentage: some.Float32(10.0),
		RescoreVector:   &types.RescoreVector{Oversample: 15.0},
		Filter:          []types.Query{{Term: map[string]types.TermQuery{"category": {Value: "after-sales"}}}},
	}).
	SourceIncludes_("title", "category").
	Do(ctx)
if err != nil {
	log.Fatal(err)
}
fmt.Println(res.Hits.Hits)

PYTHON

res = client.search(
    index="demo-knowledge-chunks",
    routing="kb-a",
    allow_partial_search_results=False,
    knn={
        "field": "embedding",
        "query_vector": [0.80, 0.12, 0.05, 0.03],
        "k": 5,
        "visit_percentage": 10.0,
        "rescore_vector": {"oversample": 15.0},
        "filter": {"term": {"category": "after-sales"}},
    },
    source=["title", "category"],
)
print(res["hits"]["hits"])

JAVASCRIPT

const res = await client.search({
  index: 'demo-knowledge-chunks',
  routing: 'kb-a',
  allow_partial_search_results: false,
  knn: {
    field: 'embedding',
    query_vector: [0.80, 0.12, 0.05, 0.03],
    k: 5,
    visit_percentage: 10.0,
    rescore_vector: { oversample: 15.0 },
    filter: { term: { category: 'after-sales' } },
  },
  _source: ['title', 'category'],
})
console.log(res.hits.hits)

JAVA

SearchResponse<Map> res = client.search(s -> s
    .index("demo-knowledge-chunks")
    .routing("kb-a")
    .allowPartialSearchResults(false)
    .knn(k -> k
        .field("embedding")
        .queryVector(0.80f, 0.12f, 0.05f, 0.03f)
        .k(5)
        .visitPercentage(10.0f)
        .rescoreVector(rv -> rv.oversample(15.0f))
        .filter(f -> f.term(t -> t.field("category").value("after-sales"))))
    .source(src -> src.filter(f -> f.includes("title", "category"))),
    Map.class);
System.out.println(res.hits().hits());

3. Set defaults in the collection mapping

If you want all queries on a vector field to use a higher scan ratio by default, or to change the default rescoring multiplier (the mapping-level oversample supports 0 or 1.0-10.0, and already defaults to the cap of 10.0; for values above 10.0, pass an override in the individual query), specify them directly in the mapping when creating the Collection:

"embedding": {
  "type": "dense_vector",
  "dims": 1024,
  "index": true,
  "similarity": "cosine",
  "index_options": {
    "type": "bbq_disk",
    "default_visit_percentage": 5.0,
    "rescore_vector": { "oversample": 10.0 }
  }
}