From 2d24d65f1c2825b1afa757bb23df520a1c1db0a8 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 4 Apr 2024 00:00:54 -0700 Subject: [PATCH] Added sparse vectors section [skip ci] --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index a96c493..53e727f 100644 --- a/README.md +++ b/README.md @@ -494,6 +494,28 @@ SELECT * FROM ( ) ORDER BY embedding <=> '[1,-2,3]' LIMIT 5; ``` +## Sparse Vectors + +*Unreleased* + +Use the `sparsevec` type to store sparse vectors + +```sql +CREATE TABLE items (id bigserial PRIMARY KEY, embedding sparsevec(10)); +``` + +Insert vectors + +```sql +INSERT INTO items (embedding) VALUES ('{0:1,1:2,2:3}/10'), ('{0:4,1:5,2:6}/10'); +``` + +Get the nearest neighbors by L2 distance + +```sql +SELECT * FROM items ORDER BY embedding <-> '{0:3,1:1,2:2}/10' LIMIT 5; +``` + ## Hybrid Search Use together with Postgres [full-text search](https://www.postgresql.org/docs/current/textsearch-intro.html) for hybrid search.