1
1
import itertools
2
2
from collections import defaultdict
3
3
4
- from django .core .checks import Error
4
+ from django .core .checks import Error , Warning
5
5
from django .db import NotSupportedError
6
6
from django .db .models import DecimalField , FloatField , Index
7
7
from django .db .models .lookups import BuiltinLookup
@@ -107,6 +107,23 @@ def where_node_idx(self, compiler, connection):
107
107
108
108
class SearchIndex (Index ):
109
109
suffix = "six"
110
+ _error_id_prefix = "django_mongodb_backend.indexes.SearchIndex"
111
+
112
+ def check (self , model , connection ):
113
+ errors = []
114
+ if not connection .features .supports_search_indexes :
115
+ errors .append (
116
+ Warning (
117
+ "This version of MongoDB does not support search indexes." ,
118
+ hint = (
119
+ "The index won't be created. Silence this warning if you "
120
+ "don't care about it."
121
+ ),
122
+ obj = self ,
123
+ id = f"{ self ._error_id_prefix } .W001" ,
124
+ )
125
+ )
126
+ return errors
110
127
111
128
# Maps Django internal type to atlas search index type.
112
129
# Reference: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#data-types
@@ -144,14 +161,14 @@ def get_pymongo_index_model(
144
161
class VectorSearchIndex (SearchIndex ):
145
162
suffix = "vsi"
146
163
ALLOWED_SIMILARITY_FUNCTIONS = frozenset (("euclidean" , "cosine" , "dotProduct" ))
164
+ _error_id_prefix = "django_mongodb_backend.indexes.VectorSearchIndex"
147
165
148
166
def __init__ (self , * expressions , similarities = "cosine" , ** kwargs ):
149
167
super ().__init__ (* expressions , ** kwargs )
150
168
self .similarities = similarities
151
169
152
170
def check (self , model , connection ):
153
- errors = []
154
- error_id_prefix = "django_mongodb_backend.indexes.VectorSearchIndex"
171
+ errors = super ().check (model , connection )
155
172
similarities = (
156
173
self .similarities if isinstance (self .similarities , list ) else [self .similarities ]
157
174
)
@@ -162,7 +179,7 @@ def check(self, model, connection):
162
179
f"{ func } isn't a valid similarity function, options "
163
180
f"are { ', ' .join (sorted (self .ALLOWED_SIMILARITY_FUNCTIONS ))} " ,
164
181
obj = self ,
165
- id = f"{ error_id_prefix } .E004" ,
182
+ id = f"{ self . _error_id_prefix } .E004" ,
166
183
)
167
184
)
168
185
for field_name , _ in self .fields_orders :
@@ -175,15 +192,15 @@ def check(self, model, connection):
175
192
Error (
176
193
"Atlas vector search requires size." ,
177
194
obj = self ,
178
- id = f"{ error_id_prefix } .E001" ,
195
+ id = f"{ self . _error_id_prefix } .E001" ,
179
196
)
180
197
)
181
198
if not isinstance (field_ .base_field , FloatField | DecimalField ):
182
199
errors .append (
183
200
Error (
184
201
"Base type must be Float or Decimal." ,
185
202
obj = self ,
186
- id = f"{ error_id_prefix } .E002" ,
203
+ id = f"{ self . _error_id_prefix } .E002" ,
187
204
)
188
205
)
189
206
else :
@@ -197,7 +214,7 @@ def check(self, model, connection):
197
214
Error (
198
215
f"Unsupported filter of type { field_ .get_internal_type ()} ." ,
199
216
obj = self ,
200
- id = "django_mongodb_backend.indexes.VectorSearchIndex .E003" ,
217
+ id = f" { self . _error_id_prefix } .E003" ,
201
218
)
202
219
)
203
220
return errors
0 commit comments