Skip to content

Commit 4a56ed2

Browse files
author
Simon Prickett
authored
Merge pull request #43 from simonprickett/patch-3
Fix full text search field indexing issue
2 parents 0b206dd + 9549130 commit 4a56ed2

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

aredis_om/model/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
14291429
if getattr(field_info, "full_text_search", False) is True:
14301430
schema = (
14311431
f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR} "
1432-
f"{name}_fts TEXT"
1432+
f"{name} AS {name}_fts TEXT"
14331433
)
14341434
else:
14351435
schema = f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"

tests/test_hash_model.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Member(BaseHashModel):
4747
email: str = Field(index=True)
4848
join_date: datetime.date
4949
age: int = Field(index=True)
50+
bio: str = Field(index=True, full_text_search=True)
5051

5152
class Meta:
5253
model_key_prefix = "member"
@@ -67,6 +68,7 @@ async def members(m):
6768
6869
age=38,
6970
join_date=today,
71+
bio="This is member 1 whose greatness makes him the life and soul of any party he goes to.",
7072
)
7173

7274
member2 = m.Member(
@@ -75,6 +77,7 @@ async def members(m):
7577
7678
age=34,
7779
join_date=today,
80+
bio="This is member 2 who can be quite anxious until you get to know them.",
7881
)
7982

8083
member3 = m.Member(
@@ -83,6 +86,7 @@ async def members(m):
8386
8487
age=100,
8588
join_date=today,
89+
bio="This is member 3 who is a funny and lively sort of person.",
8690
)
8791
await member1.save()
8892
await member2.save()
@@ -124,6 +128,21 @@ async def test_exact_match_queries(members, m):
124128
).all()
125129
assert actual == [member2]
126130

131+
@pytest.mark.asyncio
132+
async def test_full_text_search_queries(members, m):
133+
member1, member2, member3 = members
134+
135+
actual = await (
136+
m.Member.find(m.Member.bio % "great").all()
137+
)
138+
139+
assert actual == [member1]
140+
141+
actual = await (
142+
m.Member.find(~(m.Member.bio % "anxious")).all()
143+
)
144+
145+
assert actual == [member1, member3]
127146

128147
@pytest.mark.asyncio
129148
async def test_recursive_query_resolution(members, m):
@@ -163,6 +182,7 @@ async def test_tag_queries_punctuation(m):
163182
email="a|[email protected]", # NOTE: This string uses the TAG field separator.
164183
age=38,
165184
join_date=today,
185+
bio="This is a test user on our system.",
166186
)
167187
await member1.save()
168188

@@ -172,6 +192,7 @@ async def test_tag_queries_punctuation(m):
172192
email="a|[email protected]", # NOTE: This string uses the TAG field separator.
173193
age=38,
174194
join_date=today,
195+
bio="This is a villain, they are a really bad person!",
175196
)
176197
await member2.save()
177198

@@ -334,6 +355,7 @@ def test_validation_passes(m):
334355
335356
join_date=today,
336357
age=38,
358+
bio="This is the bio field.",
337359
)
338360
assert member.first_name == "Andrew"
339361

@@ -346,6 +368,7 @@ async def test_saves_model_and_creates_pk(m):
346368
347369
join_date=today,
348370
age=38,
371+
bio="This is the bio field for this user.",
349372
)
350373
# Save a model instance to Redis
351374
await member.save()
@@ -408,13 +431,15 @@ async def test_saves_many(m):
408431
409432
join_date=today,
410433
age=38,
434+
bio="This is the user bio.",
411435
)
412436
member2 = m.Member(
413437
first_name="Kim",
414438
last_name="Brookins",
415439
416440
join_date=today,
417441
age=34,
442+
bio="This is the bio for Kim.",
418443
)
419444
members = [member1, member2]
420445
result = await m.Member.add(members)
@@ -482,5 +507,5 @@ class Address(m.BaseHashModel):
482507

483508
assert (
484509
Address.redisearch_schema()
485-
== f"ON HASH PREFIX 1 {key_prefix} SCHEMA pk TAG SEPARATOR | a_string TAG SEPARATOR | a_full_text_string TAG SEPARATOR | a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE a_float NUMERIC"
510+
== f"ON HASH PREFIX 1 {key_prefix} SCHEMA pk TAG SEPARATOR | a_string TAG SEPARATOR | a_full_text_string TAG SEPARATOR | a_full_text_string AS a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE a_float NUMERIC"
486511
)

0 commit comments

Comments
 (0)