Skip to content

Instantly share code, notes, and snippets.

@benzkji
Forked from gregplaysguitar/folding_whoosh_backend.py
Created February 25, 2016 09:14
Show Gist options
  • Save benzkji/9503e07e412b7e40a8f1 to your computer and use it in GitHub Desktop.
Save benzkji/9503e07e412b7e40a8f1 to your computer and use it in GitHub Desktop.
Django-haystack Whoosh backend with character folding
# -*- coding: utf-8 -*-
"""
Whoosh backend for haystack that implements character folding, as per
http://packages.python.org/Whoosh/stemming.html#character-folding .
Tested with Haystack 2.4.0 and Whooch 2.7.0
To use, put this file on your path and add it to your haystack settings, eg.
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'search_backends.FoldingWhooshEngine',
'PATH': 'path-to-whoosh-index',
},
}
"""
from haystack.backends.whoosh_backend import WhooshEngine, WhooshSearchBackend
from whoosh.analysis import CharsetFilter, StemmingAnalyzer
from whoosh.support.charset import accent_map
from whoosh.fields import TEXT
class FoldingWhooshSearchBackend(WhooshSearchBackend):
def build_schema(self, fields):
schema = super(FoldingWhooshSearchBackend, self).build_schema(fields)
for name, field in schema[1].items():
if isinstance(field, TEXT):
field.analyzer = StemmingAnalyzer() | CharsetFilter(accent_map)
return schema
class FoldingWhooshEngine(WhooshEngine):
backend = FoldingWhooshSearchBackend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment