That's promising, is there a way to integrate this with other filters in the SQL query? Scenario: let's say I have documents and each one has a timestamp and an embedding vector. I should be able to write a query like this:
select * from documents where timestamp=today() order by similarity(embedding, query_vector)
In this case the query planner should be smart enough such that it first filters by timestamp and within those records remaining it does the HNSW search. I'm unsure whether SQLite's extension interface is flexible enough to support this.
I don't think so. You can have unindexed columns on fts tables but I don't think you can directly combine non-fts query filters in that way.
You can do it, but you need to manually filter from the main table then subquery the fts index, or use a join, etc. It's powerful but the interface isn't as friendly as it could be in that way.
The sqlite extension system is powerful enough that an extension could unify the behaviors as you describe. It's just not how the fts5 extension interface is exposed, and afaik no one has written an extension on top of it to make it easier to use.
An SQLite virtual table can specify a “cost” to a particular query plan, you you could weight your index search higher and discourage SQLite from using it to filter many rows. The tuning process works by specifying a number of “disk reads” or estimated rows for a particular query plan.
> I'm unsure whether SQLite's extension interface is flexible enough to support this.
I think it is, if I've understood your requirements correctly. e.g. from the datasette-faiss docs:
with related as (
select value from json_each(
faiss_search(
'intranet',
'embeddings',
(select embedding from embeddings where id = :id),
5
)
)
)
select id, title from articles, related
where id = value
select * from documents where timestamp=today() order by similarity(embedding, query_vector)
In this case the query planner should be smart enough such that it first filters by timestamp and within those records remaining it does the HNSW search. I'm unsure whether SQLite's extension interface is flexible enough to support this.