From: nikhilshinday (@nikhilshinday) <noreply+nikhilshinday@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #394: Question regarding HNSW
Date: Wed, 03 Jan 2024 15:17:10 +0000
Message-ID: <gh-pgvector-pgvector-394@github.com> (raw)
I'm having trouble finding the right parameters for an hnsw index. Here's the rough outline of my table ontology:
```
vectorized_document {
id: bigint pk
text: text
embedding: vector(n)
index: vectorized_document_embedding_idx hnsw (embedding vector_cosine_ops)
}
vectorized_document_relationship {
vectorized_document_id: bigint fk
call_monologue_id: bigint fk
call_summary_id: bigint fk
}
call_monologue {
id: bigint pk
text: text
call_id: bigint fk
}
call_summary {
id: bigint pk
text: text
call_id: bigint fk
}
call {
id: bigint pk
organization_id: bigint fk
}
```
Users belong to an organization id, which means that we have a permission filter in all queries that goes something like:
```sql
select vectorized_document.* from vectorized_document
left join vectorized_document_relationship
left join call_monologue
left join call_summary
where
call_monologue.call_id in (select id from call where organization_id = session.user.organization_id
or
call_summary.call_id in (select id from call where organization_id = session.user.organization_id
order by embedding <> '[....n long vector]';
```
given what I know about HNSW and the postgres query planner, it seems like, roughly, the query planner would filter down to a subset of vectorized_document IDs based on the access query and then do the ordering on those vectorized document based on the embedding column's HNSW index. The problem I see is that, since the index is built up without knowledge of the access pattern that is being used, there will be less qualified neighbors at each "small world" of vectors, which means we're _really_ likely to miss out on vectors and less results will be returned. I validated that for the above query, values are returned when I don't use semantic vector-based ordering, and values aren't returned when I use that ordering.
My question is: would jacking up the `m` parameter make sense here? My guess is that it would work, but that it would also create a ton of unnecessary neighbors for vectors that will never be compared to each other.
Broader picture, is there _any_ index that would be appropriate here? It seems like a lot of these indexes are based on the assumption that the access pattern for a table will always be a direct query.