Skip to content

Rebuild HNSW vector index when dimension or distance function changes#19046

Open
Akanksha-kedia wants to merge 1 commit into
apache:masterfrom
Akanksha-kedia:feat/rebuild-vector-index-on-config-change
Open

Rebuild HNSW vector index when dimension or distance function changes#19046
Akanksha-kedia wants to merge 1 commit into
apache:masterfrom
Akanksha-kedia:feat/rebuild-vector-index-on-config-change

Conversation

@Akanksha-kedia

Copy link
Copy Markdown
Contributor

Description

Extends VectorIndexHandler to detect and rebuild a HNSW vector index when the vector dimension or distance function changes in the table config — mirroring the same pattern used for bloom filter (#18898) and JSON index (#18920) rebuilds.

Problem

Previously, if a table's vector index config changed (e.g. dimension or distance function), the existing index would be silently reused without rebuilding:

  • A changed dimension produces a structurally incompatible index that will fail or return garbage results.
  • A changed distance function silently returns wrong query results (wrong similarity metric applied).

Neither failure was detectable without the stored build-time config.

Solution

Write a lightweight metadata file (.vector.hnsw.metadata) alongside the HNSW Lucene index directory at seal() time, recording:

  • vectorDimension (int)
  • vectorSimilarityFunction (Lucene VectorSimilarityFunction name)

VectorIndexHandler.needUpdateIndices() reads this file and triggers a rebuild when either field differs from the current table config.

Backward compatibility: Legacy segments without a metadata file skip the check and receive the file on their next rebuild — no forced migration.

Files changed

  • HnswVectorIndexCreator – writes .vector.hnsw.metadata at seal time
  • VectorIndexHandler – reads metadata file and triggers rebuild on mismatch
  • VectorIndexUtils – helper for reading/writing the metadata file
  • V1Constants – adds VECTOR_HNSW_METADATA_FILE_EXTENSION constant
  • VectorIndexHandlerTest – unit tests for all rebuild/skip branches

Test plan

  • New VectorIndexHandlerTest covers: dimension change → rebuild, distance-function change → rebuild, no change → skip, missing metadata → skip (backward compat), metadata present + matching → skip
  • Existing vector index integration tests pass

Write a lightweight metadata file (.vector.hnsw.metadata) alongside
the HNSW Lucene index directory at seal() time, recording the vector
dimension and Lucene VectorSimilarityFunction used to build the index.

VectorIndexHandler.isVectorConfigChanged() reads this file and triggers
a rebuild when dimension or distance function differs from the current
table config.  Legacy segments without a metadata file skip the check
(backward-compatible) and get the file on their next rebuild.

Changing dimension produces a structurally incompatible index; changing
the distance function silently returns wrong query results.  Neither
failure is detectable without the stored config, so both require a
deterministic rebuild rather than silent misuse of the wrong index.

L2 and EUCLIDEAN are treated as equivalent (both map to Lucene's
VectorSimilarityFunction.EUCLIDEAN) so no spurious rebuild occurs when
switching between the two aliases.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@Akanksha-kedia

Copy link
Copy Markdown
Contributor Author

cc @Jackie-Jiang @richardstartin @npawar — would appreciate a review when you get a chance!

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