bug: Empty in-memory LSH store never initializes its index after adding documents
- I have looked for existing issues (including closed) about this
Bug Report
An InMemoryVectorStore configured with IndexStrategy::LSH only creates its index during build() if embeddings already exist. When the store is built empty and documents are added later, the documents enter the backing map but the index remains absent. Every search then takes the LSH index not initialized, falling back to brute force search path. The store is usable, which makes the problem less obvious, but it never applies the strategy the caller selected.
This is a common lifecycle for applications that create a store at startup and ingest documents afterward. As the collection grows, those applications keep scanning all stored vectors, with search cost tied to the whole collection rather than LSH candidates. The behavior applies to add_documents, add_documents_with_ids, and add_documents_with_id_f, which all use the same insertion method.
There is a lifecycle-dependent difference here. A store built with vectors can create an LSH index from those vectors. A store built with the same LSH settings and later given the same vectors cannot, solely because no dimension was available at construction time. Both stores advertise the same strategy, but their search paths diverge after ingestion.
Reproduction
The following unit test can be added in crates/rig-core/src/vector_store/in_memory_store/tests.rs, where the sibling module can inspect the private index:
#[test]
fn empty_lsh_store_initializes_after_insertion() {
let mut store = InMemoryVectorStore::<String>::builder()
.index_strategy(IndexStrategy::LSH {
num_tables: 5,
num_hyperplanes: 10,
})
.build();
assert!(store.lsh_index.is_none());
store.add_documents([(
"first".to_owned(),
vec![Embedding {
document: "first".to_owned(),
vec: vec![1.0, 0.0],
}],
)]);
assert!(store.lsh_index.is_some());
}The last assertion fails on the current implementation. from_builder gets one opportunity to initialize an index, before any later insertion. insert_document only adds vectors to an index that already exists; it never creates one. A query after insertion also emits the fallback warning.
The sequence is worth testing with empty inputs as well. Adding a document with no embeddings should not create a dimensionless index. Adding a zero-dimensional embedding should still leave the index deferred. Once a valid vector arrives, the index should be created and should contain that vector; any usable vectors already in the store should be replayed into it. The query path can then use LSH candidates rather than the fallback scan.
Expected behavior
An empty LSH store should defer index creation until it receives its first non-empty embedding. The first usable embedding supplies the vector dimension. Initialization should index that document and any usable vectors stored earlier. Empty embedding lists and zero-dimensional vectors should leave initialization deferred. Stores that already have an index should continue updating it on insertion.
Screenshots
Not applicable; the behavior is visible in the store state and search path.
Additional context
The index transition belongs in the shared document insertion path so all three addition APIs behave consistently. LSH search is approximate, so activating the configured index can change candidate sets compared with the old exhaustive fallback. Applications requiring exhaustive search can explicitly choose IndexStrategy::BruteForce.
The public store API does not need another initialization call. The store already has the selected strategy and its LSH parameters; the first usable embedding supplies the missing dimension. That is enough information to complete initialization at insertion time, without asking callers to rebuild the store after ingestion.
Source: 0xPlaygrounds/rig