cs9*_*s95 4 python django elasticsearch elasticsearch-dsl-py
我正在尝试调用在 docker 上运行的本地 ES 实例。我使用以下说明来设置我的 ES 实例: https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html https://www.elastic.co/guide/en/ elasticsearch/reference/current/docker.html#docker-cli-run-dev-mode
我可以在 Kibana 上使用我的实例http://0.0.0.0:5601/app/dev_tools#/console。到这里一切正常。
现在我正在尝试使用 django 模型定义一些示例文档并通过库对它们进行索引;我按照此处的说明进行操作:https://django-elasticsearch-dsl.readthedocs.io/en/latest/quickstart.html#install-and-configure
首先,我添加 pip 安装并添加django_elasticsearch_dsl到INSTALLED_APPS
接下来,我在settings.py中添加:
ELASTICSEARCH_DSL = {
'default': {
'hosts': 'localhost:9200'
},
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个示例模型和文档,如下所示:
# models.py
from django.db import models
class Car(models.Model):
name = models.CharField(max_length=30)
color = models.CharField(max_length=30)
description = models.TextField()
type = models.IntegerField(choices=[
(1, "Sedan"),
(2, "Truck"),
(4, "SUV"),
])
Run Code Online (Sandbox Code Playgroud)
# 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',
]
Run Code Online (Sandbox Code Playgroud)
最后运行python3 manage.py search_index --rebuild 会出现如下连接错误:
raise ConnectionError("N/A", str(e), e)
elasticsearch.exceptions.ConnectionError: ConnectionError(('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))) caused by: ProtocolError(('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')))
Run Code Online (Sandbox Code Playgroud)
我怀疑我的设置可能存在问题,ELASTICSEARCH_DSL因为我没有为 https 指定任何配置,但文档没有明确说明这一点。
我该如何解决这个问题?
Django version:
Django==4.0.1
django-elasticsearch-dsl==7.2.2
Python version: Python 3.9.10
Run Code Online (Sandbox Code Playgroud)
谢谢!
我认为这是我的证书的问题。
我需要向变量添加一些额外的配置参数ELASTICSEARCH_DSL。添加此内容可以解决问题:
from elasticsearch import RequestsHttpConnection
# Elasticsearch configuration in settings.py
ELASTICSEARCH_DSL = {
'default': {
'hosts': 'localhost:9200',
'use_ssl': True,
'http_auth': ('user', 'password'),
'ca_certs': '/path/to/cert.crt'
'connection_class': RequestsHttpConnection
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅弹性文档中有关验证证书的这一部分。
如果您尚未设置证书来验证连接并且只需要快速启动并运行某些内容,则可以传递'verify_certs': False并设置'ca_certs'为None.