Skip to content

Commit ca85af7

Browse files
abrookinsclaude
andcommitted
Improve TAG field sortability error message and documentation
- Update error message to clearly explain that TAG fields cannot be sortable - Add explanation that string fields default to TAG fields for exact matching - Provide solution to use full_text_search=True for sortable TEXT fields - Document field type mapping and sortability rules in models.md and errors.md 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 9b9ceb3 commit ca85af7

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

aredis_om/model/model.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,9 +2906,10 @@ def schema_for_type(
29062906
case_sensitive = getattr(field_info, "case_sensitive", False)
29072907
full_text_search = getattr(field_info, "full_text_search", False)
29082908
sortable_tag_error = RedisModelError(
2909-
"In this Preview release, TAG fields cannot "
2910-
f"be marked as sortable. Problem field: {name}. "
2911-
"See docs: TODO"
2909+
f"TAG fields cannot be marked as sortable. Problem field: {name}. "
2910+
f"String fields are indexed as TAG fields by default, which only support exact matching. "
2911+
f"To make this field sortable, add 'full_text_search=True' to create a TEXT field instead: "
2912+
f"Field(index=True, sortable=True, full_text_search=True)"
29122913
)
29132914

29142915
# For more complicated compound validators (e.g. PositiveInt), we might get a _GenericAlias rather than

docs/errors.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class Member(JsonModel):
3838

3939
**NOTE:** Only an indexed field can be sortable.
4040

41+
**IMPORTANT:** String fields are indexed as TAG fields by default, which cannot be sortable. Only NUMERIC, TEXT, and GEO field types support sorting. To make a string field sortable, you must add `full_text_search=True` to create a TEXT field:
42+
43+
```python
44+
class Member(JsonModel):
45+
name: str = Field(index=True, sortable=True, full_text_search=True)
46+
```
47+
4148
## E3
4249

4350
>You tried to do a full-text search on the field '{field.name}', but the field is not indexed for full-text search. Use the full_text_search=True option.

docs/models.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,31 @@ class Customer(HashModel):
250250

251251
In this example, we marked `Customer.last_name` as indexed.
252252

253+
### Field Index Types
254+
255+
Redis OM automatically chooses the appropriate RediSearch field type based on the Python field type and options:
256+
257+
- **String fields****TAG fields** by default (exact matching only), or **TEXT fields** if `full_text_search=True`
258+
- **Numeric fields** (int, float) → **NUMERIC fields** (range queries and sorting)
259+
- **Boolean fields****TAG fields**
260+
- **Datetime fields****NUMERIC fields** (stored as Unix timestamps)
261+
- **Geographic fields****GEO fields**
262+
263+
### Making String Fields Sortable
264+
265+
By default, string fields are indexed as TAG fields, which only support exact matching and cannot be sorted. To make a string field sortable, you must create a TEXT field by adding `full_text_search=True`:
266+
267+
```python
268+
class Customer(HashModel):
269+
# TAG field - exact matching only, cannot be sorted
270+
category: str = Field(index=True)
271+
272+
# TEXT field - supports full-text search and sorting
273+
name: str = Field(index=True, sortable=True, full_text_search=True)
274+
```
275+
276+
Only NUMERIC, TEXT, and GEO field types support sorting in RediSearch.
277+
253278
To create the indexes for any models that have indexed fields, use the `migrate` CLI command that Redis OM installs in your Python environment.
254279

255280
This command detects any `JsonModel` or `HashModel` instances in your project and does the following for each model that isn't abstract or embedded:

0 commit comments

Comments
 (0)