Skip to content

Commit d34d3b9

Browse files
authored
Support building vector index with django_tidb>=5.0.1 (#64)
1 parent cd238bc commit d34d3b9

File tree

5 files changed

+22
-33
lines changed

5 files changed

+22
-33
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,5 @@ django_tests_dir
143143
*.swp
144144

145145
.vscode/
146+
147+
.DS_Store
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Django==4.2.4
2-
django-tidb>=4.2.4
2+
django-tidb>=5.0.1
33
mysqlclient==2.2.0
44
python-dotenv==1.0.0
5-
tidb-vector>=0.0.9
5+
tidb-vector>=0.0.9
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# Generated by Django 4.2.4 on 2024-06-25 01:51
1+
# Generated by Django 4.2.4 on 2024-11-05 05:36
22

33
from django.db import migrations, models
44
import django_tidb.fields.vector
55

66

77
class Migration(migrations.Migration):
8+
89
initial = True
910

1011
dependencies = []
@@ -25,26 +26,16 @@ class Migration(migrations.Migration):
2526
("content", models.TextField()),
2627
("embedding", django_tidb.fields.vector.VectorField(dimensions=3)),
2728
],
28-
),
29-
migrations.CreateModel(
30-
name="DocumentWithIndex",
31-
fields=[
32-
(
33-
"id",
34-
models.BigAutoField(
35-
auto_created=True,
36-
primary_key=True,
37-
serialize=False,
38-
verbose_name="ID",
29+
options={
30+
"indexes": [
31+
django_tidb.fields.vector.VectorIndex(
32+
django_tidb.fields.vector.L2Distance("embedding"), name="idx_l2"
3933
),
40-
),
41-
("content", models.TextField()),
42-
(
43-
"embedding",
44-
django_tidb.fields.vector.VectorField(
45-
db_comment="hnsw(distance=cosine)", dimensions=3
34+
django_tidb.fields.vector.VectorIndex(
35+
django_tidb.fields.vector.CosineDistance("embedding"),
36+
name="idx_cos",
4637
),
47-
),
48-
],
38+
],
39+
},
4940
),
5041
]
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
from django.db import models
2-
from django_tidb.fields.vector import VectorField
2+
from django_tidb.fields.vector import VectorField, VectorIndex, CosineDistance, L2Distance
33

44

55
class Document(models.Model):
66
content = models.TextField()
77
embedding = VectorField(dimensions=3)
8-
9-
10-
class DocumentWithIndex(models.Model):
11-
content = models.TextField()
12-
# Note:
13-
# - Using comment to add hnsw index is a temporary solution. In the future it will use `CREATE INDEX` syntax.
14-
# - Currently the HNSW index cannot be changed after the table has been created.
15-
# - Only Django >= 4.2 supports `db_comment`.
16-
embedding = VectorField(dimensions=3, db_comment="hnsw(distance=cosine)")
8+
class Meta:
9+
indexes = [
10+
VectorIndex(L2Distance("embedding"), name='idx_l2'),
11+
VectorIndex(CosineDistance("embedding"), name='idx_cos'),
12+
]

examples/orm-django-quickstart/sample_project/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ def list_routes(request):
5050
'/get_nearest_neighbors_documents',
5151
'/get_documents_within_distance'
5252
]
53-
})
53+
})

0 commit comments

Comments
 (0)