From: sieudx (@sieudx) <noreply+sieudx@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #740: HNSW index returns different values when adding a where condition
Date: Sat, 28 Dec 2024 02:33:01 +0000
Message-ID: <gh-pgvector-pgvector-740@github.com> (raw)
My table: pg_embedding_question_answers
Column: embedding, document, cmetadata,id, document_id
Query 1:
SELECT document, cmetadata, (embedding::halfvec(1024) <-> :vector) AS distance
FROM pg_embedding_question_answers
WHERE (embedding::halfvec(1024) <-> :vector) <= :threshold
ORDER BY distance
LIMIT :limit
Query 2:
SELECT document, cmetadata, (embedding::halfvec(1024) <-> :vector) AS distance
FROM pg_embedding_question_answers
WHERE (embedding::halfvec(1024) <-> :vector) <= :threshold
AND document_id = :document_id
ORDER BY distance
LIMIT :limit
I execute 2 queries with the same input parameters
Vector version: 0.7.2
Result query 1: always returns 48 records no matter what limit value I set, and can't get the record with the smallest distance value

Result query 2: Returns the correct value according to limit and gets the value with the smallest distance

I don't know why, and how to fix the query so that query 1 can get the smallest value and the records return according to the parameter of limit