Skip to content

Fix full text search field indexing issue #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2022
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
2 changes: 1 addition & 1 deletion aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
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}"
Expand Down
27 changes: 26 additions & 1 deletion tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Member(BaseHashModel):
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"
Expand All @@ -67,6 +68,7 @@ async def members(m):
email="[email protected]",
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(
Expand All @@ -75,6 +77,7 @@ async def members(m):
email="[email protected]",
age=34,
join_date=today,
bio="This is member 2 who can be quite anxious until you get to know them.",
)

member3 = m.Member(
Expand All @@ -83,6 +86,7 @@ async def members(m):
email="[email protected]",
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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -163,6 +182,7 @@ async def test_tag_queries_punctuation(m):
email="a|[email protected]", # 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()

Expand All @@ -172,6 +192,7 @@ async def test_tag_queries_punctuation(m):
email="a|[email protected]", # 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()

Expand Down Expand Up @@ -334,6 +355,7 @@ def test_validation_passes(m):
email="[email protected]",
join_date=today,
age=38,
bio="This is the bio field.",
)
assert member.first_name == "Andrew"

Expand All @@ -346,6 +368,7 @@ async def test_saves_model_and_creates_pk(m):
email="[email protected]",
join_date=today,
age=38,
bio="This is the bio field for this user.",
)
# Save a model instance to Redis
await member.save()
Expand Down Expand Up @@ -408,13 +431,15 @@ async def test_saves_many(m):
email="[email protected]",
join_date=today,
age=38,
bio="This is the user bio.",
)
member2 = m.Member(
first_name="Kim",
last_name="Brookins",
email="[email protected]",
join_date=today,
age=34,
bio="This is the bio for Kim.",
)
members = [member1, member2]
result = await m.Member.add(members)
Expand Down Expand Up @@ -482,5 +507,5 @@ class Address(m.BaseHashModel):

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"
)