From 3d9e1a05dc04382d78a6b20dbcee17f9ea09c612 Mon Sep 17 00:00:00 2001 From: "lvyanqi.lyq" Date: Fri, 10 Mar 2023 16:25:58 +0800 Subject: [PATCH] TairSearch: add tft.analyzer Link: https://code.alibaba-inc.com/tair3.0/tair-py/codereview/11933250 --- tair/tairsearch.py | 19 ++++++++++++++- tests/test_asyncio/test_tairsearch.py | 2 +- tests/test_tairsearch.py | 34 +++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/tair/tairsearch.py b/tair/tairsearch.py index 8c8768b..42eaa32 100644 --- a/tair/tairsearch.py +++ b/tair/tairsearch.py @@ -1,5 +1,6 @@ from typing import Dict, Iterable, List, Optional +import tair from tair.typing import CommandsProtocol, EncodableT, KeyT, ResponseT @@ -141,7 +142,7 @@ def tft_scandocid( return self.execute_command("TFT.SCANDOCID", *pieces) def tft_deldoc(self, index: KeyT, doc_id: Iterable[str]) -> ResponseT: - return self.execute_command("TFT.DELDOC", index, *doc_id, ) + return self.execute_command("TFT.DELDOC", index, *doc_id) def tft_delall(self, index: KeyT) -> ResponseT: return self.execute_command("TFT.DELALL", index) @@ -155,6 +156,22 @@ def tft_search(self, index: KeyT, query: str, use_cache: bool = False) -> Respon def tft_msearch(self, index_count: int, index: Iterable[KeyT], query: str) -> ResponseT: return self.execute_command("TFT.MSEARCH", index_count, *index, query) + def tft_analyzer(self, analyzer_name: str, text: str, index: Optional[KeyT] = None, + show_time: Optional[bool] = False) -> ResponseT: + pieces: List[EncodableT] = [analyzer_name, text] + if index is not None: + pieces.append("INDEX") + pieces.append(index) + if show_time: + pieces.append("show_time") + target_nodes = None + if isinstance(self, tair.TairCluster): + if index is None: + target_nodes = 'random' + else: + target_nodes = self.nodes_manager.get_node_from_slot(self.keyslot(index)) + return self.execute_command("TFT.ANALYZER", *pieces, target_nodes=target_nodes) + def tft_addsug(self, index: KeyT, mapping: Dict[str, int]) -> ResponseT: pieces: List[EncodableT] = [index] diff --git a/tests/test_asyncio/test_tairsearch.py b/tests/test_asyncio/test_tairsearch.py index 8bbb8aa..4fefc63 100644 --- a/tests/test_asyncio/test_tairsearch.py +++ b/tests/test_asyncio/test_tairsearch.py @@ -314,7 +314,7 @@ async def test_tft_deldoc(self, t: Tair): assert await t.tft_createindex(index, mappings) assert await t.tft_adddoc(index, document, doc_id="00001") == '{"_id":"00001"}' - assert await t.tft_deldoc(index, "00001", "00002") == 1 + assert await t.tft_deldoc(index, {"00001", "00002"}) == 1 @pytest.mark.asyncio async def test_tft_delall(self, t: Tair): diff --git a/tests/test_tairsearch.py b/tests/test_tairsearch.py index ce08099..8633442 100644 --- a/tests/test_tairsearch.py +++ b/tests/test_tairsearch.py @@ -457,8 +457,8 @@ def test_tft_search(self, t: Tair): t.delete(index) def test_tft_msearch(self, t: Tair): - index1 = "idx_" + str(uuid.uuid4()) - index2 = "idx_" + str(uuid.uuid4()) + index1 = "{idx}_" + str(uuid.uuid4()) + index2 = "{idx}_" + str(uuid.uuid4()) mappings = """ { "mappings": { @@ -523,6 +523,36 @@ def test_tft_msearch(self, t: Tair): t.delete(index1) t.delete(index2) + def test_tft_analyzer(self, t: Tair): + index = "idx_" + str(uuid.uuid4()) + mappings = """ +{ + "mappings":{ + "properties":{ + "f0":{ + "type":"text", + "analyzer":"my_analyzer" + } + } + }, + "settings":{ + "analysis":{ + "analyzer":{ + "my_analyzer":{ + "type":"standard" + } + } + } + } +}""" + text = 'This is tair-py.' + + assert t.tft_createindex(index, mappings) + assert t.tft_analyzer("standard", text) == t.tft_analyzer("my_analyzer", text, index) + assert 'consuming time' in str(t.tft_analyzer("standard", text, None, True)) + + t.delete(index) + def test_tft_addsug(self, t: Tair): index = "idx_" + str(uuid.uuid4())