Django Elasticsearch DSL

41 篇文章 2 订阅
34 篇文章 3 订阅

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 IndexDocumentregistry.register_documentDocumentdocuments.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中(使用信号处理程序)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ch3nnn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值