added whoosh search adapter

This commit is contained in:
Jacob Mason 2010-06-26 13:40:39 -05:00
parent d9b07f4c7d
commit f83ff52ede
4 changed files with 69 additions and 10 deletions

View File

@ -35,9 +35,8 @@ class WebSupportBuilder(StandaloneHTMLBuilder):
return docname
def load_indexer(self, docnames):
keep = set(self.env.all_docs) - set(docnames)
self.indexer = self.app.search
self.indexer.init_indexing()
self.indexer.init_indexing(changed=docnames)
def handle_page(self, pagename, addctx, templatename='page.html',
outfilename=None, event_arg=None):

View File

@ -12,7 +12,7 @@
import re
class BaseSearch(object):
def init_indexing(self):
def init_indexing(self, changed=[]):
pass
def finish_indexing(self):
@ -37,10 +37,14 @@ class BaseSearch(object):
return ''
start = max(res.start() - 120, 0)
end = start + 240
context = ['...' if start > 0 else '',
text[start:end],
'...' if end < len(text) else '']
return unicode(''.join(context), errors='ignore')
context = ''.join(['...' if start > 0 else '',
text[start:end],
'...' if end < len(text) else ''])
try:
return unicode(context, errors='ignore')
except TypeError:
return context
search_adapters = {
'xapian': ('xapiansearch', 'XapianSearch'),

View File

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""
sphinx.websupport.search.whooshsearch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Whoosh search adapter.
:copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from whoosh import index
from whoosh.fields import Schema, ID, TEXT, STORED
from whoosh.analysis import StemmingAnalyzer
from whoosh import highlight
from sphinx.util.osutil import ensuredir
from sphinx.websupport.search import BaseSearch
class WhooshSearch(BaseSearch):
schema = Schema(path=ID(stored=True, unique=True),
title=TEXT(field_boost=2.0, stored=True),
text=TEXT(analyzer=StemmingAnalyzer(), stored=True))
def __init__(self, db_path):
ensuredir(db_path)
if index.exists_in(db_path):
self.index = index.open_dir(db_path)
else:
self.index = index.create_in(db_path, schema=self.schema)
self.searcher = self.index.searcher()
def init_indexing(self, changed=[]):
for changed_path in changed:
self.index.delete_by_term('path', changed_path)
self.writer = self.index.writer()
def finish_indexing(self):
self.writer.commit()
def add_document(self, path, title, text):
self.writer.add_document(path=unicode(path),
title=title,
text=text)
def handle_query(self, q):
res = self.searcher.find('text', q)
results = []
for result in res:
context = self.extract_context(result['text'], q)
results.append((result['path'],
result.get('title', ''),
context))
return results, len(res), res.scored_length()

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
sphinx.websupport.search.xapian
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sphinx.websupport.search.xapiansearch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Xapian search adapter.
@ -26,7 +26,7 @@ class XapianSearch(BaseSearch):
def __init__(self, db_path):
self.db_path = db_path
def init_indexing(self):
def init_indexing(self, changed=[]):
ensuredir(self.db_path)
self.database = xapian.WritableDatabase(self.db_path,
xapian.DB_CREATE_OR_OPEN)