Django Haystack


官方 Github 链接 官方文档

Haystack 为 Django 提供模块化搜索,它具有统一、标准的API,可以配合不同的后台,例如:Solr / Elasticsearch / Whoosh 等等,而无需修改代码。

Quick Start

1. 安装
pip install django-haystack
2. 添加haystack到 settings.py 的 INSTALLED_APPS 中。
3. 修改 settings.py,增加一个设置以指示使用何种后端
// Elasticsearch  2.x
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch2_backend.Elasticsearch2SearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

// WhooshEngine
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'DjangoBlog.whoosh_cn_backend.WhooshEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
    },
}
# Automatically update searching index
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
4. 为 models 创建 search_indexes.py 文件。
- Haystack 用 SearchIndex 对象来决定那些数据需要建立搜索索引,这很类似 Models & Forms
5. Setup the main SearchIndex via autodiscover.
6. 将 haystack.urls 添加到你的 URLconf 中.
7. 建立(刷新)索引