From: ankane (@ankane) <noreply+ankane@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #259: Understanding HNSW + filtering
Date: Fri, 20 Sep 2024 00:53:20 +0000
Message-ID: <gh-pgvector-pgvector-259-c2362500744@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-259@github.com>
References: <gh-pgvector-pgvector-259@github.com>
Hi all, just posted the initial plan for 0.8.0 (#677), which includes an option for iterative index scans.
```sql
SET hnsw.iterative_scan = on;
```
If too few results from the initial index scan match the filters, the scan will continue until enough matching results are found.
A drawback of this approach is results may not be returned in strict order (each iteration of results will be ordered, but it's possible for an iteration to find results with a lower distance than previous iterations). You can use a CTE if strict ordering is required.
```sql
WITH approx_order AS MATERIALIZED (
SELECT id, embedding <-> $1 AS distance FROM items WHERE ... ORDER BY distance LIMIT 10
) SELECT * FROM approx_order ORDER BY distance;
```
Will share another update once it's merged for anyone who would like to help test.
There are also plans to continue exploring composite indexes (which incorporate filters into index construction) for better performance.