From: hinthornw (@hinthornw) <noreply+hinthornw@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #722: Index support for filters beyond equivalence
Date: Mon, 02 Dec 2024 20:32:02 +0000
Message-ID: <gh-pgvector-pgvector-722@github.com> (raw)
Great work in 0.8.0 adding the iterative scan to resolve issues like [#678](https://github.com/pgvector/pgvector/issues/678), [#259](https://github.com/pgvector/pgvector/issues/259), and [#524](https://github.com/pgvector/pgvector/pull/524). I may be mistaken, but it seems the current index usage is still restricted to equivalence filters(?).
1. Is support for LIKE expressions (or other filters) in pre-filtering currently possible?
2. If not, is this feature planned for future releases (or something you'd invite a community contribution for)?
## Context
We want to support vector searches within namespaces. We have a btree index on a namespace/prefix column and want to retrieve rows by similarity within a namespace. When using the LIKE operator, the index is not used (a Sequential Scan is performed).
An analogy would be if you wanted to model a filesystem in postgres and wanted to search for documents in all subdirectories of `/Users/<user-id>` (Note: this is just one example; if it were limited to this then I'd create a partial index or partition by user-id).
```sql
SELECT *
FROM items
WHERE prefix LIKE 'prefix%'
ORDER BY embedding <-> query_vector ASC
LIMIT 10;
```
## Example Schema
```sql
CREATE TABLE items (
id SERIAL PRIMARY KEY,
prefix TEXT,
embedding vector(384)
);
CREATE INDEX IF NOT EXISTS items_prefix_idx ON items USING btree (prefix text_pattern_ops);
CREATE INDEX vector_idx ON items USING ivfflat (embedding vector_cosine_ops);
```
## Desired Behavior
The query should use both indices:
1. btree index for LIKE-based pre-filtering
2. vector index for similarity search
At present, I believe the recommendation is to either omit the index and just do a sequential scan after filtering by prefix or to use the vector index and then filter by prefix, depending on how the data is structured, though I could be missing something obvious here. Neither approach is that appetizing.
## Additional Information
- pgvector version: 0.8.0
- PostgreSQL version: 15, 16