This is an automated email from the ASF dual-hosted git repository.
sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git
The following commit(s) were added to refs/heads/master by this push:
new 77d454e Get oldest and youngest in single query
77d454e is described below
commit 77d454edd1ec3c94b137e221d271eed583362080
Author: Sebb <[email protected]>
AuthorDate: Sat Sep 18 12:59:03 2021 +0100
Get oldest and youngest in single query
---
server/plugins/messages.py | 29 +++++++++++++----------------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/server/plugins/messages.py b/server/plugins/messages.py
index 0f1bab8..6bebf2c 100644
--- a/server/plugins/messages.py
+++ b/server/plugins/messages.py
@@ -445,27 +445,24 @@ async def get_years(session, query_defuzzed):
{"bool": {"should": [{"term": {"private": False}}, {"terms":
{"list_raw": private_lists_accessible}}]}}
]
- # Get oldest doc
+ # Get oldest and youngest doc in single scan
res = await session.database.search(
index=session.database.dbs.mbox,
- size=1,
- body={"query": {"bool": query_defuzzed}, "sort": {"epoch": "asc"}},
+ size=0,
+ body={"query": {"bool": query_defuzzed},
+ "aggs": {
+ "first": {"min": {"field": "epoch"}},
+ "last": {"max": {"field": "epoch"}},
+ },
+ }
)
- oldest = datetime.datetime.fromtimestamp(0)
- if res["hits"]["hits"]:
- doc = res["hits"]["hits"][0]
- oldest = datetime.datetime.fromtimestamp(doc["_source"]["epoch"])
- # Get youngest doc
- res = await session.database.search(
- index=session.database.dbs.mbox,
- size=1,
- body={"query": {"bool": query_defuzzed}, "sort": {"epoch": "desc"}}
- )
+ oldest = datetime.datetime.fromtimestamp(0)
youngest = datetime.datetime.fromtimestamp(0)
- if res["hits"]["hits"]:
- doc = res["hits"]["hits"][0]
- youngest = datetime.datetime.fromtimestamp(doc["_source"]["epoch"])
+ if res["aggregations"]:
+ aggs = res["aggregations"]
+ oldest = datetime.datetime.fromtimestamp(aggs["first"]["value"])
+ youngest = datetime.datetime.fromtimestamp(aggs["last"]["value"])
return oldest.year, youngest.year, oldest.month, youngest.month