From 04b96506f55397171cdb9347873c0374532b9a21 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 3 Nov 2023 20:14:28 -0700 Subject: [PATCH] Added info on storing vectors with more precision [skip ci] --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 98749b4..d0235f6 100644 --- a/README.md +++ b/README.md @@ -425,6 +425,29 @@ and SELECT * FROM items WHERE model_id = 123 ORDER BY embedding::vector(3) <-> '[3,1,2]' LIMIT 5; ``` +#### Can I store vectors with more precision? + +You can use the `double precision[]` or `numeric[]` type to store vectors with more precision. + +```sql +CREATE TABLE items (id bigserial PRIMARY KEY, embedding double precision[]); + +-- use {} instead of [] for Postgres arrays +INSERT INTO items (embedding) VALUES ('{1,2,3}'), ('{4,5,6}'); +``` + +Use [expression indexing](https://www.postgresql.org/docs/current/indexes-expressional.html) to index (at a lower precision): + +```sql +CREATE INDEX ON items USING hnsw ((embedding::vector(3)) vector_l2_ops); +``` + +and query with: + +```sql +SELECT * FROM items ORDER BY embedding::vector(3) <-> '[3,1,2]' LIMIT 5; +``` + ## Troubleshooting #### Why isn’t a query using an index?