changeset 5045:d77ac3c87ac8

Xapian2009: refactoring of searchPages. Tests for it.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Thu, 27 Aug 2009 18:49:13 +0300
parents f5f3d7348564
children a9e597a6e012
files MoinMoin/search/Xapian/search.py MoinMoin/search/__init__.py MoinMoin/search/_tests/test_search.py
diffstat 3 files changed, 51 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/Xapian/search.py	Thu Aug 27 18:46:31 2009 +0300
+++ b/MoinMoin/search/Xapian/search.py	Thu Aug 27 18:49:13 2009 +0300
@@ -17,6 +17,9 @@
 from MoinMoin.search.builtin import BaseSearch, MoinSearch, BaseIndex
 from MoinMoin.search.Xapian.indexing import XapianIndex
 
+class IndexDoesNotExistError(Exception):
+    pass
+
 class XapianSearch(BaseSearch):
 
     def __init__(self, request, query, sort='weight', mtime=None, historysearch=0):
@@ -31,8 +34,10 @@
         """
         index = XapianIndex(self.request)
 
-        if index.exists():
-            return index
+        if not index.exists():
+            raise IndexDoesNotExistError
+
+        return index
 
     def _search(self):
         """ Search using Xapian
@@ -41,11 +46,8 @@
         return moin search in those pages if needed.
         """
         clock = self.request.clock
-        pages = None
         index = self.index
 
-        assert index, 'XXX Assume that index exist, actually we should have thrown an exception, so MoinSearch could be used instead'
-
         clock.start('_xapianSearch')
         try:
             clock.start('_xapianQuery')
--- a/MoinMoin/search/__init__.py	Thu Aug 27 18:46:31 2009 +0300
+++ b/MoinMoin/search/__init__.py	Thu Aug 27 18:49:13 2009 +0300
@@ -18,7 +18,8 @@
 
 
 def searchPages(request, query, sort='weight', mtime=None, historysearch=None, **kw):
-    """ Search the text of all pages for query.
+    """
+    Search the text of all pages for query.
 
     @param request: current request
     @param query: the expression (string or query objects) we want to search for
@@ -31,17 +32,34 @@
     @rtype: SearchResults instance
     @return: search results
     """
-    if isinstance(query, str) or isinstance(query, unicode):
-        query = QueryParser(**kw).parse_query(query)
 
-    searcher = MoinSearch
+    return _get_searcher(request, query, sort, mtime, historysearch, **kw).run()
+
+
+def _get_searcher(request, query, sort='weight', mtime=None, historysearch=None, **kw):
+    """
+    Return a searcher object according to the configuration.
+    """
+    query = _parse_query(query, **kw)
+    searcher = None
 
     if request.cfg.xapian_search:
         try:
-            from MoinMoin.search.Xapian.search import XapianSearch
-            searcher = XapianSearch
+            from MoinMoin.search.Xapian.search import XapianSearch, IndexDoesNotExistError
+            searcher = XapianSearch(request, query, sort, mtime=mtime, historysearch=historysearch)
         except ImportError, error:
             logging.warning("%s. Either disable Xapian completetly in your wikiconfig or upgrade your Xapian installation" % str(error))
+        except IndexDoesNotExistError:
+            logging.warning("Xapian index does not exist. Please crate it. Slow moin search is used.")
 
-    return searcher(request, query, sort, mtime=mtime, historysearch=historysearch).run()
+    if searcher is None:
+        searcher = MoinSearch(request, query, sort, mtime=mtime, historysearch=historysearch)
 
+    return searcher
+
+def _parse_query(query, **kw):
+    if isinstance(query, str) or isinstance(query, unicode):
+        query = QueryParser(**kw).parse_query(query)
+
+    return query
+
--- a/MoinMoin/search/_tests/test_search.py	Thu Aug 27 18:46:31 2009 +0300
+++ b/MoinMoin/search/_tests/test_search.py	Thu Aug 27 18:49:13 2009 +0300
@@ -10,7 +10,7 @@
 
 import py, os, time
 
-from MoinMoin.search import QueryError
+from MoinMoin.search import QueryError, _get_searcher
 from MoinMoin.search.queryparser import QueryParser
 from MoinMoin.search.builtin import MoinSearch
 from MoinMoin._tests import nuke_xapian_index, wikiconfig, become_trusted, create_page, nuke_page
@@ -95,6 +95,8 @@
              u'HelpOnEditing': None,
              u'HelpIndex': None}
 
+    searcher_class = None
+
     def setup_class(self):
         become_trusted(self.request)
 
@@ -282,9 +284,14 @@
         del self.pages['TestCreatePage']
         assert len(result.hits) == 1
 
+    def test_get_searcher(self):
+        assert isinstance(_get_searcher(self.request, ''), self.searcher_class)
+
 
 class TestMoinSearch(BaseSearchTest):
 
+    searcher_class = MoinSearch
+
     def get_searcher(self, query):
         pages = [{'pagename': page, 'attachment': '', 'wikiname': 'Self', } for page in self.pages]
         return MoinSearch(self.request, query, pages=pages)
@@ -316,8 +323,10 @@
 
         try:
             from MoinMoin.search.Xapian import XapianIndex
+            from MoinMoin.search.Xapian.search import XapianSearch
+            self.searcher_class = XapianSearch
+
         except ImportError, error:
-            print str(error)
             if not str(error).startswith('Xapian '):
                 raise
 
@@ -404,6 +413,14 @@
 
         nuke_xapian_index(self.request)
 
+class TestGetSearcher(object):
+
+    class Config(wikiconfig.Config):
+
+        xapian_search = True
+
+    def test_get_searcher(self):
+        assert isinstance(_get_searcher(self.request, ''), MoinSearch), 'Xapian index is not created, despite the configuration, MoinSearch mist be used!'
 
 coverage_modules = ['MoinMoin.search']