changeset 4979:d2cda24a5475

Xapian2009: MoinSearchConnection class was introduced which inherits from xappy.SearchConnection and provides get_all_documents method. Various cahnges to the queryparser.py now tests fail on asserts, but not in the errors in the code.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Thu, 06 Aug 2009 17:26:28 +0200
parents 0b5aaec043cd
children d43af366f10a
files MoinMoin/search/Xapian.py MoinMoin/search/queryparser.py
diffstat 2 files changed, 31 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/Xapian.py	Thu Aug 06 16:19:52 2009 +0200
+++ b/MoinMoin/search/Xapian.py	Thu Aug 06 17:26:28 2009 +0200
@@ -41,6 +41,19 @@
 
         Query.__init__(self, *nargs, **kwargs)
 
+
+class MoinSearchConnection(xappy.SearchConnection):
+
+    def get_all_documents(self):
+        """
+        Return all the documents in the xapian index.
+        """
+        document_count = self.get_doccount()
+        query = self.query_all()
+        hits = self.search(query, 0, document_count)
+        return hits
+
+
 class MoinIndexerConnection(xappy.IndexerConnection):
 
     def __init__(self, *args, **kwargs):
@@ -245,17 +258,6 @@
         """ Check if the Xapian index exists """
         return BaseIndex.exists(self) and os.listdir(self.dir)
 
-    def get_all_documents(self):
-        """
-        Return all the documents in the xapian index.
-        """
-        connection = xappy.SearchConnection(self.dir)
-        document_count = connection.get_doccount()
-        query = connection.query_all()
-        hits = connection.search(query, 0, document_count)
-        connection.close()
-        return hits
-
     def _search(self, query, sort='weight', historysearch=0):
         """
         Perform the search using xapian (read-lock acquired)
@@ -272,7 +274,7 @@
                 else:
                     break
             except IndexError:
-                searcher = xappy.SearchConnection(self.dir)
+                searcher = MoinSearchConnection(self.dir)
                 timestamp = self.mtime()
                 break
 
--- a/MoinMoin/search/queryparser.py	Thu Aug 06 16:19:52 2009 +0200
+++ b/MoinMoin/search/queryparser.py	Thu Aug 06 17:26:28 2009 +0200
@@ -103,9 +103,6 @@
             self.search_re = re.compile(pattern, flags)
             self.pattern = pattern
 
-    def xapian_wanted(self):
-        return False
-
     def _get_query_for_search_re(self, connection, field_to_check=None):
         """
         Return a query which satisfy self.search_re for field values.
@@ -118,16 +115,22 @@
             data = document.data
             if field_to_check:
                 # Check only field with given name
-                if self.search_re.match(data[field_to_check]):
-                    queries.append(connection.query_field(field_to_check, data[field_to_check]))
+                if field_to_check in data:
+                    term = data[field_to_check][0]
+                    if self.search_re.match(term):
+                        queries.append(connection.query_field(field_to_check, term))
             else:
                 # Check all fields
                 for field, value in data.iteritems():
-                    if self.search_re.match(value):
-                        queries.append(connection.query_field(field, value))
+                    term = value[0]
+                    if self.search_re.match(term):
+                        queries.append(connection.query_field(field, term))
 
         return Query(Query.OP_OR, queries)
 
+    def xapian_wanted(self):
+        return False
+
     def __unicode__(self):
         neg = self.negated and '-' or ''
         return u'%s%s"%s"' % (neg, self._tag, unicode(self._pattern))
@@ -312,9 +315,9 @@
                 matches.extend(result)
         return matches
 
-    def xapian_term(self, request, allterms):
+    def xapian_term(self, request, connection):
         # XXX: negated terms managed by _moinSearch?
-        return Query(Query.OP_OR, [term.xapian_term(request, allterms) for term in self._subterms])
+        return Query(Query.OP_OR, [term.xapian_term(request, connection) for term in self._subterms])
 
 
 class TextSearch(BaseExpression):
@@ -422,7 +425,7 @@
         # titlesearch OR parsed wikiwords
         return Query(Query.OP_OR,
                      # XXX allterms for titlesearch
-                     [self.titlesearch.xapian_term(request, []),
+                     [self.titlesearch.xapian_term(request, connection),
                       Query(Query.OP_AND, queries)])
 
 
@@ -490,7 +493,7 @@
     def xapian_need_postproc(self):
         return self.case
 
-    def xapian_term(self, request, allterms):
+    def xapian_term(self, request, connection):
         if self.use_re:
             # XXX weight for a query!
             queries = [self._get_query_for_search_re(connection, 'fulltitle')]
@@ -536,11 +539,11 @@
 
     _field_to_search = None
 
-    def xapian_term(self, request, allterms):
+    def xapian_term(self, request, connection):
         if self.use_re:
             return self._get_query_for_search_re(connection, self._field_to_search)
         else:
-            return connection.query_field(_field_to_search, self.pattern)
+            return connection.query_field(self._field_to_search, self.pattern)
 
 
 class LinkSearch(BaseFieldSearch):
@@ -732,7 +735,7 @@
     def xapian_need_postproc(self):
         return self.case
 
-    def xapian_term(self, request, allterms):
+    def xapian_term(self, request, connection):
         # XXX Probably, it is a good idea to inherit this class from
         # BaseFieldSearch and get rid of this definition
         if self.use_re: