changeset 1465:f61493ff5e10

added support for xapwrap for collapsing and sorting by relevance/key, ensure to just show the newest revision in _getHits if historysearch is disabled
author Franz Pletz <fpletz AT franz-pletz DOT org>
date Wed, 23 Aug 2006 14:38:24 +0200
parents 446e20f5005c
children 500e043cf7cd
files MoinMoin/search/Xapian.py MoinMoin/search/builtin.py MoinMoin/support/xapwrap/index.py
diffstat 3 files changed, 26 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/Xapian.py	Wed Aug 23 12:02:50 2006 +0200
+++ b/MoinMoin/search/Xapian.py	Wed Aug 23 14:38:24 2006 +0200
@@ -197,7 +197,7 @@
         """ Check if the Xapian index exists """
         return BaseIndex.exists(self) and os.listdir(self.dir)
 
-    def _search(self, query, sort=None):
+    def _search(self, query, sort=None, historysearch=0):
         """ read lock must be acquired """
         while True:
             try:
@@ -217,6 +217,7 @@
             # XXX: we need real weight here, like _moinSearch
             # (TradWeight in xapian)
             kw['sortByRelevence'] = True
+            kw['sortKey'] = 'revision'
         if sort == 'page_name':
             kw['sortKey'] = 'pagename'
 
@@ -468,10 +469,12 @@
                 mimetype, att_content = self.contentfilter(filename)
                 xmimetype = xapdoc.Keyword('mimetype', mimetype)
                 xcontent = xapdoc.TextField('content', att_content)
+                xdomains = [xapdoc.Keyword('domain', domain)
+                        for domain in domains]
                 doc = xapdoc.Document(textFields=(xcontent, ),
-                                      keywords=(xatt_itemid, xtitle,
-                                          xlanguage, xstem_language,
-                                          xmimetype, ),
+                                      keywords=xdomains + [xatt_itemid,
+                                          xtitle, xlanguage, xstem_language,
+                                          xmimetype, ],
                                       sortFields=(xpname, xattachment, xmtime,
                                           xwname, xrev, ),
                                      )
--- a/MoinMoin/search/builtin.py	Wed Aug 23 12:02:50 2006 +0200
+++ b/MoinMoin/search/builtin.py	Wed Aug 23 14:38:24 2006 +0200
@@ -423,7 +423,8 @@
                 self.request.log("xapianSearch: query = %r" %
                         query.get_description())
                 query = xapwrap.index.QObjQuery(query)
-                enq, mset, hits = index.search(query, sort=self.sort)
+                enq, mset, hits = index.search(query, sort=self.sort,
+                        historysearch=self.historysearch)
                 clock.stop('_xapianQuery')
                 #self.request.log("xapianSearch: finds: %r" % hits)
                 def dict_decode(d):
@@ -506,6 +507,7 @@
     def _getHits(self, pages, matchSearchFunction):
         """ Get the hit tuples in pages through matchSearchFunction """
         hits = []
+        revisionCache = {}
         fs_rootpage = self.fs_rootpage
         for hit in pages:
             if 'values' in hit:
@@ -538,7 +540,13 @@
                 else:
                     matches = matchSearchFunction(page, uid)
                     if matches:
+                        if not self.historysearch and \
+                                pagename in revisionCache and \
+                                revisionCache[pagename][0] < revision:
+                            hits.remove(revisionCache[pagename][1])
+                            del revisionCache[pagename]
                         hits.append((wikiname, page, attachment, matches))
+                        revisionCache[pagename] = (revision, hits[-1])
             else: # other wiki
                 hits.append((wikiname, pagename, attachment, None, revision))
         return hits
--- a/MoinMoin/support/xapwrap/index.py	Wed Aug 23 12:02:50 2006 +0200
+++ b/MoinMoin/support/xapwrap/index.py	Wed Aug 23 14:38:24 2006 +0200
@@ -562,7 +562,8 @@
                batchSize = MAX_DOCS_TO_RETURN,
                sortIndex = None, sortAscending = True,
                sortByRelevence = False,
-               valuesWanted = None):
+               valuesWanted = None,
+               collapseKey = None):
         """
         Search an index.
 
@@ -586,6 +587,8 @@
         # the only thing we use sortKey for is to set sort index
         if sortKey is not None:
             sortIndex = self.indexValueMap[sortKey]
+        if collapseKey is not None:
+            collapseKey = self.indexValueMap[collapseKey]
 
         # once you call set_sorting on an Enquire instance, there is no
         # way to resort it by relevence, so we have to open a new
@@ -612,11 +615,16 @@
                     del self._searchSessions[qString]
                     self._searchSessions[qString] = (self.enquire(q), None)
                     enq, lastIndexSortedBy = self._searchSessions[qString]
-            if sortIndex is not None:
+            if sortByRelevence is not None and sortIndex is not None:
+                enq.set_sort_by_relevance_then_value(sortIndex, not sortAscending)
+            elif sortIndex is not None:
                 # It seems that we have the opposite definition of sort ascending
                 # than Xapian so we invert the ascending flag!
                 enq.set_sort_by_value(sortIndex, not sortAscending)
 
+            if collapseKey is not None:
+                enq.set_collapse_key(collapseKey)
+
             self._searchSessions[qString] = (enq, sortIndex)
 
             mset = enq.get_mset(startingIndex, batchSize)