Skip to content

Commit

Permalink
Add support for WITHSUFFIXTRIE to FT.CREATE (#2324)
Browse files Browse the repository at this point in the history
* withsuffixtrie

* Update test_search.py

* fix
  • Loading branch information
dvora-h authored Aug 4, 2022
1 parent 34f07a7 commit 2cea637
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
12 changes: 11 additions & 1 deletion redis/commands/search/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
weight: float = 1.0,
no_stem: bool = False,
phonetic_matcher: str = None,
withsuffixtrie: bool = False,
**kwargs,
):
Field.__init__(self, name, args=[Field.TEXT, Field.WEIGHT, weight], **kwargs)
Expand All @@ -78,6 +79,8 @@ def __init__(
]:
Field.append_arg(self, self.PHONETIC)
Field.append_arg(self, phonetic_matcher)
if withsuffixtrie:
Field.append_arg(self, "WITHSUFFIXTRIE")


class NumericField(Field):
Expand Down Expand Up @@ -108,11 +111,18 @@ class TagField(Field):
CASESENSITIVE = "CASESENSITIVE"

def __init__(
self, name: str, separator: str = ",", case_sensitive: bool = False, **kwargs
self,
name: str,
separator: str = ",",
case_sensitive: bool = False,
withsuffixtrie: bool = False,
**kwargs,
):
args = [Field.TAG, self.SEPARATOR, separator]
if case_sensitive:
args.append(self.CASESENSITIVE)
if withsuffixtrie:
args.append("WITHSUFFIXTRIE")

Field.__init__(self, name, args=args, **kwargs)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_asyncio/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,30 @@ async def test_aggregations_sort_by_and_limit(modclient: redis.Redis):
assert res.rows[0] == ["t1", "b"]


@pytest.mark.redismod
@pytest.mark.experimental
async def test_withsuffixtrie(modclient: redis.Redis):
# create index
assert await modclient.ft().create_index((TextField("txt"),))
await waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = await modclient.ft().info()
assert "WITHSUFFIXTRIE" not in info["attributes"][0]
assert await modclient.ft().dropindex("idx")

# create withsuffixtrie index (text field)
assert await modclient.ft().create_index((TextField("t", withsuffixtrie=True)))
await waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = await modclient.ft().info()
assert "WITHSUFFIXTRIE" in info["attributes"][0]
assert await modclient.ft().dropindex("idx")

# create withsuffixtrie index (tag field)
assert await modclient.ft().create_index((TagField("t", withsuffixtrie=True)))
await waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = await modclient.ft().info()
assert "WITHSUFFIXTRIE" in info["attributes"][0]


@pytest.mark.redismod
@skip_if_redis_enterprise()
async def test_search_commands_in_pipeline(modclient: redis.Redis):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -1712,3 +1712,27 @@ def test_expire_while_search(modclient: redis.Redis):
modclient.ft().search(Query("*")).docs[1]
time.sleep(1)
assert 2 == modclient.ft().search(Query("*")).total


@pytest.mark.redismod
@pytest.mark.experimental
def test_withsuffixtrie(modclient: redis.Redis):
# create index
assert modclient.ft().create_index((TextField("txt"),))
waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = modclient.ft().info()
assert "WITHSUFFIXTRIE" not in info["attributes"][0]
assert modclient.ft().dropindex("idx")

# create withsuffixtrie index (text fiels)
assert modclient.ft().create_index((TextField("t", withsuffixtrie=True)))
waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = modclient.ft().info()
assert "WITHSUFFIXTRIE" in info["attributes"][0]
assert modclient.ft().dropindex("idx")

# create withsuffixtrie index (tag field)
assert modclient.ft().create_index((TagField("t", withsuffixtrie=True)))
waitForIndex(modclient, getattr(modclient.ft(), "index_name", "idx"))
info = modclient.ft().info()
assert "WITHSUFFIXTRIE" in info["attributes"][0]

0 comments on commit 2cea637

Please sign in to comment.