From: mauriciocirelli (@mauriciocirelli) <noreply+mauriciocirelli@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #259: Understanding HNSW + filtering
Date: Wed, 08 May 2024 14:54:33 +0000
Message-ID: <gh-pgvector-pgvector-259-c2100776676@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-259@github.com>
References: <gh-pgvector-pgvector-259@github.com>
Hi @williamhakim10
Thanks for replying.
In order to make sure that the amount of data would not compromise the tests, I had dumped the production database to my local machine for testing (the tables that were actually used for this development). So, by the time of those tests, the amount of data on the documents table was the very same between both databases. The only difference was the PG version.
Regarding the limits, the first planner suggests it has found 14 and limited to 10. However, PGAdmin shows only 3 records, while it shows 10 on the production database with the second query planner provided.
It seems to me that on PG15, it was first searching for the 10 nearest neighbors and applying the category/owner filter on them, returning just 3 results (because the other 7 did not match the category/owner filters); while on PG16, it was first searching the documents by category/owner and, then, finding the 10 nearest neighbors, returning 10 results.
In these KNN queries, the order of the WHERE conditions matters a lot, as we have been discussing on this thread.
Redis solves this issue by splitting the query in a two steps: a pre-filter query and a KNN query. This leads to the expected results. I am seeing this only on PG16: it is using the category/owner index first and filtering by distance later.
On PG15, it is using the embedding index first and applying the category/owner filter later. This leads to results that are not what we expect (usually less results).