From 800ec69565e348afc14c11ab147cb202d9050f0b Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Thu, 9 Dec 2021 23:11:15 +0000 Subject: [PATCH 1/3] Attempts to fix full text search fields Adds field name alias to fix full text searches. Closes #42 --- aredis_om/model/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 7d75030..8a1fac2 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -1429,7 +1429,7 @@ class HashModel(RedisModel, abc.ABC): if getattr(field_info, "full_text_search", False) is True: schema = ( f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR} " - f"{name}_fts TEXT" + f"{name} AS {name}_fts TEXT" ) else: schema = f"{name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}" From 8d69b59916e13dd803fdf9962b73674176fd8609 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Wed, 5 Jan 2022 18:08:49 +0000 Subject: [PATCH 2/3] Adds tests for full text hash search. --- tests/test_hash_model.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 331e3ff..5d7b6fe 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -47,6 +47,7 @@ async def m(key_prefix, redis): email: str = Field(index=True) join_date: datetime.date age: int = Field(index=True) + bio: str = Field(index=True, full_text_search=True) class Meta: model_key_prefix = "member" @@ -67,6 +68,7 @@ async def members(m): email="a@example.com", age=38, join_date=today, + bio="This is member 1 whose greatness makes him the life and soul of any party he goes to.", ) member2 = m.Member( @@ -75,6 +77,7 @@ async def members(m): email="k@example.com", age=34, join_date=today, + bio="This is member 2 who can be quite anxious until you get to know them.", ) member3 = m.Member( @@ -83,6 +86,7 @@ async def members(m): email="as@example.com", age=100, join_date=today, + bio="This is member 3 who is a funny and lively sort of person.", ) await member1.save() await member2.save() @@ -124,6 +128,21 @@ async def test_exact_match_queries(members, m): ).all() assert actual == [member2] +@pytest.mark.asyncio +async def test_full_text_search_queries(members, m): + member1, member2, member3 = members + + actual = await ( + m.Member.find(m.Member.bio % "great").all() + ) + + assert actual == [member1] + + actual = await ( + m.Member.find(~(m.Member.bio % "anxious")).all() + ) + + assert actual == [member1, member3] @pytest.mark.asyncio async def test_recursive_query_resolution(members, m): @@ -163,6 +182,7 @@ async def test_tag_queries_punctuation(m): email="a|b@example.com", # NOTE: This string uses the TAG field separator. age=38, join_date=today, + bio="This is a test user on our system.", ) await member1.save() @@ -172,6 +192,7 @@ async def test_tag_queries_punctuation(m): email="a|villain@example.com", # NOTE: This string uses the TAG field separator. age=38, join_date=today, + bio="This is a villain, they are a really bad person!", ) await member2.save() @@ -334,6 +355,7 @@ def test_validation_passes(m): email="a@example.com", join_date=today, age=38, + bio="This is the bio field.", ) assert member.first_name == "Andrew" @@ -346,6 +368,7 @@ async def test_saves_model_and_creates_pk(m): email="a@example.com", join_date=today, age=38, + bio="This is the bio field for this user.", ) # Save a model instance to Redis await member.save() @@ -408,6 +431,7 @@ async def test_saves_many(m): email="a@example.com", join_date=today, age=38, + bio="This is the user bio.", ) member2 = m.Member( first_name="Kim", @@ -415,6 +439,7 @@ async def test_saves_many(m): email="k@example.com", join_date=today, age=34, + bio="This is the bio for Kim.", ) members = [member1, member2] result = await m.Member.add(members) From 954913031a63cf8494195ddf5fa00e564c87517f Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Wed, 5 Jan 2022 21:12:31 +0000 Subject: [PATCH 3/3] Amended schema test too. --- tests/test_hash_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 5d7b6fe..134bc0e 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -507,5 +507,5 @@ def test_schema(m): assert ( Address.redisearch_schema() - == 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" + == 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" )