mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-13 08:06:54 +08:00
Added storage examples [skip ci]
This commit is contained in:
29
README.md
29
README.md
@@ -37,7 +37,7 @@ You can also install it with [Docker](#docker), [Homebrew](#homebrew), [PGXN](#p
|
|||||||
Create a vector column with 3 dimensions
|
Create a vector column with 3 dimensions
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE TABLE items (embedding vector(3));
|
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
|
||||||
```
|
```
|
||||||
|
|
||||||
Insert values
|
Insert values
|
||||||
@@ -46,16 +46,39 @@ Insert values
|
|||||||
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
|
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
|
||||||
```
|
```
|
||||||
|
|
||||||
Get the nearest neighbor by L2 distance
|
Get the nearest neighbors by L2 distance
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 1;
|
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
|
||||||
```
|
```
|
||||||
|
|
||||||
Also supports inner product (`<#>`) and cosine distance (`<=>`)
|
Also supports inner product (`<#>`) and cosine distance (`<=>`)
|
||||||
|
|
||||||
Note: `<#>` returns the negative inner product since Postgres only supports `ASC` order index scans on operators
|
Note: `<#>` returns the negative inner product since Postgres only supports `ASC` order index scans on operators
|
||||||
|
|
||||||
|
## Storing
|
||||||
|
|
||||||
|
Insert vectors
|
||||||
|
|
||||||
|
```sql
|
||||||
|
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: `COPY` is also supported
|
||||||
|
|
||||||
|
Upsert vectors
|
||||||
|
|
||||||
|
```sql
|
||||||
|
INSERT INTO items (id, embedding) VALUES (1, '[1,2,3]'), (2, '[4,5,6]')
|
||||||
|
ON CONFLICT (id) DO UPDATE SET embedding = EXCLUDED.embedding;
|
||||||
|
```
|
||||||
|
|
||||||
|
Update vectors
|
||||||
|
|
||||||
|
```sql
|
||||||
|
UPDATE items SET embedding = '[1,2,3]' WHERE id = 1;
|
||||||
|
```
|
||||||
|
|
||||||
## Querying
|
## Querying
|
||||||
|
|
||||||
Use a `SELECT` clause to get the distance
|
Use a `SELECT` clause to get the distance
|
||||||
|
|||||||
Reference in New Issue
Block a user