Skip to content

Commit

Permalink
TairSearch: add tft.analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
lyq2333 authored and yangbodong22011 committed Mar 10, 2023
1 parent 57a9a66 commit 3d9e1a0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
19 changes: 18 additions & 1 deletion tair/tairsearch.py
@@ -1,5 +1,6 @@
from typing import Dict, Iterable, List, Optional

import tair
from tair.typing import CommandsProtocol, EncodableT, KeyT, ResponseT


Expand Down Expand Up @@ -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)
Expand All @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_asyncio/test_tairsearch.py
Expand Up @@ -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):
Expand Down
34 changes: 32 additions & 2 deletions tests/test_tairsearch.py
Expand Up @@ -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": {
Expand Down Expand Up @@ -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())

Expand Down

0 comments on commit 3d9e1a0

Please sign in to comment.