Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,10 @@
from copy import copy
from enum import Enum
from functools import reduce
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Mapping,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Union,
)
from typing import (Any, Callable, Dict, List, Literal, Mapping, Optional,
Sequence, Set, Tuple, Type, TypeVar, Union)
from typing import get_args as typing_get_args
from typing import (
no_type_check,
)
from typing import no_type_check

from more_itertools import ichunked
from pydantic import BaseModel
Expand Down Expand Up @@ -2142,6 +2127,7 @@ def __init__(self, default: Any = ..., **kwargs: Any) -> None:
full_text_search = kwargs.pop("full_text_search", None)
vector_options = kwargs.pop("vector_options", None)
expire = kwargs.pop("expire", None)
separator = kwargs.pop("separator", SINGLE_VALUE_TAG_FIELD_SEPARATOR)
super().__init__(default=default, **kwargs)
self.primary_key = primary_key
self.sortable = sortable
Expand All @@ -2150,6 +2136,7 @@ def __init__(self, default: Any = ..., **kwargs: Any) -> None:
self.full_text_search = full_text_search
self.vector_options = vector_options
self.expire = expire
self.separator = separator


class RelationshipInfo(Representation):
Expand Down Expand Up @@ -2261,6 +2248,7 @@ def Field(
full_text_search: Union[bool, UndefinedType] = Undefined,
vector_options: Optional[VectorFieldOptions] = None,
expire: Optional[int] = None,
separator: str = SINGLE_VALUE_TAG_FIELD_SEPARATOR,
**kwargs: Unpack[_FromFieldInfoInputs],
) -> Any:
"""
Expand All @@ -2276,6 +2264,8 @@ def Field(
vector_options: Vector field configuration for similarity search.
expire: TTL in seconds for this field (HashModel only, requires Redis 7.4+).
When set, the field will automatically expire after save().
separator: TAG field separator character for RediSearch indexing.
Defaults to "|". Use "," for comma-separated multi-value fields.
**kwargs: Additional Pydantic field options.

Returns:
Expand All @@ -2291,6 +2281,7 @@ def Field(
full_text_search=full_text_search,
vector_options=vector_options,
expire=expire,
separator=separator,
)
return field_info

Expand Down Expand Up @@ -3286,9 +3277,10 @@ def schema_for_fields(cls):

if getattr(field_info, "primary_key", None) is True:
if issubclass(_type, str):
redisearch_field = (
f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
separator = getattr(
field_info, "separator", SINGLE_VALUE_TAG_FIELD_SEPARATOR
)
redisearch_field = f"{name} TAG SEPARATOR {separator}"
else:
redisearch_field = cls.schema_for_type(name, _type, field_info)
schema_parts.append(redisearch_field)
Expand Down Expand Up @@ -3346,13 +3338,15 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
else:
schema = f"{name} NUMERIC"
elif issubclass(typ, str):
separator = getattr(
field_info, "separator", SINGLE_VALUE_TAG_FIELD_SEPARATOR
)
if getattr(field_info, "full_text_search", False) is True:
schema = (
f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR} "
f"{name} AS {name}_fts TEXT"
f"{name} TAG SEPARATOR {separator} " f"{name} AS {name}_fts TEXT"
)
else:
schema = f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
schema = f"{name} TAG SEPARATOR {separator}"
elif issubclass(typ, RedisModel):
sub_fields = []
for embedded_name, field in typ.model_fields.items():
Expand All @@ -3363,7 +3357,10 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
)
schema = " ".join(sub_fields)
else:
schema = f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
separator = getattr(
field_info, "separator", SINGLE_VALUE_TAG_FIELD_SEPARATOR
)
schema = f"{name} TAG SEPARATOR {separator}"
if schema and sortable is True:
schema += " SORTABLE"
if schema and case_sensitive is True:
Expand Down Expand Up @@ -3627,7 +3624,10 @@ def schema_for_fields(cls):

if getattr(field_info, "primary_key", None) is True:
if issubclass(_type, str):
redisearch_field = f"$.{name} AS {name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
separator = getattr(
field_info, "separator", SINGLE_VALUE_TAG_FIELD_SEPARATOR
)
redisearch_field = f"$.{name} AS {name} TAG SEPARATOR {separator}"
else:
redisearch_field = cls.schema_for_type(
json_path, name, "", _type, field_info
Expand Down Expand Up @@ -3781,6 +3781,11 @@ def schema_for_type(
else typ
)

# Get separator from field_info, defaulting to pipe
separator = getattr(
field_info, "separator", SINGLE_VALUE_TAG_FIELD_SEPARATOR
)

if is_vector and vector_options:
schema = f"{path} AS {index_field_name} {vector_options.schema}"
elif parent_is_container_type or parent_is_model_in_container:
Expand All @@ -3795,7 +3800,7 @@ def schema_for_type(
f"search. Problem field: {name}. Docs: {ERRORS_URL}#E13"
)
# List/tuple fields are indexed as TAG fields and can be sortable
schema = f"{path} AS {index_field_name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
schema = f"{path} AS {index_field_name} TAG SEPARATOR {separator}"
if sortable is True:
schema += " SORTABLE"
if case_sensitive is True:
Expand All @@ -3815,7 +3820,7 @@ def schema_for_type(
elif issubclass(typ, str):
if full_text_search is True:
schema = (
f"{path} AS {index_field_name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR} "
f"{path} AS {index_field_name} TAG SEPARATOR {separator} "
f"{path} AS {index_field_name}_fts TEXT"
)
if sortable is True:
Expand All @@ -3829,14 +3834,14 @@ def schema_for_type(
raise RedisModelError("Text fields cannot be case-sensitive.")
else:
# String fields are indexed as TAG fields and can be sortable
schema = f"{path} AS {index_field_name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
schema = f"{path} AS {index_field_name} TAG SEPARATOR {separator}"
if sortable is True:
schema += " SORTABLE"
if case_sensitive is True:
schema += " CASESENSITIVE"
else:
# Default to TAG field, which can be sortable
schema = f"{path} AS {index_field_name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
schema = f"{path} AS {index_field_name} TAG SEPARATOR {separator}"
if sortable is True:
schema += " SORTABLE"

Expand Down
Loading