@@ -1290,19 +1290,20 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa C901
1290
1290
)
1291
1291
new_class .Meta = new_class ._meta
1292
1292
1293
- if new_class .model_config .get ("index" , None ) is True :
1293
+ is_indexed = kwargs .get ("index" , None ) is True
1294
+
1295
+ if is_indexed and new_class .model_config .get ("index" , None ) is True :
1294
1296
raise RedisModelError (
1295
1297
f"{ new_class .__name__ } cannot be indexed, only one model can be indexed in an inheritance tree"
1296
1298
)
1297
1299
1300
+ new_class .model_config ["index" ] = is_indexed
1301
+
1298
1302
# Create proxies for each model field so that we can use the field
1299
1303
# in queries, like Model.get(Model.field_name == 1)
1300
1304
# Only set if the model is has index=True
1301
- is_indexed = kwargs .get ("index" , None ) is True
1302
- new_class .model_config ["index" ] = is_indexed
1303
-
1304
1305
for field_name , field in new_class .model_fields .items ():
1305
- if field . __class__ is PydanticFieldInfo :
1306
+ if type ( field ) is PydanticFieldInfo :
1306
1307
field = FieldInfo (** field ._attributes_set )
1307
1308
setattr (new_class , field_name , field )
1308
1309
@@ -1370,6 +1371,15 @@ def outer_type_or_annotation(field: FieldInfo):
1370
1371
else :
1371
1372
return field .annotation .__args__ [0 ] # type: ignore
1372
1373
1374
+ def should_index_field (field_info : FieldInfo ) -> bool :
1375
+ # for vector, full text search, and sortable fields, we always have to index
1376
+ # We could require the user to set index=True, but that would be a breaking change
1377
+ return (
1378
+ getattr (field_info , "index" , False ) is True
1379
+ or getattr (field_info , "vector_options" , None ) is not None
1380
+ or getattr (field_info , "full_text_search" , False ) is True
1381
+ or getattr (field_info , "sortable" , False ) is True
1382
+ )
1373
1383
1374
1384
class RedisModel (BaseModel , abc .ABC , metaclass = ModelMeta ):
1375
1385
pk : Optional [str ] = Field (
@@ -1736,7 +1746,7 @@ def schema_for_fields(cls):
1736
1746
else :
1737
1747
redisearch_field = cls .schema_for_type (name , _type , field_info )
1738
1748
schema_parts .append (redisearch_field )
1739
- elif getattr (field_info , "index" , None ) is True :
1749
+ elif should_index_field (field_info ) :
1740
1750
schema_parts .append (cls .schema_for_type (name , _type , field_info ))
1741
1751
elif is_subscripted_type :
1742
1752
# Ignore subscripted types (usually containers!) that we don't
@@ -1945,10 +1955,7 @@ def schema_for_type(
1945
1955
field_info : PydanticFieldInfo ,
1946
1956
parent_type : Optional [Any ] = None ,
1947
1957
) -> str :
1948
- should_index = (
1949
- getattr (field_info , "index" , False ) is True
1950
- or getattr (field_info , "vector_options" , None ) is not None
1951
- )
1958
+ should_index = should_index_field (field_info )
1952
1959
is_container_type = is_supported_container_type (typ )
1953
1960
parent_is_container_type = is_supported_container_type (parent_type )
1954
1961
parent_is_model = False
0 commit comments