Skip to content

Commit 599fd75

Browse files
authored
feat: add FT.ALIASLIST support (#4198)
* feat: add FT.ALIASLIST support * Fixing response callbacks for aliaslist executed in pipelines * Fixed aliaslist to respect client decoding config.
1 parent 71e275e commit 599fd75

7 files changed

Lines changed: 235 additions & 5 deletions

File tree

‎redis/cluster.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ class AbstractRedisCluster:
528528
"FT.ALIASADD",
529529
"FT.ALIASUPDATE",
530530
"FT.ALIASDEL",
531+
"FT.ALIASLIST",
531532
"FT.TAGVALS",
532533
"FT.SUGADD",
533534
"FT.SUGGET",

‎redis/commands/cluster.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
"JSON.STRLEN",
166166
"JSON.TYPE",
167167
# RediSearch Module
168+
"FT.ALIASLIST",
168169
"FT.EXPLAIN",
169170
"FT.INFO",
170171
"FT.PROFILE",

‎redis/commands/policies.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
request_policy=RequestPolicy.DEFAULT_KEYLESS,
104104
response_policy=ResponsePolicy.DEFAULT_KEYLESS,
105105
),
106+
"aliaslist": CommandPolicies(
107+
request_policy=RequestPolicy.DEFAULT_KEYLESS,
108+
response_policy=ResponsePolicy.DEFAULT_KEYLESS,
109+
),
106110
"sugdel": CommandPolicies(
107111
request_policy=RequestPolicy.DEFAULT_KEYED,
108112
response_policy=ResponsePolicy.DEFAULT_KEYED,

‎redis/commands/search/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def _register_module_callbacks(self):
200200
elif check_protocol_version(protocol, 3):
201201
cmd_callbacks = self._RESP3_MODULE_CALLBACKS
202202
else:
203-
cmd_callbacks = {}
203+
cmd_callbacks = self._RESP2_LEGACY_PIPELINE_CALLBACKS
204204
else:
205205
if check_protocol_version(protocol, 3):
206206
cmd_callbacks = self._RESP3_UNIFIED_MODULE_CALLBACKS

‎redis/commands/search/commands.py‎

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import itertools
22
import time
3-
from typing import Any, Dict, List, Optional, Union
3+
from typing import Any, Dict, List, Optional, Set, Union
44

55
from redis._parsers.helpers import pairs_to_dict
66
from redis.client import NEVER_DECODE, Pipeline
@@ -11,6 +11,7 @@
1111
HybridQuery,
1212
)
1313
from redis.commands.search.hybrid_result import HybridCursorResult, HybridResult
14+
from redis.typing import KeyT
1415
from redis.utils import (
1516
check_protocol_version,
1617
decode_field_value,
@@ -57,6 +58,7 @@
5758
ALIAS_ADD_CMD = "FT.ALIASADD"
5859
ALIAS_UPDATE_CMD = "FT.ALIASUPDATE"
5960
ALIAS_DEL_CMD = "FT.ALIASDEL"
61+
ALIAS_LIST_CMD = "FT.ALIASLIST"
6062
INFO_CMD = "FT.INFO"
6163
SUGADD_COMMAND = "FT.SUGADD"
6264
SUGDEL_COMMAND = "FT.SUGDEL"
@@ -109,6 +111,7 @@ def _init_module_callbacks(self):
109111
SPELLCHECK_CMD: self._parse_spellcheck,
110112
CONFIG_CMD: self._parse_config_get,
111113
SYNDUMP_CMD: self._parse_syndump,
114+
ALIAS_LIST_CMD: self._parse_aliaslist,
112115
}
113116
# Explicit ``protocol=3`` + ``legacy_responses=True`` keeps the
114117
# pre-existing native RESP3 surface. SEARCH and HYBRID both use
@@ -119,6 +122,7 @@ def _init_module_callbacks(self):
119122
self._RESP3_MODULE_CALLBACKS = {
120123
SEARCH_CMD: self._parse_search_resp3_native,
121124
HYBRID_CMD: self._parse_hybrid_search_resp3_native,
125+
ALIAS_LIST_CMD: self._parse_aliaslist,
122126
}
123127
# ``protocol=None`` + ``legacy_responses=True`` (the v8 default):
124128
# the wire is RESP3 but the Python surface mirrors RESP2 legacy
@@ -133,6 +137,7 @@ def _init_module_callbacks(self):
133137
SPELLCHECK_CMD: self._parse_spellcheck_resp3,
134138
CONFIG_CMD: self._parse_config_get_resp3_to_legacy,
135139
SYNDUMP_CMD: self._parse_syndump_resp3,
140+
ALIAS_LIST_CMD: self._parse_aliaslist,
136141
}
137142
# Search pipelines historically returned raw wire responses in
138143
# legacy mode. The default connection now uses RESP3 on the wire,
@@ -141,6 +146,14 @@ def _init_module_callbacks(self):
141146
self._RESP3_TO_RESP2_LEGACY_PIPELINE_CALLBACKS = {
142147
SEARCH_CMD: self._pipeline_parse_search_resp3_to_legacy,
143148
HYBRID_CMD: self._pipeline_parse_hybrid_search_resp3_to_legacy,
149+
ALIAS_LIST_CMD: self._parse_aliaslist,
150+
}
151+
# ``protocol=2`` + ``legacy_responses=True`` pipelines otherwise return
152+
# raw wire responses. ``FT.ALIASLIST`` is a new command with no pre-v8
153+
# raw pipeline shape to preserve, so it is still normalized to a ``set``
154+
# to match the direct-call behavior and the documented return type.
155+
self._RESP2_LEGACY_PIPELINE_CALLBACKS = {
156+
ALIAS_LIST_CMD: self._parse_aliaslist,
144157
}
145158
# ``legacy_responses=False`` + RESP2 wire: enhanced RESP2 parsers
146159
# producing the unified shape (``attributes`` as list of dicts,
@@ -155,6 +168,7 @@ def _init_module_callbacks(self):
155168
SPELLCHECK_CMD: self._parse_spellcheck,
156169
CONFIG_CMD: self._parse_config_get_unified,
157170
SYNDUMP_CMD: self._parse_syndump_unified,
171+
ALIAS_LIST_CMD: self._parse_aliaslist,
158172
}
159173
# ``legacy_responses=False`` + RESP3 wire: keeps the native RESP3
160174
# shape for commands whose unified shape diverges from the
@@ -261,6 +275,16 @@ def _parse_info(self, res, **kwargs):
261275
it = map(str_if_bytes, res)
262276
return dict(zip(it, it))
263277

