Django ElasticSearch集成。
django-el的Python项目详细描述
这是Django应用程序,帮助Django与ElasticSearch集成。 它建立在elasticsearch-dsl之上。
项目旨在支持Python3和Django1.8(至少)。
库正在开发中,请谨慎使用,因为在稳定api之前 是一个可以改变的话题。
快速启动
将模型配置为可索引:
from django.db import models from el.models import Indexed class Article(models.Model, Indexed): title = models.CharField(max_length=78) @classmethod def get_indexable(cls): return cls.objects.all() @classmethod def configure_mapping(cls, mapping): # mapping is an elasticsearch_dsl Mapping object mapping.field('title', 'string') return mapping
从现在起,Article模型将被自动发现和索引。
更新搜索索引:
./manage.py update_index
使用elasticsearch_dsl查询:
# articles is a list of an Article instances articles = Article.search().query('match', title="Bob's article").execute() # articles is a list of elasticsearch_dsl hits articles = Article.search().query('match', title="Bob's article").execute(cast=False)
与elasticsearch_dsl相反,django-el提供了修改的 Search返回django模型实例而不是原始实例的对象 默认情况下为ElasticSearch结果。您可以使用 cast参数。
安装
使用pip:
pip install django-el
配置
django el构建在elasticsearch_dsl库之上,并提供 通过settings.py:
的django路连接配置ELASTICSEARCH_CONNECTIONS = { 'default': { 'hosts': ['127.0.0.1:9200'], 'serializer': 'project.serializers.MySerializer', } }
可以使用^{tt11}定义项目连接$ 设置。它只是一个高于低级的高级接口 elasticsearch_dsl.connections.connections.create_connection函数。
键(在本例中是默认的)是连接别名,它的值是 是create_connection参数。