From: jeff-davis (@jeff-davis) <noreply+jeff-davis@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #259: Understanding HNSW + filtering
Date: Tue, 07 Nov 2023 04:58:34 +0000
Message-ID: <gh-pgvector-pgvector-259-c1797825115@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-259@github.com>
References: <gh-pgvector-pgvector-259@github.com>
> Hi @jeff-davis, thanks for the proposal.
> For the approach, I'm hoping we can avoid the complexity of multiple graphs by using HQANN. I've started some prototyping on this and think it can be done with a single change to the on-disk format (reinterpreting `vec` as an `IndexTuple` when `IndexRelationGetNumberOfAttributes(index) > 1`).
I was originally planning to propose something related to HQANN, so I look forward to reading your design. I looked at it as a way to simplify some engineering challenges (one type of index, one disk structure, etc.), but what I intended to propose was essentially the same (mathematically) as the btree approach that I did propose. The idea I had was to turn scalar attributes into a number by hashing, and ensure that the scalar hash always dominated the vector distance in the overall distance calculation, which is a simplification of the HQANN approach. That would make the top few levels look like a skip list, and lower levels look like a proper HNSW, but it would all be in one HNSW graph with one entry point.
The reason I ended up proposing the btree approach is that it seemed simpler to explain. Whether the end result is simpler using a btree or HQANN will probably revolve around the details, so I can't say for sure that one is simpler than the other. I am happy with either approach at a high level.
> I also think it may allow for searching any combination of the attributes (`(V, A1)` and `(V, A2)`), not just the leading ones.
That would be possible in either approach. The above proposal (with slight modification) allows for enumerating all subsets of scalar attributes, and using similar partial tuples with `*` markers in the btree keys.
The challenge is not so much in storage or execution, but in planning, I believe. We either need to make the planner aware that the index will work for any subset, or we need to inject such paths ourselves. To do this "right" also involves handling parameterized paths. I believe we can and should do all of this, but it's extra work and we can start simply (i.e. prefixes only, then combinations, then parameterized combinations).
> Will spend some more time on it this week to try and have something to share.
Great!