Skip to content

Commit

Permalink
re-organize search
Browse files Browse the repository at this point in the history
  • Loading branch information
MorvanZhou committed Oct 17, 2023
1 parent 82b45cc commit ed40842
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 327 deletions.
13 changes: 3 additions & 10 deletions src/rethink/controllers/node/trash_ops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rethink import const, models
from rethink.controllers import schemas
from rethink.controllers.utils import TokenDecode, datetime2str
from rethink.controllers.utils import TokenDecode


def move_to_trash(
Expand All @@ -27,20 +27,13 @@ def get_from_trash(
ps: int = 10,
rid: str = "",
) -> schemas.node.GetFromTrashResponse:
ns = models.node.get_nodes_in_trash(uid=td.uid, page=p, page_size=ps)
ns, total = models.node.get_nodes_in_trash(uid=td.uid, page=p, page_size=ps)
code = const.Code.OK
return schemas.node.GetFromTrashResponse(
requestId=rid,
code=code.value,
message=const.get_msg_by_code(code, td.language),
nodes=[schemas.node.NodesInfoResponse.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 ns],
data=schemas.node.parse_nodes_info(ns, total)
)


Expand Down
39 changes: 30 additions & 9 deletions src/rethink/controllers/schemas/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pydantic import BaseModel, NonNegativeInt, Field

from ..utils import datetime2str


class NodeData(BaseModel):
class LinkedNode(BaseModel):
Expand Down Expand Up @@ -53,18 +55,22 @@ class UpdateRequest(BaseModel):


class NodesInfoResponse(BaseModel):
class NodeInfo(BaseModel):
id: str
title: str
snippet: str
type: int
createdAt: str
modifiedAt: str
class Data(BaseModel):
class NodeInfo(BaseModel):
id: str
title: str
snippet: str
type: int
createdAt: str
modifiedAt: str

nodes: List[NodeInfo]
total: NonNegativeInt

code: NonNegativeInt
message: str
requestId: str
nodes: List[NodeInfo]
data: Data


class RestoreFromTrashRequest(BaseModel):
Expand All @@ -82,4 +88,19 @@ class GetFromTrashResponse(BaseModel):
code: NonNegativeInt
message: str
requestId: str
nodes: List[NodesInfoResponse.NodeInfo]
data: NodesInfoResponse.Data


def parse_nodes_info(nodes, total):
data = NodesInfoResponse.Data(
nodes=[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 nodes],
total=total,
)
return data
9 changes: 8 additions & 1 deletion src/rethink/controllers/schemas/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CursorQueryRequest(BaseModel):

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

code: NonNegativeInt
Expand All @@ -36,3 +36,10 @@ class AddToRecentSearchHistRequest(BaseModel):
requestId: str
nid: str
toNid: str


class RecentSearchQueriesResponse(BaseModel):
code: NonNegativeInt
message: str
requestId: str
queries: List[str]
34 changes: 24 additions & 10 deletions src/rethink/controllers/search/node_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def search_user_nodes(
requestId=req.requestId,
nodes=[],
)
nodes = models.node.search_user_node(
nodes, total = models.search.user_node(
uid=td.uid,
query=req.query,
sort_key=req.sortKey,
Expand All @@ -64,14 +64,7 @@ def search_user_nodes(
code=code.value,
message=const.get_msg_by_code(code, td.language),
requestId=req.requestId,
nodes=[schemas.node.NodesInfoResponse.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 nodes],
data=schemas.node.parse_nodes_info(nodes, total),
)


Expand All @@ -85,9 +78,30 @@ def add_to_recent_search_history(
message=const.get_msg_by_code(td.code, td.language),
requestId=req.requestId,
)
code = models.node.add_to_recent_history(uid=td.uid, nid=req.nid, to_nid=req.toNid)
code = models.search.add_recent_search_nid(uid=td.uid, nid=req.nid, to_nid=req.toNid)
return schemas.base.AcknowledgeResponse(
code=code.value,
message=const.get_msg_by_code(code, td.language),
requestId=req.requestId,
)


def get_recent_search_queries(
td: TokenDecode,
rid: str,
) -> schemas.search.RecentSearchQueriesResponse:
if td.code != const.Code.OK:
return schemas.search.RecentSearchQueriesResponse(
code=td.code.value,
message=const.get_msg_by_code(td.code, td.language),
requestId=rid,
queries=[],
)
queries = models.search.get_recent_search_queries(uid=td.uid)
code = const.Code.OK
return schemas.search.RecentSearchQueriesResponse(
code=code.value,
message=const.get_msg_by_code(code, td.language),
requestId=rid,
queries=queries,
)
1 change: 1 addition & 0 deletions src/rethink/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
database,
node,
user,
search,
tps,
db_ops,
)
1 change: 1 addition & 0 deletions src/rethink/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def create_node(text, title):
"nickname": const.DEFAULT_USER["nickname"],
"modifiedAt": datetime.datetime.now(tz=utc),
"recentSearchedNodeIds": [n0["id"], n1["id"]],
"recentSearchQueries": [],
"language": language,
}
_ = COLL.users.insert_one(u)
Expand Down
113 changes: 8 additions & 105 deletions src/rethink/models/node.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import datetime
import re
from typing import List, Optional, Sequence, Set, Tuple
from typing import List, Optional, Set, Tuple

from bson import ObjectId
from bson.tz_util import utc

from rethink import config, const
from . import user, tps, utils, db_ops
from .database import COLL
from .search import user_node

AT_PTN = re.compile(r'\[@[\w ]+?]\(([\w/]+?)\)', re.MULTILINE)
TITLE_PTN = re.compile(r'^#*?\s*?(.+?)\s*$', re.MULTILINE)
Expand Down Expand Up @@ -210,7 +211,7 @@ def cursor_query(
except ValueError:
pass
return query, list(COLL.nodes.find({"id": {"$in": rn}}))
return query, search_user_node(
nodes_found, _ = user_node(
uid=uid,
query=query,
sort_key="modifiedAt",
Expand All @@ -219,6 +220,7 @@ def cursor_query(
page_size=8,
nid_exclude=[nid],
)
return query, nodes_found


def to_trash(uid: str, nid: str) -> const.Code:
Expand Down Expand Up @@ -247,22 +249,22 @@ def to_trash(uid: str, nid: str) -> const.Code:
return const.Code.OK


def get_nodes_in_trash(uid: str, page: int, page_size: int) -> List[tps.Node]:
def get_nodes_in_trash(uid: str, page: int, page_size: int) -> Tuple[List[tps.Node], int]:
unids, code = user.get_node_ids(uid=uid)
if code != const.Code.OK:
return []
return [], 0

condition = {
"id": {"$in": unids},
"disabled": False,
"inTrash": True,
}
docs = COLL.nodes.find(condition).sort("inTrashAt", direction=-1)

total = COLL.nodes.count_documents(condition)
if page_size > 0:
docs = docs.skip(page * page_size).limit(page_size)

return list(docs)
return list(docs), total


def restore_from_trash(uid: str, nid: str) -> const.Code:
Expand Down Expand Up @@ -321,106 +323,7 @@ def disable(
return const.Code.OK


def search_user_node(
uid: str,
query: str = "",
sort_key: str = "createAt",
sort_order: int = -1,
page: int = 0,
page_size: int = 0,
nid_exclude: Sequence[str] = None,
) -> List[tps.Node]:
unids, code = user.get_node_ids(uid=uid)
if code != const.Code.OK:
return []

if nid_exclude is None or len(nid_exclude) == 0:
nids = unids
else:
nid_exclude = nid_exclude or []
nid_exclude = set(nid_exclude)
nids = set(unids)
nids.difference_update(nid_exclude)
nids = list(nids)

condition = {
"id": {"$in": nids},
"disabled": False,
"inTrash": False,
}
query = query.strip().lower()

# on remote mongodb
if query != "" and not config.is_local_db():
condition["$or"] = [
{"searchKeys": {"$regex": query}},
{"text": {"$regex": query}},
]

docs = COLL.nodes.find(condition)

if sort_key != "":
if sort_key == "createAt":
sort_key = "_id"
elif sort_key == "similarity":
sort_key = "_id" # TODO: sort by similarity
docs = docs.sort(sort_key, direction=sort_order)

if page_size > 0:
docs = docs.skip(page * page_size).limit(page_size)

if config.is_local_db() and query != "":
return [doc for doc in docs if query in doc["searchKeys"] or query in doc["text"]]
return list(docs)


def add_to_recent_history(
uid: str,
nid: str,
to_nid: str,
) -> const.Code:
# add selected node to recentSearchedNodeIds
user_c = {"id": uid}
unid_c = {"id": uid}

# on remote mongodb
if not config.is_local_db():
user_c.update({"disabled": False})
unid_c.update({"nodeIds": {"$in": [nid, to_nid]}})

# try finding user
u = COLL.users.find_one(user_c)
if u is None:
return const.Code.ACCOUNT_OR_PASSWORD_ERROR

# try finding node
unids = COLL.unids.find_one(unid_c)
if unids is None:
return const.Code.NODE_NOT_EXIST

# do it on local db
if config.is_local_db():
if u["disabled"]:
return const.Code.ACCOUNT_OR_PASSWORD_ERROR
if nid not in unids["nodeIds"] or to_nid not in unids["nodeIds"]:
return const.Code.NODE_NOT_EXIST

rns = u["recentSearchedNodeIds"]
if to_nid in rns:
rns.remove(to_nid)
rns.insert(0, to_nid)
if len(rns) > 10:
rns = rns[:10]

# add to recentSearchedNodeIds
res = COLL.users.update_one(
{"id": uid},
{"$set": {"recentSearchedNodeIds": rns}}
)
if res.matched_count != 1:
return const.Code.OPERATION_FAILED

return const.Code.OK


def new_user_add_default_nodes(language: str, uid: str) -> const.Code:
Expand Down
Loading

0 comments on commit ed40842

Please sign in to comment.