使用django-haystack搜索时忽略连字符

时间:2014-07-31 09:50:17

标签: django django-haystack whoosh

我尝试使用haystack后端whoosh进行搜索。

在我的文档中,我有字符串1234-567

我的搜索可能看起来像这样

SearchQuerySet().filter(content="1234567

由于1234567之间的连字符,Haystack找不到该对象。

任何人都知道我应该如何解决这个问题? 我应该在生成文档时删除连字符吗? 还有其他字符,如"。"和" /"可能会导致同样的问题。

1 个答案:

答案 0 :(得分:0)

我说要在生成文档时这样做。这可以通过定义准备方法来完成:http://django-haystack.readthedocs.org/en/latest/searchindex_api.html#advanced-data-preparation

也许对于你的情况,例如:

content = indexes.CharField()

def prepare_content(self, obj):
    return ''.join(obj.content.split('-'))

如果它不仅仅是一个字符,您可能希望使用正则表达式来清理字符串。