Skip to content

Commit 26aa350

Browse files
committed
Add parameter connection to the vector search index check
1 parent 4263fe6 commit 26aa350

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

django_mongodb_backend/indexes.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@
44
from django.core.checks import Error
55
from django.db import NotSupportedError
66
from django.db.models import (
7-
BooleanField,
8-
CharField,
9-
DateField,
10-
DateTimeField,
117
DecimalField,
128
FloatField,
139
Index,
14-
IntegerField,
15-
TextField,
16-
UUIDField,
1710
)
1811
from django.db.models.lookups import BuiltinLookup
1912
from django.db.models.sql.query import Query
2013
from django.db.models.sql.where import AND, XOR, WhereNode
2114
from pymongo import ASCENDING, DESCENDING
2215
from pymongo.operations import IndexModel, SearchIndexModel
2316

24-
from django_mongodb_backend.fields import ArrayField, ObjectIdAutoField, ObjectIdField
17+
from django_mongodb_backend.fields import ArrayField
2518

2619
from .query_utils import process_rhs
2720

@@ -161,7 +154,7 @@ def __init__(self, *expressions, similarities="cosine", **kwargs):
161154
# validate the similarities types
162155
self.similarities = similarities
163156

164-
def check(self, model):
157+
def check(self, model, connection):
165158
errors = []
166159
error_id_prefix = "django_mongodb_backend.indexes.VectorSearchIndex"
167160
similarities = (
@@ -196,28 +189,20 @@ def check(self, model):
196189
id=f"{error_id_prefix}.E002",
197190
)
198191
)
199-
# filter - for fields that contain boolean, date, objectId,
200-
# numeric, string, or UUID values. Reference:
201-
# https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type/#atlas-vector-search-index-fields
202-
elif not isinstance(
203-
field_,
204-
BooleanField
205-
| IntegerField
206-
| DateField
207-
| DateTimeField
208-
| CharField
209-
| TextField
210-
| UUIDField
211-
| ObjectIdField
212-
| ObjectIdAutoField,
213-
):
214-
errors.append(
215-
Error(
216-
f"Unsupported filter of type {field_.get_internal_type()}.",
217-
obj=self,
218-
id="django_mongodb_backend.indexes.VectorSearchIndex.E003",
192+
else:
193+
field_type = field_.db_type(connection)
194+
search_type = self.search_index_data_types(field_, field_type)
195+
# filter - for fields that contain boolean, date, objectId,
196+
# numeric, string, or UUID values. Reference:
197+
# https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type/#atlas-vector-search-index-fields
198+
if search_type not in ("number", "string", "boolean", "objectId", "uuid", "date"):
199+
errors.append(
200+
Error(
201+
f"Unsupported filter of type {field_.get_internal_type()}.",
202+
obj=self,
203+
id="django_mongodb_backend.indexes.VectorSearchIndex.E003",
204+
)
219205
)
220-
)
221206
return errors
222207

223208
def deconstruct(self):

0 commit comments

Comments
 (0)