From: metasidd (@metasidd) <noreply+metasidd@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
Date: Fri, 27 Sep 2024 17:38:16 +0000
Message-ID: <gh-pgvector-pgvector-684@github.com> (raw)
### Setup:
I'm working on creating a word database with vectors assigned for each word (a row). The table setup is quite simple and looks something like this. Currently, I have around 100,000 rows in there, but I could see it ballooning up to 1,000,000 rows soon.
```
create table
public.word_vectors_openai (
raw_word text not null, // usually a maximum of 30-40 characters.
processed_word text null, // usually a maximum of 30-40 characters.
embedding extensions.vector null
) tablespace pg_default;
```
These vectors are populated with OpenAI's large text model, limiting to 512 dimensions.
Then I use vector cosine similarity to fetch the close vectors based on a set of variables. I usually set the `match_count` to 2000.
```
select
word_vectors_openai.raw_word,
1 - (word_vectors_openai.embedding <=> query_embedding) as similarity
from word_vectors_openai
where word_vectors_openai.embedding <=> query_embedding < 1 - match_threshold
order by word_vectors_openai.embedding <=> query_embedding
limit match_count;
```
I then indexed this table using HSNW with these parameters
```
CREATE INDEX word_vectors_index ON public.word_vectors_openai USING hnsw (embedding vector_cosine_ops) WITH (m = 64, ef_construction = 256)
```
Finally, I used a Supabase Edge Function to call this database function. The function requests a `match_count` of 2,000 to 3,000 words. The rough use case is... **"give me 3,000 words related to `vehicles`"**.
### Problem
- **👍 Before Indexing: 3000 results accurately shown**
- **👎 After Indexing: 40 results only**
### Questions
- Do the `ef` and `m` numbers impact how many results are returned?
- Why would the number of results go down after indexing? Do I need to make changes in my database function?
- Is there anything else I may have overlooked?
Any help would be appreciated. Thank you.