Skip to content

Commit 57226ca

Browse files
committed
Added example of getting items within a certain distance with SQLAlchemy - closes pgvector#11
1 parent 6a05297 commit 57226ca

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ session.scalars(select(Item).order_by(Item.embedding.l2_distance([3, 1, 2])).lim
129129

130130
Also supports `max_inner_product` and `cosine_distance`
131131

132+
Get items within a certain distance
133+
134+
```python
135+
session.scalars(select(Item).filter(Item.embedding.l2_distance([3, 1, 2]) < 5))
136+
```
137+
132138
Add an approximate index
133139

134140
```python

tests/test_sqlalchemy.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ def test_cosine_distance(self):
105105
items = session.query(Item).order_by(Item.embedding.cosine_distance([1, 1, 1])).all()
106106
assert [v.id for v in items] == [1, 2, 3]
107107

108+
def test_filter(self):
109+
create_items()
110+
with Session(engine) as session:
111+
items = session.query(Item).filter(Item.embedding.l2_distance([1, 1, 1]) < 1).all()
112+
assert [v.id for v in items] == [1]
113+
108114
def test_select(self):
109115
with Session(engine) as session:
110116
session.add(Item(embedding=[2, 3, 3]))

0 commit comments

Comments
 (0)