用于ElasticSearch的扩展Haystack后端
django-haystack-es的Python项目详细描述
ElasticSearch 5的Django Haystack后端。
快速启动
安装django haystack es:
pip install django-haystack-es
在settings.py中将haystack_es.backends.ElasticsearchSearchEngine添加到HAYSTACK_CONNECTIONS引擎中
示例
HAYSTACK_CONNECTIONS={'default':{'ENGINE':'haystack_es.backends.Elasticsearch5SearchEngine',# ...}}
使用haystack_es.indexes而不是haystack.indexes定义索引。
示例
# myapp/search_indexes.pyfromhaystack_esimportindexesfrommyapp.modelsimportMyModelclassMyModelIndex(indexes.SearchIndex,indexes.Indexable):text=indexes.CharField(document=True,use_template=True)# ...
如果安装了celery-haystack,则可以使用 haystack_es.indexes.CelerySearchIndex用于使用芹菜干草堆定义搜索索引
如果要使用其他searchqueryset方法,请使用haystack_es.query.SearchQuerySet 而不是haystack.query.SearchQuerySet。
示例
fromhaystack_es.queryimportSearchQuerySetsqs=SearchQuerySet().filter(content='some query')sqs.boost_fields({'field_name':2,'some_field':1.5,'another_field':1})sqs.facet('some_field')# ...
与默认Django Haystack ElasticSearch后端相比的差异
- 适用于ElasticSearch>;=5
- 允许查询时间字段提升。
- 允许查询时间 negative boost
- 提供其他搜索字段;DictField、NestedField和GeometryField
- 尝试使用ElasticSearch filter context 而不是用于筛选结果的查询字符串。
- 使用multi-fields 用于创建用于执行以下操作的阴影字段 切面和精确匹配需要非分析值。
查询时间字段boosting
from haystack_es.query import SearchQuerySet SearchQuerySet().boost_fields(boost_fields)
示例SearchQuerySet().boost_fields({'field_name': 2, 'another_field': 1})
负增压
from haystack_es.query import SearchQuerySet SearchQuerySet().boost_negative(query, negative_boost)
示例 SearchQuerySet().boost_negative({'match': {'category.raw': 'awful type'}}, negative_boost)
运行测试
代码真的有用吗?
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install tox (myenv) $ tox
学分
灵感来源于
用于呈现此包的工具:
历史记录
0.1.0(2017-07-27)
- pypi上的第一个版本。
0.2.8(2019-07-18)
- 在tox中简化django版本。