From: ankane (@ankane) <noreply+ankane@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #838: accessing IVFFLAT lists
Date: Thu, 15 May 2025 11:09:37 +0000
Message-ID: <gh-pgvector-pgvector-838-c2883438325@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-838@github.com>
References: <gh-pgvector-pgvector-838@github.com>
Hi @tansaku, there isn't a way to query it directly (#206), but you could use an approach like the one in this script:
```python
import psycopg
conn = psycopg.connect(dbname='mydb', autocommit=True)
conn.execute('SET enable_seqscan = off');
conn.execute('SET ivfflat.probes = 1');
res = conn.execute('SELECT id FROM items').fetchall()
remaining_ids = set([r[0] for r in res])
lists = []
while remaining_ids:
res = conn.execute('SELECT id FROM items ORDER BY embedding <=> (SELECT embedding FROM items WHERE id = %s)', (next(iter(remaining_ids)),)).fetchall()
ids = [r[0] for r in res]
lists.append(ids)
print(f'Found {len(lists)}')
for id in ids:
remaining_ids.remove(id)
for i, ids in enumerate(lists):
print(f'List {i + 1}')
print(ids)
```
(takes under 5 seconds on my machine with 500k, 1536-dimension vectors and 500 lists)