Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 6867e8b

Browse files
committed
Added HnswIndex for Django
1 parent e6ca2c2 commit 6867e8b

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
dev-files: true
2020
- run: |
2121
cd /tmp
22-
git clone --branch v0.4.4 https://github.com/pgvector/pgvector.git
22+
git clone --branch v0.5.0 https://github.com/pgvector/pgvector.git
2323
cd pgvector
2424
make
2525
sudo make install

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.2 (unreleased)
2+
3+
- Added `HnswIndex` for Django
4+
15
## 0.2.1 (2023-07-31)
26

37
- Fixed form issues with Django

pgvector/django/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ def get_with_params(self):
7373
return with_params
7474

7575

76+
class HnswIndex(PostgresIndex):
77+
suffix = 'hnsw'
78+
79+
def __init__(self, *expressions, m=None, ef_construction=None, **kwargs):
80+
self.m = m
81+
self.ef_construction = ef_construction
82+
super().__init__(*expressions, **kwargs)
83+
84+
def deconstruct(self):
85+
path, args, kwargs = super().deconstruct()
86+
if self.m is not None:
87+
kwargs['m'] = self.m
88+
if self.ef_construction is not None:
89+
kwargs['ef_construction'] = self.ef_construction
90+
return path, args, kwargs
91+
92+
def get_with_params(self):
93+
with_params = []
94+
if self.m is not None:
95+
with_params.append('m = %d' % self.m)
96+
if self.ef_construction is not None:
97+
with_params.append('ef_construction = %d' % self.ef_construction)
98+
return with_params
99+
100+
76101
class DistanceBase(Func):
77102
output_field = FloatField()
78103

tests/test_django.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from math import sqrt
88
import numpy as np
99
import pgvector.django
10-
from pgvector.django import VectorExtension, VectorField, IvfflatIndex, L2Distance, MaxInnerProduct, CosineDistance
10+
from pgvector.django import VectorExtension, VectorField, IvfflatIndex, HnswIndex, L2Distance, MaxInnerProduct, CosineDistance
1111
from unittest import mock
1212

1313
settings.configure(
@@ -28,10 +28,17 @@ class Meta:
2828
app_label = 'myapp'
2929
indexes = [
3030
IvfflatIndex(
31-
name='my_index',
31+
name='ivfflat_idx',
3232
fields=['embedding'],
3333
lists=100,
3434
opclasses=['vector_l2_ops']
35+
),
36+
HnswIndex(
37+
name='hnsw_idx',
38+
fields=['embedding'],
39+
m=16,
40+
ef_construction=100,
41+
opclasses=['vector_l2_ops']
3542
)
3643
]
3744

0 commit comments

Comments
 (0)