mirror of
https://github.com/matrix-org/pantalaimon.git
synced 2025-01-07 05:38:06 -05:00
client: Add the ability to order the search by recency.
This commit is contained in:
parent
88dbd2000a
commit
1e3d891a57
@ -607,6 +607,8 @@ class PanClient(AsyncClient):
|
|||||||
if order_by not in ["rank", "recent"]:
|
if order_by not in ["rank", "recent"]:
|
||||||
raise InvalidOrderByError(f"Invalid order by: {order_by}")
|
raise InvalidOrderByError(f"Invalid order by: {order_by}")
|
||||||
|
|
||||||
|
order_by_date = order_by == "recent"
|
||||||
|
|
||||||
before_limit = 0
|
before_limit = 0
|
||||||
after_limit = 0
|
after_limit = 0
|
||||||
include_profile = False
|
include_profile = False
|
||||||
@ -620,7 +622,8 @@ class PanClient(AsyncClient):
|
|||||||
include_profile = event_context.get("include_profile", False)
|
include_profile = event_context.get("include_profile", False)
|
||||||
|
|
||||||
searcher = self.index.searcher()
|
searcher = self.index.searcher()
|
||||||
search_func = partial(searcher.search, term, max_results=limit)
|
search_func = partial(searcher.search, term, max_results=limit,
|
||||||
|
order_by_date=order_by_date)
|
||||||
|
|
||||||
result = await loop.run_in_executor(None, search_func)
|
result = await loop.run_in_executor(None, search_func)
|
||||||
|
|
||||||
@ -646,7 +649,10 @@ class PanClient(AsyncClient):
|
|||||||
include_state
|
include_state
|
||||||
)
|
)
|
||||||
|
|
||||||
event_dict["rank"] = score
|
if order_by_date:
|
||||||
|
event_dict["rank"] = 1.0
|
||||||
|
else:
|
||||||
|
event_dict["rank"] = score
|
||||||
|
|
||||||
result_dict["results"].append(event_dict)
|
result_dict["results"].append(event_dict)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ def sanitize_room_id(room_id):
|
|||||||
|
|
||||||
class Searcher:
|
class Searcher:
|
||||||
def __init__(self, index, body_field, name_field, topic_field,
|
def __init__(self, index, body_field, name_field, topic_field,
|
||||||
column_field, room_field, searcher):
|
column_field, room_field, timestamp_field, searcher):
|
||||||
self._index = index
|
self._index = index
|
||||||
self._searcher = searcher
|
self._searcher = searcher
|
||||||
|
|
||||||
@ -34,14 +34,14 @@ class Searcher:
|
|||||||
self.topic_field = name_field
|
self.topic_field = name_field
|
||||||
self.column_field = column_field
|
self.column_field = column_field
|
||||||
self.room_field = room_field
|
self.room_field = room_field
|
||||||
|
self.timestamp_field = timestamp_field
|
||||||
|
|
||||||
def search(self, search_term, room=None, max_results=10):
|
def search(self, search_term, room=None, max_results=10, order_by_date=False):
|
||||||
# type (str, str) -> List[int, int]
|
# type (str, str, int, bool) -> List[int, int]
|
||||||
"""Search for events in the index.
|
"""Search for events in the index.
|
||||||
|
|
||||||
Returns the score and the column id for the event.
|
Returns the score and the column id for the event.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
queryparser = tantivy.QueryParser.for_index(
|
queryparser = tantivy.QueryParser.for_index(
|
||||||
self._index,
|
self._index,
|
||||||
[
|
[
|
||||||
@ -62,7 +62,11 @@ class Searcher:
|
|||||||
|
|
||||||
query = queryparser.parse_query(search_term)
|
query = queryparser.parse_query(search_term)
|
||||||
|
|
||||||
collector = tantivy.TopDocs(max_results)
|
if order_by_date:
|
||||||
|
collector = tantivy.TopDocs(max_results,
|
||||||
|
order_by_field=self.timestamp_field)
|
||||||
|
else:
|
||||||
|
collector = tantivy.TopDocs(max_results)
|
||||||
|
|
||||||
result = self._searcher.search(query, collector)
|
result = self._searcher.search(query, collector)
|
||||||
|
|
||||||
@ -85,7 +89,7 @@ class Index:
|
|||||||
self.topic_field = schema_builder.add_text_field("topic")
|
self.topic_field = schema_builder.add_text_field("topic")
|
||||||
|
|
||||||
self.timestamp_field = schema_builder.add_unsigned_field(
|
self.timestamp_field = schema_builder.add_unsigned_field(
|
||||||
"server_timestamp"
|
"server_timestamp", fast="single"
|
||||||
)
|
)
|
||||||
self.date_field = schema_builder.add_date_field(
|
self.date_field = schema_builder.add_date_field(
|
||||||
"message_date"
|
"message_date"
|
||||||
@ -146,5 +150,6 @@ class Index:
|
|||||||
self.topic_field,
|
self.topic_field,
|
||||||
self.column_field,
|
self.column_field,
|
||||||
self.room_field,
|
self.room_field,
|
||||||
|
self.timestamp_field,
|
||||||
self.reader.searcher()
|
self.reader.searcher()
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user