278+
def _parse_aliaslist(self, res, **kwargs):
279+
# RESP2 replies with an array and RESP3 with a set; both are decoded
280+
# into a list/set by the parsers, so normalize to an unordered ``set``.
281+
# Alias names are user data, so they honor ``decode_responses`` (``str``
282+
# when decoding is enabled, ``bytes`` when it is not) — matching the
283+
# passthrough behavior of FT.TAGVALS and FT.DICTDUMP rather than being
284+
# force-decoded like the structural map keys handled by the other
285+
# search parsers.
286+
return set(res) if res else set()
287+
264288
def _parse_search(self, res, **kwargs):
265289
return Result(
266290
res,
@@ -1611,7 +1635,7 @@ def tagvals(self, tagfield: str):
16111635

16121636
return self.execute_command(TAGVALS_CMD, self.index_name, tagfield)
16131637

1614-
def aliasadd(self, alias: str):
1638+
def aliasadd(self, alias: KeyT):
16151639
"""
16161640
Alias a search index - will fail if alias already exists
16171641
@@ -1624,7 +1648,7 @@ def aliasadd(self, alias: str):
16241648

16251649
return self.execute_command(ALIAS_ADD_CMD, alias, self.index_name)
16261650

1627-
def aliasupdate(self, alias: str):
1651+
def aliasupdate(self, alias: KeyT):
16281652
"""
16291653
Updates an alias - will fail if alias does not already exist
16301654
@@ -1637,7 +1661,7 @@ def aliasupdate(self, alias: str):
16371661

16381662
return self.execute_command(ALIAS_UPDATE_CMD, alias, self.index_name)
16391663

1640-
def aliasdel(self, alias: str):
1664+
def aliasdel(self, alias: KeyT):
16411665
"""
16421666
Removes an alias to a search index
16431667
@@ -1649,6 +1673,23 @@ def aliasdel(self, alias: str):
16491673
""" # noqa
16501674
return self.execute_command(ALIAS_DEL_CMD, alias)
16511675

1676+
def aliaslist(self) -> Set[str | bytes]:
1677+
"""
1678+
List all aliases associated with the current index as an unordered set.
1679+
1680+
The index must be the name of an index created with ``FT.CREATE``; an
1681+
alias name is not accepted as a substitute. Returns an empty set when
1682+
the index exists but has no aliases. Alias names honor
1683+
``decode_responses`` (``str`` when decoding is enabled, ``bytes``
1684+
otherwise).
1685+
1686+
For more information see `FT.ALIASLIST <https://redis.io/commands/ft.aliaslist>`_.
1687+
""" # noqa
1688+
res = self.execute_command(ALIAS_LIST_CMD, self.index_name)
1689+
if isinstance(res, Pipeline):
1690+
return res
1691+
return self._parse_results(ALIAS_LIST_CMD, res)
1692+
16521693
def sugadd(self, key, *suggestions, **kwargs):
16531694
"""
16541695
Add suggestion terms to the AutoCompleter engine. Each suggestion has
@@ -1794,6 +1835,23 @@ async def info(self):
17941835
res = await self.execute_command(INFO_CMD, self.index_name)
17951836
return self._parse_results(INFO_CMD, res)
17961837

1838+
async def aliaslist(self) -> Set[str | bytes]:
1839+
"""
1840+
List all aliases associated with the current index as an unordered set.
1841+
1842+
The index must be the name of an index created with ``FT.CREATE``; an
1843+
alias name is not accepted as a substitute. Returns an empty set when
1844+
the index exists but has no aliases. Alias names honor
1845+
``decode_responses`` (``str`` when decoding is enabled, ``bytes``
1846+
otherwise).
1847+
1848+
For more information see `FT.ALIASLIST <https://redis.io/commands/ft.aliaslist>`_.
1849+
""" # noqa
1850+
res = await self.execute_command(ALIAS_LIST_CMD, self.index_name)
1851+
if isinstance(res, Pipeline):
1852+
return res
1853+
return self._parse_results(ALIAS_LIST_CMD, res)
1854+
17971855
async def search(
17981856
self,
17991857
query: Union[str, Query],

‎tests/test_asyncio/test_search.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,64 @@ async def test_alias_basic(self, decoded_r: redis.Redis):
935935
with pytest.raises(Exception):
936936
_ = (await alias_client2.search("*")).docs[0]
937937

938+
@pytest.mark.redismod
939+
@skip_if_server_version_lt("8.9.0")
940+
async def test_aliaslist(self, decoded_r: redis.Redis):
941+
index = decoded_r.ft("aliaslistidx")
942+
await index.create_index((TextField("txt"),))
943+
944+
# An existing index with no aliases returns an empty set, not an error.
945+
assert await index.aliaslist() == set()
946+
947+
# Aliases are returned as an unordered collection.
948+
await index.aliasadd("alias1")
949+
await index.aliasadd("alias2")
950+
aliases = await index.aliaslist()
951+
assert isinstance(aliases, set)
952+
assert aliases == {"alias1", "alias2"}
953+
954+
# FT.ALIASUPDATE moving an alias to another index removes it from the
955+
# previous index's listing.
956+
index2 = decoded_r.ft("aliaslistidx2")
957+
await index2.create_index((TextField("txt"),))
958+
await index2.aliasupdate("alias1")
959+
assert await index.aliaslist() == {"alias2"}
960+
assert await index2.aliaslist() == {"alias1"}
961+
962+
# FT.ALIASDEL removes the alias from the listing.
963+
await index.aliasdel("alias2")
964+
assert await index.aliaslist() == set()
965+
966+
# A missing index and an alias name supplied as the index both fail
967+
# with the index-not-found error; the client must not resolve aliases.
968+
with pytest.raises(redis.ResponseError):
969+
await decoded_r.ft("nonexistent_aliaslist_index").aliaslist()
970+
with pytest.raises(redis.ResponseError):
971+
await decoded_r.ft("alias1").aliaslist()
972+
973+
@pytest.mark.redismod
974+
@skip_if_server_version_lt("8.9.0")
975+
@skip_if_redis_enterprise()
976+
async def test_aliaslist_decode_responses_false(self, create_redis, stack_url):
977+
# ``decode_responses`` is not part of the CI protocol/legacy matrix,
978+
# so pin only that here and let the harness supply the protocol x
979+
# legacy combinations (as it does for ``test_aliaslist`` above).
980+
client = await create_redis(decode_responses=False, url=stack_url)
981+
index = client.ft("aliaslistbytesidx")
982+
await index.create_index((TextField("txt"),))
983+
984+
# ``aliasadd`` accepts a ``KeyT`` (str | bytes | memoryview); mix a
985+
# ``str`` and a ``bytes`` alias to exercise the widened input type.
986+
await index.aliasadd("alias1")
987+
await index.aliasadd(b"alias2")
988+
989+
# Alias names are user data, so with ``decode_responses=False`` they
990+
# are returned as ``bytes`` (honoring the flag, like FT.TAGVALS /
991+
# FT.DICTDUMP) rather than being force-decoded to ``str``.
992+
aliases = await index.aliaslist()
993+
assert isinstance(aliases, set)
994+
assert aliases == {b"alias1", b"alias2"}
995+
938996
@pytest.mark.redismod
939997
async def test_tags(self, decoded_r: redis.Redis):
940998
await decoded_r.ft().create_index((TextField("txt"), TagField("tags")))
@@ -2546,6 +2604,30 @@ async def test_search_commands_in_pipeline(self, decoded_r: redis.Redis):
25462604
== {"txt": "foo bar"}
25472605
)
25482606

2607+
@pytest.mark.redismod
2608+
@skip_if_redis_enterprise()
2609+
@skip_if_server_version_lt("8.9.0")
2610+
async def test_aliaslist_in_pipeline(self, decoded_r: redis.Redis):
2611+
index = decoded_r.ft("aliaslistpipeidx")
2612+
await index.create_index((TextField("txt"),))
2613+
2614+
# An empty listing normalizes to ``set()`` through the pipeline, not
2615+
# the raw ``[]`` wire response.
2616+
p = index.pipeline()
2617+
await p.aliaslist()
2618+
res = await p.execute()
2619+
assert isinstance(res[0], set)
2620+
assert res[0] == set()
2621+
2622+
# Aliases are returned as an unordered set, matching the direct call.
2623+
await index.aliasadd("alias1")
2624+
await index.aliasadd("alias2")
2625+
p = index.pipeline()
2626+
await p.aliaslist()
2627+
res = await p.execute()
2628+
assert isinstance(res[0], set)
2629+
assert res[0] == {"alias1", "alias2"}
2630+
25492631
@pytest.mark.redismod
25502632
@skip_if_server_version_lt("8.3.224")
25512633
async def test_hybrid_search_query_with_pipeline(self, decoded_r: redis.Redis):

‎tests/test_search.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,66 @@ def test_alias_basic(self, client):
893893
with pytest.raises(Exception):
894894
_ = alias_client2.search("*").docs[0]
895895

896+
@pytest.mark.redismod
897+
@skip_if_server_version_lt("8.9.0")
898+
def test_aliaslist(self, client):
899+
index = client.ft("aliaslistidx")
900+
index.create_index((TextField("txt"),))
901+
902+
# An existing index with no aliases returns an empty set, not an error.
903+
assert index.aliaslist() == set()
904+
905+
# Aliases are returned as an unordered collection.
906+
index.aliasadd("alias1")
907+
index.aliasadd("alias2")
908+
aliases = index.aliaslist()
909+
assert isinstance(aliases, set)
910+
assert aliases == {"alias1", "alias2"}
911+
912+
# FT.ALIASUPDATE moving an alias to another index removes it from the
913+
# previous index's listing.
914+
index2 = client.ft("aliaslistidx2")
915+
index2.create_index((TextField("txt"),))
916+
index2.aliasupdate("alias1")
917+
assert index.aliaslist() == {"alias2"}
918+
assert index2.aliaslist() == {"alias1"}
919+
920+
# FT.ALIASDEL removes the alias from the listing.
921+
index.aliasdel("alias2")
922+
assert index.aliaslist() == set()
923+
924+
# A missing index and an alias name supplied as the index both fail
925+
# with the index-not-found error; the client must not resolve aliases.
926+
with pytest.raises(redis.ResponseError):
927+
client.ft("nonexistent_aliaslist_index").aliaslist()
928+
with pytest.raises(redis.ResponseError):
929+
client.ft("alias1").aliaslist()
930+
931+
@pytest.mark.redismod
932+
@skip_if_server_version_lt("8.9.0")
933+
@skip_if_redis_enterprise()
934+
def test_aliaslist_decode_responses_false(self, request, stack_url):
935+
# ``decode_responses`` is not part of the CI protocol/legacy matrix,
936+
# so pin only that here and let the harness supply the protocol x
937+
# legacy combinations (as it does for ``test_aliaslist`` above).
938+
client = _get_client(
939+
redis.Redis, request, decode_responses=False, from_url=stack_url
940+
)
941+
index = client.ft("aliaslistbytesidx")
942+
index.create_index((TextField("txt"),))
943+
944+
# ``aliasadd`` accepts a ``KeyT`` (str | bytes | memoryview); mix a
945+
# ``str`` and a ``bytes`` alias to exercise the widened input type.
946+
index.aliasadd("alias1")
947+
index.aliasadd(b"alias2")
948+
949+
# Alias names are user data, so with ``decode_responses=False`` they
950+
# are returned as ``bytes`` (honoring the flag, like FT.TAGVALS /
951+
# FT.DICTDUMP) rather than being force-decoded to ``str``.
952+
aliases = index.aliaslist()
953+
assert isinstance(aliases, set)
954+
assert aliases == {b"alias1", b"alias2"}
955+
896956
@pytest.mark.redismod
897957
def test_textfield_sortable_nostem(self, client):
898958
# Creating the index definition with sortable and no_stem
@@ -3682,6 +3742,30 @@ def test_search_commands_in_pipeline(self, client):
36823742
== {"txt": "foo bar"}
36833743
)
36843744

3745+
@pytest.mark.redismod
3746+
@skip_if_redis_enterprise()
3747+
@skip_if_server_version_lt("8.9.0")
3748+
def test_aliaslist_in_pipeline(self, client):
3749+
index = client.ft("aliaslistpipeidx")
3750+
index.create_index((TextField("txt"),))
3751+
3752+
# An empty listing normalizes to ``set()`` through the pipeline, not
3753+
# the raw ``[]`` wire response.
3754+
p = index.pipeline()
3755+
p.aliaslist()
3756+
res = p.execute()
3757+
assert isinstance(res[0], set)
3758+
assert res[0] == set()
3759+
3760+
# Aliases are returned as an unordered set, matching the direct call.
3761+
index.aliasadd("alias1")
3762+
index.aliasadd("alias2")
3763+
p = index.pipeline()
3764+
p.aliaslist()
3765+
res = p.execute()
3766+
assert isinstance(res[0], set)
3767+
assert res[0] == {"alias1", "alias2"}
3768+
36853769
@pytest.mark.redismod
36863770
@skip_if_server_version_lt("8.4.0")
36873771
def test_hybrid_search_query_with_pipeline(self, client):

0 commit comments

Comments
 (0)