From 1e3d891a576d06b098db2f481a5f458158bd8db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 10 Jun 2019 16:12:45 +0200 Subject: [PATCH] client: Add the ability to order the search by recency. --- pantalaimon/client.py | 10 ++++++++-- pantalaimon/index.py | 17 +++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pantalaimon/client.py b/pantalaimon/client.py index ad68935..63f3173 100644 --- a/pantalaimon/client.py +++ b/pantalaimon/client.py @@ -607,6 +607,8 @@ class PanClient(AsyncClient): if order_by not in ["rank", "recent"]: raise InvalidOrderByError(f"Invalid order by: {order_by}") + order_by_date = order_by == "recent" + before_limit = 0 after_limit = 0 include_profile = False @@ -620,7 +622,8 @@ class PanClient(AsyncClient): include_profile = event_context.get("include_profile", False) 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) @@ -646,7 +649,10 @@ class PanClient(AsyncClient): 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) diff --git a/pantalaimon/index.py b/pantalaimon/index.py index 5ba472a..5d62191 100644 --- a/pantalaimon/index.py +++ b/pantalaimon/index.py @@ -25,7 +25,7 @@ def sanitize_room_id(room_id): class Searcher: 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._searcher = searcher @@ -34,14 +34,14 @@ class Searcher: self.topic_field = name_field self.column_field = column_field self.room_field = room_field + self.timestamp_field = timestamp_field - def search(self, search_term, room=None, max_results=10): - # type (str, str) -> List[int, int] + def search(self, search_term, room=None, max_results=10, order_by_date=False): + # type (str, str, int, bool) -> List[int, int] """Search for events in the index. Returns the score and the column id for the event. """ - queryparser = tantivy.QueryParser.for_index( self._index, [ @@ -62,7 +62,11 @@ class Searcher: 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) @@ -85,7 +89,7 @@ class Index: self.topic_field = schema_builder.add_text_field("topic") self.timestamp_field = schema_builder.add_unsigned_field( - "server_timestamp" + "server_timestamp", fast="single" ) self.date_field = schema_builder.add_date_field( "message_date" @@ -146,5 +150,6 @@ class Index: self.topic_field, self.column_field, self.room_field, + self.timestamp_field, self.reader.searcher() )