Django Elasticsearch DSL

网友投稿 924 2022-11-16

Django Elasticsearch DSL

Django Elasticsearch DSL

Django Elasticsearch DSL是一个软件包,允许在elasticsearch中索引Django模型。它是作为​​Elasticsearch-dsl-py​​的薄包装而构建的, 因此您可以使用elasticsearch-dsl-py团队开发的所有功能。

功能

基于​​elasticsearch-dsl-py,​​因此您可以使用​​Search​​类进行查询。Django信号接收器处于保存和删除状态,以保持Elasticsearch同步。用于创建,删除,重建和填充索引的管理命令。从Django模型字段中的Elasticsearch自动映射。复杂字段类型支持(ObjectField,NestedField)。使用并行索引快速建立索引。

要求

Django> = 1.11Python 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.xelasticsearch-dsl>=7.0.0,<8.0.0# Elasticsearch 6.xelasticsearch-dsl>=6.0.0,<7.0.0# Elasticsearch 5.xelasticsearch-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.pyclass 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.pyfrom django_elasticsearch_dsl import Documentfrom django_elasticsearch_dsl.registries import registryfrom .models import Car@registry.register_documentclass 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")# ors = 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)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Scrapy 架构流程图(官方文档) - 翻译整理
下一篇:HTML Marquee 标签实现文字滚动
相关文章

 发表评论

暂时没有评论,来抢沙发吧~