changeset 5015:bc42755b5820

Xapian2009: AndExpression.xapian_term() was refactored. Tests for "and" were updated.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Sat, 15 Aug 2009 20:43:43 +0200
parents c56728051226
children b4f41e24833c
files MoinMoin/search/Xapian.py MoinMoin/search/_tests/test_search.py MoinMoin/search/queryparser.py
diffstat 3 files changed, 17 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/Xapian.py	Sat Aug 15 20:36:56 2009 +0200
+++ b/MoinMoin/search/Xapian.py	Sat Aug 15 20:43:43 2009 +0200
@@ -217,7 +217,6 @@
                                 yield both at once as a tuple (False)
         """
         for word, pos in self.raw_tokenize(value):
-            word = word.lower() # transform it into what xapian wants
             if flat_stemming:
                 yield (word, pos)
                 if self.stemmer:
@@ -636,8 +635,8 @@
                 os.unlink(os.path.join(self.dir, f))
             mode = 'add'
 
+        connection = MoinIndexerConnection(self.dir)
         try:
-            connection = MoinIndexerConnection(self.dir)
             self.touch()
             logging.debug("indexing all (%d) pages..." % len(pages))
             for pagename in pages:
--- a/MoinMoin/search/_tests/test_search.py	Sat Aug 15 20:36:56 2009 +0200
+++ b/MoinMoin/search/_tests/test_search.py	Sat Aug 15 20:43:43 2009 +0200
@@ -115,6 +115,8 @@
     def test_title_search_simple(self):
         searches = {u'title:SearchTestPage': 1,
                     u'title:LanguageSetup': 1,
+                    u'title:HelpIndex': 1,
+                    u'title:Help': 2,
                     u'title:SearchTestNotExisting': 0}
 
         def test(query, res_count):
@@ -220,11 +222,14 @@
         result = self.search(u'domain:system')
         assert result.hits
 
-    def testTitleSearchAND(self):
+    def test_search_and(self):
         """ search: title search with AND expression """
-        result = self.search(u"title:Help title:Index")
+        result = self.search(u"title:HelpOnCreoleSyntax lang:en")
         assert len(result.hits) == 1
 
+        result = self.search(u"title:HelpOnCreoleSyntax lang:de")
+        assert len(result.hits) == 0
+
         result = self.search(u"title:Help title:%s" % self.doesnotexist)
         assert not result.hits
 
--- a/MoinMoin/search/queryparser.py	Sat Aug 15 20:36:56 2009 +0200
+++ b/MoinMoin/search/queryparser.py	Sat Aug 15 20:43:43 2009 +0200
@@ -254,6 +254,7 @@
         # sort negated terms
         terms = []
         not_terms = []
+
         for term in self._subterms:
             if not term.negated:
                 terms.append(term.xapian_term(request, connection))
@@ -261,28 +262,18 @@
                 not_terms.append(term.xapian_term(request, connection))
 
         # prepare query for not negated terms
-        if not terms:
-            t1 = None
-        elif len(terms) == 1:
-            t1 = Query(terms[0])
+        if terms:
+            query = Query(Query.OP_AND, terms)
         else:
-            t1 = Query(Query.OP_AND, terms)
+            query = Query('') # MatchAll
 
         # prepare query for negated terms
-        if not not_terms:
-            t2 = None
-        elif len(not_terms) == 1:
-            t2 = Query(not_terms[0])
+        if not_terms:
+            query_negated = Query(Query.OP_OR, not_terms)
         else:
-            t2 = Query(Query.OP_OR, not_terms)
+            query_negated = Query()
 
-        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)
+        return Query(Query.OP_AND_NOT, query, query_negated)
 
 
 class OrExpression(AndExpression):
@@ -474,6 +465,7 @@
                     # XXX UnicodeQuery was used here!
                     t = [connection.query_field('title', w) for w, pos in analyzer.tokenize(term)]
 
+                # XXX what should be there OR or AND?!
                 queries.append(Query(Query.OP_OR, t))
 
             if not self.case and stemmed: