Skip to content

Commit dcdf271

Browse files
committed
Add check when VectorSearchIndex has no vector field
1 parent 0f28afe commit dcdf271

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

django_mongodb_backend/indexes.py

+12
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ def check(self, model, connection):
241241
id=f"{self._error_id_prefix}.E005",
242242
)
243243
)
244+
# There isn't any vector.
245+
if expected_similarities == 0:
246+
errors.append(
247+
Error(
248+
"VectorSearchIndex requires at least one field containing vector data "
249+
"(e.g., an ArrayField(FloatField, size=10)). "
250+
"If you're aiming to perform search operations on other data types, "
251+
"consider using SearchIndex instead.",
252+
obj=model,
253+
id=f"{self._error_id_prefix}.E006",
254+
)
255+
)
244256
return errors
245257

246258
def deconstruct(self):

tests/indexes_/test_checks.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ class Meta:
4141
def test_vector_search_requires_atlas_search_support(self):
4242
class Article(models.Model):
4343
title = models.CharField(max_length=10)
44+
vector_float = ArrayField(models.FloatField(), size=10)
4445

4546
class Meta:
46-
indexes = [VectorSearchIndex(fields=["title"])]
47+
indexes = [VectorSearchIndex(fields=["title", "vector_float"])]
4748

4849
errors = checks.run_checks(app_configs=self.apps.get_app_configs(), databases={"default"})
4950
self.assertEqual(
@@ -108,9 +109,10 @@ class Meta:
108109
def test_unsupported_type(self):
109110
class Article(models.Model):
110111
data = models.JSONField()
112+
vector_float = ArrayField(models.FloatField(), size=10)
111113

112114
class Meta:
113-
indexes = [VectorSearchIndex(fields=["data"])]
115+
indexes = [VectorSearchIndex(fields=["data", "vector_float"])]
114116

115117
errors = checks.run_checks(app_configs=self.apps.get_app_configs(), databases={"default"})
116118
self.assertEqual(
@@ -219,3 +221,24 @@ class Meta:
219221

220222
errors = checks.run_checks(app_configs=self.apps.get_app_configs(), databases={"default"})
221223
self.assertEqual(errors, [])
224+
225+
def test_requires_vector_field(self):
226+
class NoSearchVectorModel(models.Model):
227+
text = models.CharField(max_length=100)
228+
229+
class Meta:
230+
indexes = [VectorSearchIndex(name="recent_test_idx", fields=["text"])]
231+
232+
errors = checks.run_checks(app_configs=self.apps.get_app_configs(), databases={"default"})
233+
self.assertEqual(
234+
errors,
235+
[
236+
checks.Error(
237+
"VectorSearchIndex requires at least one field containing vector data "
238+
"(e.g., an ArrayField(FloatField, size=10)). If you're aiming to perform "
239+
"search operations on other data types, consider using SearchIndex instead.",
240+
id="django_mongodb_backend.indexes.VectorSearchIndex.E006",
241+
obj=NoSearchVectorModel,
242+
),
243+
],
244+
)

0 commit comments

Comments
 (0)