Skip to content

Commit

Permalink
Support building vector index with django_tidb>=5.0.1 (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySon-Huang authored Nov 5, 2024
1 parent cd238bc commit d34d3b9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ django_tests_dir
*.swp

.vscode/

.DS_Store
4 changes: 2 additions & 2 deletions examples/orm-django-quickstart/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Django==4.2.4
django-tidb>=4.2.4
django-tidb>=5.0.1
mysqlclient==2.2.0
python-dotenv==1.0.0
tidb-vector>=0.0.9
tidb-vector>=0.0.9
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Generated by Django 4.2.4 on 2024-06-25 01:51
# Generated by Django 4.2.4 on 2024-11-05 05:36

from django.db import migrations, models
import django_tidb.fields.vector


class Migration(migrations.Migration):

initial = True

dependencies = []
Expand All @@ -25,26 +26,16 @@ class Migration(migrations.Migration):
("content", models.TextField()),
("embedding", django_tidb.fields.vector.VectorField(dimensions=3)),
],
),
migrations.CreateModel(
name="DocumentWithIndex",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
options={
"indexes": [
django_tidb.fields.vector.VectorIndex(
django_tidb.fields.vector.L2Distance("embedding"), name="idx_l2"
),
),
("content", models.TextField()),
(
"embedding",
django_tidb.fields.vector.VectorField(
db_comment="hnsw(distance=cosine)", dimensions=3
django_tidb.fields.vector.VectorIndex(
django_tidb.fields.vector.CosineDistance("embedding"),
name="idx_cos",
),
),
],
],
},
),
]
16 changes: 6 additions & 10 deletions examples/orm-django-quickstart/sample_project/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from django.db import models
from django_tidb.fields.vector import VectorField
from django_tidb.fields.vector import VectorField, VectorIndex, CosineDistance, L2Distance


class Document(models.Model):
content = models.TextField()
embedding = VectorField(dimensions=3)


class DocumentWithIndex(models.Model):
content = models.TextField()
# Note:
# - Using comment to add hnsw index is a temporary solution. In the future it will use `CREATE INDEX` syntax.
# - Currently the HNSW index cannot be changed after the table has been created.
# - Only Django >= 4.2 supports `db_comment`.
embedding = VectorField(dimensions=3, db_comment="hnsw(distance=cosine)")
class Meta:
indexes = [
VectorIndex(L2Distance("embedding"), name='idx_l2'),
VectorIndex(CosineDistance("embedding"), name='idx_cos'),
]
2 changes: 1 addition & 1 deletion examples/orm-django-quickstart/sample_project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def list_routes(request):
'/get_nearest_neighbors_documents',
'/get_documents_within_distance'
]
})
})

0 comments on commit d34d3b9

Please sign in to comment.