pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #648: Custom scoring with HNSW index
5+ messages / 3 participants
[nested] [flat]
* [pgvector/pgvector] issue #648: Custom scoring with HNSW index
@ 2024-08-19 16:11 "fede-bello (@fede-bello)" <noreply+fede-bello@github.com>
0 siblings, 0 replies; 5+ messages in thread
From: fede-bello (@fede-bello) @ 2024-08-19 16:11 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I am trying to achieve a search where some tuples have more weight than others. Let's say I have my table:
```
Document:
- id
- embedding
- is_important : boolean
```
If the document is important, I would like to give more weight to it at the moment of searching.
I am working with HNSW indexes and I have ~4 M tuples in the database, so needs to use the index or else it takes too long. My parameters are: `ef_construction= 256`, `m=32` and `ef_search = 256`.
So, first I tried something like:
```
SELECT "t1"."id",
(CASE WHEN ("t1"."is_important") THEN 1.2 ELSE 1 END * ("t1"."embedding" <#> <vector_search_embedding>)) AS "weighted_score"
FROM "Document" AS "t1"
WHERE ((CASE WHEN ("t1"."is_important") THEN 1.2 ELSE 1 END * ("t1"."embedding" <#> <vector_search_embedding>)) < -0.5)
ORDER BY (CASE WHEN ("t1"."is_important") THEN 1.2 ELSE 1 END * ("t1"."embedding" <#> <vector_search_embedding>)) ASC
LIMIT 50
```
So, ordering by my custom score. That, of course, did not work (didn't use the index)
Then, I tried using a subquery to find the most similar documents and then ordering by my custom score:
```
SELECT "t1"."id",
(CASE WHEN ("t1"."isImportant") THEN 1.2 ELSE 1 END * ("t1"."summaryEmbedding" <#> '<vector_search_embedding')) AS "weighted_score"
FROM "Document" AS "t1"
WHERE ("t1"."id" IN (
SELECT "t1"."id"
FROM "Document" AS "t1"
WHERE ((CASE WHEN ("t1"."isImportant") THEN 1.2 ELSE 1 END * ("t1"."summaryEmbedding" <#> '<vector_search_embedding')) < -0.5))
ORDER BY ("t1"."summaryEmbedding" <#> '<vector_search_embedding') ASC
LIMIT 128)
ORDER BY weighted_score ASC
LIMIT 50
```
But its not ideal since there are important documents that might have a higher weighted score but get filtered by the limit in the first subquery.
So, my question is: is there any workaround to make this custom scoring efficiently? I could do several queries - one for important documents, one for not important documents and then merging and ordering this but I am looking for something more efficient.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #648: Custom scoring with HNSW index
@ 2024-08-19 18:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2024-08-19 18:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @fede-bello, I think the subquery approach above will be the most efficient, but the merging approach could provide better recall (I'd recommend measuring it for both). Also, the merging approach should be fast if properly indexed and can be parallelized.
Finally, I'd recommend seeing if `m = 16` works for your data, as `m` has a large impact on search time.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #648: Custom scoring with HNSW index
@ 2024-08-19 18:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2024-08-19 18:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #648: Custom scoring with HNSW index
@ 2024-08-20 15:45 ` "fede-bello (@fede-bello)" <noreply+fede-bello@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: fede-bello (@fede-bello) @ 2024-08-20 15:45 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Just as an fyi in case someone is trying to achieve the same. A bit counter intuitive for me, but the merging approach was as efficient (and even more) than the subquery approach, even without parallelizing . It also had a **much** better recall.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #648: Custom scoring with HNSW index
@ 2024-08-20 15:49 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: jkatz (@jkatz) @ 2024-08-20 15:49 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@fede-bello If you can, I suggest sharing/blogging about it!
^ permalink raw [nested|flat] 5+ messages in thread
end of thread, other threads:[~2024-08-20 15:49 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-08-19 16:11 [pgvector/pgvector] issue #648: Custom scoring with HNSW index "fede-bello (@fede-bello)" <noreply+fede-bello@github.com>
2024-08-19 18:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-19 18:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-08-20 15:45 ` "fede-bello (@fede-bello)" <noreply+fede-bello@github.com>
2024-08-20 15:49 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox