Django的全文检索功能(一):haystack全文检索的框架

全文检索不同于特定字段的模糊查询,使用全文检索的效率更高,并且能够对于中文进行分词处理

haystack:django的一个包,属于一种全文检索的框架,可以方便地对model里面的内容进行索引、搜索,完成了对 Solr, Elasticsearch, Whoosh, Xapian, 等等的使用封装,让我们在使用过程中只需更改settings.py中的引擎即可方便切换方法,不用更改其他代码。

安装

[python] view plain copy
pip install django-haystack  

配置settings.py文件:

添加应用:

[python] view plain copy
INSTALLED_APPS = (  
    ...  
    'haystack',  
)  
添加搜索引擎:

[python] view plain copy
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中添加url:

[python] view plain copy
urlpatterns = [  
    ...  
    url(r'^search/', include('haystack.urls')),  
]  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值