From 7aa29a8d496fe00c6f99f93a00c1508dff00ed6d Mon Sep 17 00:00:00 2001
From: Masahiro Ikeda <ikedamsh@oss.nttdata.com>
Date: Sat, 6 Feb 2021 17:01:50 +0900
Subject: [PATCH] [wip] add an search parameter to aggregate into threads

---
 django/archives/mailarchives/views.py | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/django/archives/mailarchives/views.py b/django/archives/mailarchives/views.py
index 78f4564..d71b6a6 100644
--- a/django/archives/mailarchives/views.py
+++ b/django/archives/mailarchives/views.py
@@ -756,6 +756,9 @@ def search(request):
     else:
         list_sort = 'r'
 
+    if 't' in request.POST:
+        aggregate = request.POST['t'] == "1"
+
     # Ok, we have all we need to do the search
 
     if query.find('@') > 0:
@@ -775,7 +778,12 @@ def search(request):
         # If not found, fall through to a regular search
 
     curs.execute("SET gin_fuzzy_search_limit=10000")
-    qstr = "SELECT messageid, date, subject, _from, ts_rank_cd(fti, plainto_tsquery('public.pg', %(q)s)), ts_headline(bodytxt, plainto_tsquery('public.pg', %(q)s),'StartSel=\"[[[[[[\",StopSel=\"]]]]]]\"') FROM messages m WHERE fti @@ plainto_tsquery('public.pg', %(q)s)"
+    qstr = "SELECT messageid, date, subject, _from, ts_rank_cd(fti, plainto_tsquery('public.pg', %(q)s)), ts_headline(bodytxt, plainto_tsquery('public.pg', %(q)s),'StartSel=\"[[[[[[\",StopSel=\"]]]]]]\"') FROM"
+    if aggregate:
+        qstr += " (SELECT messageid, date, subject, _from, fti, bodytxt, RANK() OVER (PARTITION BY threadid ORDER BY ts_rank_cd(fti, plainto_tsquery('wal writer')) DESC, id) AS rank FROM messages m"
+    else:
+        qstr += " messages m"
+    qstr += " WHERE fti @@ plainto_tsquery('public.pg', %(q)s)"
     params = {
         'q': query,
     }
@@ -785,6 +793,8 @@ def search(request):
     if firstdate:
         qstr += " AND m.date > %(date)s"
         params['date'] = firstdate
+    if aggregate:
+        qstr += ") m1 WHERE m1.rank = 1"
     if list_sort == 'r':
         qstr += " ORDER BY ts_rank_cd(fti, plainto_tsquery(%(q)s)) DESC LIMIT 1000"
     elif list_sort == 'd':
-- 
2.25.1

