django全文搜索学习心得(一)haystack 篇

    最近开始学习django开发,而网站避免不了使用全文搜索,于是乎,就研究了一下。

    首先,说一下个人对网站全文搜索的简单认识,就是将部分数据库内容以特定索引方式存在一个文件中,然后利用各种高效方法对其进行查找,匹配。django中我查看一些app,可用的很多。这里先记录一下简单应用,后期再补充各种高级应用。

    这里先介绍一个比较强势的,django-haystack ,官方说完成了对 SolrElasticsearchWhooshXapian, 等等的使用封装,让我们在使用过程中只需更改settings.py 中的引擎即可方便切换方法,不用更改其他代码。

安装

    到https://github.com/toastdriven/django-haystack  下载zip,2.0版本的。2.0版本的settings.py 设置方法比较好。

 

INSTALLED_APPS = [
    ...
    'haystack',
    ...
]

import os
HAYSTACK_CONNECTIONS = {
    'default': {
        # For Solr:
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://localhost:9001/solr/example',
        'TIMEOUT': 60 * 5,
        'INCLUDE_SPELLING': True,
    },
    'whoosh': {
        # For Whoosh:
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
        'INCLUDE_SPELLING': True,
    },
    'simple': {
        # For Simple:
        'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
    },
    'xapian': {
        # For Xapian (requires the third-party install):
        'ENGINE': 'xapian_haystack.xapian_backend.XapianEngine',
        'PATH': os.path.join(os.path.dirname(__file__), 'xapian_index'),
    }
}

 urls.py 的配置

urlpatterns = patterns('',
                       (r'^search/', include('haystack.urls')),
)

  

posted @ 2013-01-10 17:42  深秋的黎明  阅读(3022)  评论(0编辑  收藏  举报