pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
5+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
@ 2024-09-27 17:38 "metasidd (@metasidd)" <noreply+metasidd@github.com>
  0 siblings, 0 replies; 5+ messages in thread

From: metasidd (@metasidd) @ 2024-09-27 17:38 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

### 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.

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
@ 2024-09-27 17:54 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: jkatz (@jkatz) @ 2024-09-27 17:54 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@metasidd See https://github.com/pgvector/pgvector/issues/263 for a full discussion, including upcoming work to address this and https://github.com/pgvector/pgvector/issues/678 for the latest patches.

However, specifically here, `LIMIT` <= `hnsw.ef_search`, so you have to raise `hnsw.ef_search` to a larger value. The max value is `1000`. This will change with https://github.com/pgvector/pgvector/issues/678

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
@ 2024-09-27 18:47 ` "metasidd (@metasidd)" <noreply+metasidd@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: metasidd (@metasidd) @ 2024-09-27 18:47 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thank you so much! Removing the `order by` and removing the `match_count` did the job. 🚀

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
@ 2024-09-27 18:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2024-09-27 18:56 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @metasidd, removing the `order by` will no longer perform nearest neighbor search (or use the index). Check out the [troubleshooting docs](https://github.com/pgvector/pgvector?tab=readme-ov-file#why-are-there-less-results-for-a-query-after...) for more info on the initial issue.

^ permalink  raw  [nested|flat] 5+ messages in thread

* Re: [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW
@ 2024-09-27 18:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
  3 siblings, 0 replies; 5+ messages in thread

From: ankane (@ankane) @ 2024-09-27 18:56 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2024-09-27 18:56 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-09-27 17:38 [pgvector/pgvector] issue #684: Getting fewer results after indexing using HNSW "metasidd (@metasidd)" <noreply+metasidd@github.com>
2024-09-27 17:54 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-09-27 18:47 ` "metasidd (@metasidd)" <noreply+metasidd@github.com>
2024-09-27 18:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-27 18:56 ` "ankane (@ankane)" <noreply+ankane@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