Skip to content

Commit

Permalink
update search
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Oct 26, 2023
1 parent dd04387 commit a95d351
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 59 deletions.
6 changes: 1 addition & 5 deletions src/rethink/controllers/schemas/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ class CursorQueryRequest(BaseModel):


class CursorQueryResponse(BaseModel):
class Result(BaseModel):
nodes: List[NodesInfoResponse.Data.NodeInfo]
query: Optional[str]

code: NonNegativeInt
message: str
requestId: str
result: Optional[Result]
nodes: List[NodesInfoResponse.Data.NodeInfo]


class AddToRecentSearchHistRequest(BaseModel):
Expand Down
24 changes: 10 additions & 14 deletions src/rethink/controllers/search/node_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def cursor_query(
code=td.code.value,
message=const.get_msg_by_code(td.code, td.language),
requestId=req.requestId,
result=None
nodes=None
)
query, recommended_nodes = models.node.cursor_query(
recommended_nodes = models.node.cursor_query(
uid=td.uid,
nid=req.nid,
cursor_text=req.textBeforeCursor,
Expand All @@ -24,18 +24,14 @@ def cursor_query(
code=code.value,
message=const.get_msg_by_code(code, td.language),
requestId=req.requestId,
result=schemas.search.CursorQueryResponse.Result(
nodes=[schemas.node.NodesInfoResponse.Data.NodeInfo(
id=n["id"],
title=n["title"],
snippet=n["snippet"],
type=n["type"],
createdAt=datetime2str(n["_id"].generation_time),
modifiedAt=datetime2str(n["modifiedAt"]),
) for n in recommended_nodes
],
query=query,
)
nodes=[schemas.node.NodesInfoResponse.Data.NodeInfo(
id=n["id"],
title=n["title"],
snippet=n["snippet"],
type=n["type"],
createdAt=datetime2str(n["_id"].generation_time),
modifiedAt=datetime2str(n["modifiedAt"]),
) for n in recommended_nodes],
)


Expand Down
24 changes: 13 additions & 11 deletions src/rethink/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,27 @@ def cursor_query(
uid: str,
nid: str,
cursor_text: str,
) -> Tuple[Optional[str], List[tps.Node]]:
if cursor_text.startswith("@"):
query = cursor_text[1:].strip()
else:
found = CURSOR_AT_PTN.search(cursor_text)
if found is None:
return None, []
query = found.group(1).strip()
) -> List[tps.Node]:
# if cursor_text.startswith("@"):
# query = cursor_text[1:].strip()
# else:
# found = CURSOR_AT_PTN.search(cursor_text)
# if found is None:
# return None, []
# query = found.group(1).strip()

query = cursor_text.strip()

if query == "":
u = COLL.users.find_one({"id": uid})
if u is None:
return query, []
return []
rn = u["recentCursorSearchSelectedNIds"]
try:
rn.remove(nid)
except ValueError:
pass
return query, sorted(list(COLL.nodes.find({"id": {"$in": rn}})), key=lambda x: rn.index(x["id"]))
return sorted(list(COLL.nodes.find({"id": {"$in": rn}})), key=lambda x: rn.index(x["id"]))
nodes_found, _ = user_node(
uid=uid,
query=query,
Expand All @@ -226,7 +228,7 @@ def cursor_query(
page_size=8,
nid_exclude=[nid],
)
return query, nodes_found
return nodes_found


def to_trash(uid: str, nid: str) -> const.Code:
Expand Down
7 changes: 3 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,15 @@ def test_node(self):
json={
"requestId": "xxx",
"nid": node["id"],
"textBeforeCursor": "@How",
"textBeforeCursor": "How",
},
headers={"token": self.token},
)
rj = resp.json()
self.assertEqual(0, rj["code"])
self.assertEqual("xxx", rj["requestId"])
self.assertEqual("How", rj["result"]["query"])
self.assertEqual(1, len(rj["result"]["nodes"]))
self.assertEqual("How do I record", rj["result"]["nodes"][0]["title"])
self.assertEqual(1, len(rj["nodes"]))
self.assertEqual("How do I record", rj["nodes"][0]["title"])

self.client.put(
"/api/trash",
Expand Down
30 changes: 5 additions & 25 deletions tests/test_models_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,48 +176,28 @@ def test_cursor_text(self):
)
self.assertEqual(const.Code.OK, code)

q, recom = models.node.cursor_query(
recom = models.node.cursor_query(
uid=self.uid,
nid=n2["id"],
cursor_text="te",
)
self.assertEqual(0, len(recom))
self.assertEqual(None, q)

q, recom = models.node.cursor_query(
uid=self.uid,
nid=n2["id"],
cursor_text="@te",
)
self.assertEqual(2, len(recom))
self.assertEqual("te", q)

q, recom = models.node.cursor_query(
uid=self.uid,
nid=n2["id"], # exclude the second node
cursor_text="sw21 @te",
)
self.assertEqual("te", q)
self.assertEqual(2, len(recom))
self.assertEqual("title", recom[1]["title"])

q, recom = models.node.cursor_query(
recom = models.node.cursor_query(
uid=self.uid,
nid=n2["id"], # exclude the second node
cursor_text="sw21 @",
cursor_text="",
)
self.assertEqual("", q)
self.assertEqual(2, len(recom))

code = models.search.add_recent_cursor_search(self.uid, n1["id"], n2["id"])
self.assertEqual(const.Code.OK, code)

q, recom = models.node.cursor_query(
recom = models.node.cursor_query(
uid=self.uid,
nid=n1["id"], # exclude the second node
cursor_text="sw21 @",
cursor_text="",
)
self.assertEqual("", q)
self.assertEqual(3, len(recom))
self.assertEqual("Welcome to Rethink", recom[2]["title"])

Expand Down

0 comments on commit a95d351

Please sign in to comment.