Back to index

moin  1.9.0~rc2
search.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - search engine internals
00004 
00005     @copyright: 2005 MoinMoin:FlorianFesti,
00006                 2005 MoinMoin:NirSoffer,
00007                 2005 MoinMoin:AlexanderSchremmer,
00008                 2006-2009 MoinMoin:ThomasWaldmann,
00009                 2006 MoinMoin:FranzPletz
00010     @license: GNU GPL, see COPYING for details
00011 """
00012 
00013 
00014 from MoinMoin import log
00015 logging = log.getLogger(__name__)
00016 
00017 from MoinMoin.search.builtin import BaseSearch, MoinSearch, BaseIndex
00018 from MoinMoin.search.Xapian.indexing import XapianIndex
00019 
00020 class IndexDoesNotExistError(Exception):
00021     pass
00022 
00023 class XapianSearch(BaseSearch):
00024 
00025     def __init__(self, request, query, sort='weight', mtime=None, historysearch=0):
00026         super(XapianSearch, self).__init__(request, query, sort, mtime, historysearch)
00027 
00028         self.index = self._xapian_index()
00029 
00030     def _xapian_index(self):
00031         """ Get the xapian index if possible
00032 
00033         @param request: current request
00034         """
00035         index = XapianIndex(self.request)
00036 
00037         if not index.exists():
00038             raise IndexDoesNotExistError
00039 
00040         return index
00041 
00042     def _search(self):
00043         """ Search using Xapian
00044 
00045         Get a list of pages using fast xapian search and
00046         return moin search in those pages if needed.
00047         """
00048         clock = self.request.clock
00049         index = self.index
00050 
00051         clock.start('_xapianSearch')
00052         clock.start('_xapianQuery')
00053         search_results = index.search(self.query, sort=self.sort, historysearch=self.historysearch)
00054         clock.stop('_xapianQuery')
00055         logging.debug("_xapianSearch: finds: %r" % search_results)
00056 
00057         # Note: .data is (un)pickled inside xappy, so we get back exactly what
00058         #       we had put into it at indexing time (including unicode objects).
00059         pages = [{'uid': r.id,
00060                   'wikiname': r.data['wikiname'][0],
00061                   'pagename': r.data['pagename'][0],
00062                   'attachment': r.data['attachment'][0],
00063                   'revision': r.data.get('revision', [0])[0]}
00064                  for r in search_results]
00065         try:
00066             if not self.query.xapian_need_postproc():
00067                 # xapian handled the full query
00068                 clock.start('_xapianProcess')
00069                 try:
00070                     _ = self.request.getText
00071                     return self._getHits(pages), (search_results.estimate_is_exact and '' or _('about'), search_results.matches_estimated)
00072                 finally:
00073                     clock.stop('_xapianProcess')
00074         finally:
00075             clock.stop('_xapianSearch')
00076 
00077         # some postprocessing by MoinSearch is required
00078         return MoinSearch(self.request, self.query, self.sort, self.mtime, self.historysearch, pages=pages)._search()
00079 
00080