Skip to content

Commit e5f5c9b

Browse files
docs: explain relaxed ordering on distance results (#218)
closes #212 #101 --------- Signed-off-by: Alejandro Do Nascimento <alejandrodnm@gmail.com> Co-authored-by: James Guthrie <JamesGuthrie@users.noreply.github.com>
1 parent 4004865 commit e5f5c9b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,25 @@ COMMIT;
358358
* Null labels are treated as empty arrays
359359
* Null values in label arrays are ignored
360360

361+
## ORDER BY vector distance
362+
363+
pgvectorscale's diskann index uses relaxed ordering which allows results to be
364+
slightly out of order by distance. This is analogous to using
365+
[`iterative scan with relaxed ordering`][pgvector-iterative-index-scan] with
366+
pgvector's ivfflat or hnsw indexes.
367+
368+
If you need strict ordering you can use a [materialized CTE][materialized-cte]:
369+
370+
```sql
371+
WITH relaxed_results AS MATERIALIZED (
372+
SELECT id, embedding <=> '[1,2,3]' AS distance
373+
FROM items
374+
WHERE category_id = 123
375+
ORDER BY distance
376+
LIMIT 5
377+
) SELECT * FROM relaxed_results ORDER BY distance;
378+
```
379+
361380
## Get involved
362381

363382
pgvectorscale is still at an early stage. Now is a great time to help shape the
@@ -375,3 +394,5 @@ Timescale is a PostgreSQL cloud company. To learn more visit the [timescale.com]
375394
[pgvector]: https://github.com/pgvector/pgvector/blob/master/README.md
376395
[rust-language]: https://www.rust-lang.org/
377396
[pgvector-install]: https://github.com/pgvector/pgvector?tab=readme-ov-file#installation
397+
[pgvector-iterative-index-scan]: https://github.com/pgvector/pgvector?tab=readme-ov-file#iterative-index-scans
398+
[materialized-cte]: https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-CTE-MATERIALIZATION

0 commit comments

Comments
 (0)