From: jkatz (@jkatz) <noreply+jkatz@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
Date: Wed, 18 Sep 2024 13:56:47 +0000
Message-ID: <gh-pgvector-pgvector-671-c2358545791@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-671@github.com>
References: <gh-pgvector-pgvector-671@github.com>
@vinodhbalasubramanian As mentioned previously, this has to do with how the HNSW index is searching and returning rows. The search occurs on vector similarity, and it doesn't account for the filters. The filters are applied after the index is scanned. You can increase the number of results returned from the index by increasing `hnsw.ef_search`.
However, as stated (and discussed in #244), this is in general an area of active research around considering filters as part of a vector similarity search. If you have examples of the types of filters that you're running on your similarity search queries, along with overall dataset size and how much data you expect to be returned by a filter, that would help with determining how we want to proceed with supporting in-index filtering (aka "prefiltering").
> In some conditions Postgres doesn't use the B-tree index Ex: if the filter parameter has a condition that pulls heavy data (Ex: amount>1 and amount <100000), in this case Postgres doesn't use any of the B-tree indexes even though amount parameter is B-Tree index.
This may be because the PostgreSQL query planner believes a sequential scan is faster than an index scan for this case (and it very may well be, without knowing anything about your data).