Django Elasticsearch DSL是一个软件包,允许在elasticsearch中索引Django模型。它是作为Elasticsearch-dsl-py的薄包装而构建的, 因此您可以使用elasticsearch-dsl-py团队开发的所有功能。
功能
-
基于elasticsearch-dsl-py,因此您可以使用Search类进行查询。
-
Django信号接收器处于保存和删除状态,以保持Elasticsearch同步。
-
用于创建,删除,重建和填充索引的管理命令。
-
从Django模型字段中的Elasticsearch自动映射。
-
复杂字段类型支持(ObjectField,NestedField)。
-
使用并行索引快速建立索引。
要求
Django> = 1.11
Python 2.7、3.5、3.6、3.7
Elasticsearch兼容性: 该库与5.x以后的所有Elasticsearch版本兼容, 但是您必须使用匹配的主要版本:
- 对于Elasticsearch 7.0及更高版本,请使用库的主要版本7(7.xy)。
- 对于Elasticsearch 6.0及更高版本,请使用库的主要版本6(6.xy)。
- 对于Elasticsearch 5.0和更高版本,请使用库的主要版本0.5(0.5.x)。
# Elasticsearch 7.x
elasticsearch-dsl>=7.0.0,<8.0.0
# Elasticsearch 6.x
elasticsearch-dsl>=6.0.0,<7.0.0
# Elasticsearch 5.x
elasticsearch-dsl>=0.5.1,<6.0.0
安装Django Elasticsearch DSL:
pip install django-elasticsearch-dsl
然后添加django_elasticsearch_dsl
到INSTALLED_APPS
您必须ELASTICSEARCH_DSL
在django设置中定义。
例如:
ELASTICSEARCH_DSL={
'default': {
'hosts': 'localhost:9200'
},
}
ELASTICSEARCH_DSL
然后传递给elasticsearch-dsl-py.connections.configure
(请参阅此处)。
声明数据索引
然后对于模型:
# models.py
class Car(models.Model):
name = models.CharField()
color = models.CharField()
description = models.TextField()
type = models.IntegerField(choices=[
(1, "Sedan"),
(2, "Truck"),
(4, "SUV"),
])
为了使该模型与Elasticsearch一起使用,请创建的子类django_elasticsearch_dsl.Document
,在该类的内部创建一个以定义您的Elasticsearch索引,名称,设置等,最后使用decorator注册该类。它需要在您的应用程序目录中定义类 。class Index
Document
registry.register_document
Document
documents.py
# documents.py
from django_elasticsearch_dsl import Document
from django_elasticsearch_dsl.registries import registry
from .models import Car
@registry.register_document
class CarDocument(Document):
class Index:
# Name of the Elasticsearch index
name = 'cars'
# See Elasticsearch Indices API reference for available settings
settings = {'number_of_shards': 1,
'number_of_replicas': 0}
class Django:
model = Car # The model associated with this Document
# The fields of the model you want to be indexed in Elasticsearch
fields = [
'name',
'color',
'description',
'type',
]
# Ignore auto updating of Elasticsearch when a model is saved
# or deleted:
# ignore_signals = True
# Don't perform an index refresh after every update (overrides global setting):
# auto_refresh = False
# Paginate the django queryset used to populate the index with the specified size
# (by default it uses the database driver's default setting)
# queryset_pagination = 5000
填充
要创建并填充Elasticsearch索引和映射,请使用search_index命令:
$ ./manage.py search_index --rebuild
现在,当您执行以下操作时:
car = Car(
name="Car one",
color="red",
type=1,
description="A beautiful car"
)
car.save()
该对象也将保存在Elasticsearch中(使用信号处理程序)。
搜索
要获取elasticsearch-dsl-py搜索实例,请使用:
s = CarDocument.search().filter("term", color="red")
# or
s = CarDocument.search().query("match", description="beautiful")
for hit in s:
print(
"Car name : {}, description {}".format(hit.name, hit.description)
)
前面的例子返回一个结果具体到elasticsearch_dsl,但它也可以将elastisearch结果转换成真正的Django的queryset的,只是知道这个成本SQL请求,检索与由elastisearch查询返回的ID的模型实例。
s = CarDocument.search().filter("term", color="blue")[:30]
qs = s.to_queryset()
# qs is just a django queryset and it is called with order_by to keep
# the same order as the elasticsearch result.
for car in qs:
print(car.name)