changeset 3678:0cbd8b41aebd

Xapian search: fix searching for negative terms only
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 08 Jun 2008 01:29:49 +0200
parents aba86e157059
children 78850075daad
files MoinMoin/search/queryparser.py docs/CHANGES
diffstat 2 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/queryparser.py	Sat Jun 07 21:48:06 2008 +0200
+++ b/MoinMoin/search/queryparser.py	Sun Jun 08 01:29:49 2008 +0200
@@ -213,23 +213,28 @@
                 not_terms.append(term.xapian_term(request, allterms))
 
         # prepare query for not negated terms
-        if len(terms) == 1:
+        if not terms:
+            t1 = None
+        elif len(terms) == 1:
             t1 = Query(terms[0])
         else:
             t1 = Query(Query.OP_AND, terms)
 
-        # negated terms?
+        # prepare query for negated terms
         if not not_terms:
-            # no, just return query for not negated terms
-            return t1
-
-        # yes, link not negated and negated terms' query with a AND_NOT query
-        if len(not_terms) == 1:
+            t2 = None
+        elif len(not_terms) == 1:
             t2 = Query(not_terms[0])
         else:
             t2 = Query(Query.OP_OR, not_terms)
 
-        return Query(Query.OP_AND_NOT, t1, t2)
+        if t1 and not t2:
+            return t1
+        elif t2 and not t1:
+            return Query(Query.OP_AND_NOT, Query(""), t2)  # Query("") == MatchAll
+        else:
+            # yes, link not negated and negated terms' query with a AND_NOT query
+            return Query(Query.OP_AND_NOT, t1, t2)
 
 
 class OrExpression(AndExpression):
@@ -1028,9 +1033,10 @@
             elif isinstance(item, list):
                 # strip off the opening parenthesis
                 terms.append(self._analyse_items(item[1:]))
-        sub = terms.subterms()
-        if len(sub) == 1:
-            return sub[0]
+
+        # Note: do NOT reduce "terms" when it has a single subterm only!
+        # Doing that would break "-someterm" searches as we rely on AndExpression
+        # doing a "MatchAll AND_NOT someterm" for that case!
         return terms
 
     def parse_query(self, query):
@@ -1044,7 +1050,9 @@
                                                         multikey=True,
                                                         brackets=('()', ),
                                                         quotes='\'"')
+            logging.debug("parse_quoted_separated items: %r" % items)
         except wikiutil.BracketError:
             raise ValueError()
         query = self._analyse_items(items)
+        logging.debug("analyse_items query: %r" % query)
         return query
--- a/docs/CHANGES	Sat Jun 07 21:48:06 2008 +0200
+++ b/docs/CHANGES	Sun Jun 08 01:29:49 2008 +0200
@@ -32,6 +32,7 @@
   Bug Fixes:
     * Fix xmlrpc request.read() call to use content-length header, if available.
     * Fix traceback in filesys.py on Mac OS X when import Carbon fails.
+    * Xapian search: fix searching for negative terms only
 
   Other changes:
     * HINT: If you want to use xapian based indexed search, you need to have