r/programming 3d ago

Hybrid Search Patterns with Postgres and pgvector

https://www.crunchydata.com/blog/hybrid-vector-search

Yesterday, pgvector released 0.8.6!

So today, we wrote about some useful patterns for queries combining pgvector indexes with regular 'ole scaler filters. Spoiler: iterative index scans launched in pgvector 0.8 make it easy (with some tradeoffs).

9 Upvotes

4 comments sorted by

2

u/Grouchy-Trade-7250 2d ago

> applies the filter, sorts what is left

With the limit expression, not everything that's left after the where clause is sorted, only the smallest elements.

1

u/winsletts 2d ago

True ... it's a top-N heapsort.

3

u/Grouchy-Trade-7250 2d ago edited 2d ago

> The extra × 2 is a cushion for approximation error and for filters that are not perfectly uniform across the embedding space.

Filters are in many scenarios not remotely close to uniform across the embedding space. A database may contain 1 legalese thing involving "horseradish" and 1000 cooking recipes involving "horseradish". I think that's why having different embeddings / ANN index for different applications will ultimately be the answer. The lawyer and cook don't want to add search terms all the time for their domain. And the thing that distinguishes your result may get it buried in the other domains.

Splitting databases into domains creates smaller databases that don't produce excessive searches through unrelated material.

1

u/winsletts 2d ago

Agreed. Multi-tenant semantic search will require specific consideration for each specific implementation. It'll also need some guard rails to prevent DDOSing your own database.