Skip to content

fix(vector): use index metric and efc for post-build HNSW inserts - #90

Merged
adsharma merged 1 commit into
LadybugDB:mainfrom
adsharma:fix/hnsw-insert-metric
Sep 25, 2026
Merged

adsharma merged 1 commit into
LadybugDB:mainfrom
adsharma:fix/hnsw-insert-metric

Conversation

@adsharma

Copy link
Copy Markdown
Contributor

Summary

Rows inserted into a table after CREATE_VECTOR_INDEX (via CREATE/MERGE through initInsertState, COPY FROM through finalize, and embedding updates through initUpdateState) had their HNSW neighbours chosen by cosine distance regardless of the index's configured metric.

Root cause

OnDiskHNSWIndex::HNSWInsertState built its HNSWSearchState with a default-constructed HNSWIndexConfig{} (metric = cosine) and default QueryHNSWConfig{} (efs = 200). insertInternal -> searchNNInUpperLayer / insertToLayer -> searchKNNInLayer all score candidates with searchState.metricFunc, so post-build inserts silently used cosine (and default ef) on l2 / l2sq / dotproduct indexes over non-normalized vectors, lowering recall for those rows. The initial build (InMemHNSWIndex), shrink (shrinkForNode uses config.metric), and query path (real index config) were already correct.

Fix

  • HNSWInsertState now takes const HNSWIndexConfig& and forwards it to HNSWSearchState instead of HNSWIndexConfig{}.
  • New getInsertQueryConfig() helper maps the index's efc onto the insert search's efs, so custom efc is honored for incremental inserts too.
  • Both producers (initInsertState, finalize) pass through config.

Testing

  • Full vector e2e suite: 87/87 passed (E2E_TEST_FILES_DIRECTORY=extension ... e2e_test --gtest_filter='vector~*').
  • New code is clang-format clean.

Related to LadybugDB/ladybug#1023

OnDiskHNSWIndex::HNSWInsertState built its search state with a
default-constructed HNSWIndexConfig (metric = cosine) and default
QueryHNSWConfig (efs = 200), so rows inserted after CREATE_VECTOR_INDEX
(CREATE/MERGE via initInsertState, COPY FROM via finalize, embedding
updates via initUpdateState) picked HNSW neighbours by cosine distance
regardless of the index metric, silently lowering recall on l2/l2sq/
dotproduct indexes over non-normalized vectors. Thread the index config
through and map efc onto the insert search ef.

Related to LadybugDB/ladybug#1023
@adsharma
adsharma merged commit e14664b into LadybugDB:main Sep 25, 2026
2 checks passed
@adsharma
adsharma deleted the fix/hnsw-insert-metric branch September 25, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant