pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: knizhnik (@knizhnik) <noreply+knizhnik@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #259: Understanding HNSW + filtering
Date: Wed, 20 Sep 2023 05:42:04 +0000
Message-ID: <gh-pgvector-pgvector-259-c1727006213@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-259@github.com>
References: <gh-pgvector-pgvector-259@github.com>

The problem we are trying to solve is not specific for HNSW/IVFFLAT indexes. It is universal problem of multidimensional search with heterogeneous  dimensions. Imagine that you find the nearest restaurant with the highest rating and preferable Italian cuisine. How to compare distance with rating? Is it better to go 100m more to reach restaurant with score 4.5 instead of 3.7? Or French restaurant 0.5km closer is better? Looks like CS is not able to provide something better for such multicriteria search rather than K-D Tree. But it is not self balanced and not working for large number of dimensions...

Postgres provides GIN (Generalised Inverted Index) which can be used for full text search. It idea is very similar with IVFFLAT. Here we also have the problem with multidimensional search (assume that we want to find document containing some keywords published before some date). My former colleagues proposed VODKA index: https://www.pgcon.org/2014/schedule/events/696.en.html which is actually two dimensional index, using B-Tree for posting lists. But it is still not able to solve all problems with multidimensional search...

Right now if you need to combine several search  criteria, Postgres can propose only two approaches:
1. Filtered index scan (you perform index search using one criteria and filter records matching another criteria).
2. Bitmap scan (index scans construct bitmap which you can then intersect or union and use for heap scan)

First approach is not working well for HNSW because it returns limited (K) number of results.
Second approach is not possible because we need to return records undesired order. It will be nice if we can test bitmap inside index AM, but right now it is not possible. 

So how can we address this problem with HNSW/IVFFLAT?
1. Provide iterator which returns all recordist distance descending order (as it is done in Postgres GIST KNN implementation). Now sure if it is possible for HNSW...
2. Somehow push down search predicate to HNSW index so it can filter records while search. Postgres index AM doesn't support it. Certainly we can define some executors hook and analyze query plan, trying to locate our indexscsan node pin this tree and extra top level predicates. But it is very non trivial and hacky. 
3. Propose some combined index structure (like K-D tree) which will be able to perform such multidimensional search. It seems to be most efficient solution. But at the same time - most difficult to implement. 
4. Somehow managed to add extra attributes (like `category_id`) to the embedding vector.
5. Repeat search with increased K value to provide more candidates. This is most compromise approach implemented in my PR. 

And last note: most likely it will be necessary to perform search with and without extra criteria. For example, we can look for documents of particular category or perform search through all documents. Maintaining two different indexes seems be be quite expensive solution.


view thread (90+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-259-c1727006213@github.com>
Permalink:  ../gh-pgvector-pgvector-259-c1727006213@github.com/

 · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox