NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: 建立新连接失败: [Errno 111] 连接被拒绝)

NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused)

我正在尝试构建一个可以搜索汽车的简单 django elastice 搜索。但是当我试图重建索引时,它给了我上面的错误。我正在关注此文档 -> quickstart elasticsearch...下面给出了完整的回溯 ->

elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused)

我的 models.py 是简单的汽车模型,具有我在文档中添加的名称、颜色、描述、类型字段

class Car(models.Model):
    name = models.CharField(max_length=50)
    color = models.CharField(max_length=50)
    description = models.TextField()
    type = models.IntegerField(choices=[
        (1, "Sedan"),
        (2, "Truck"),
        (4, "SUV"),
    ])

我的 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 = 'cars'
        settings = {'number_of_shards': 1,
                    'number_of_replicas': 0}

    class Django:
        model = Car

        fields = [
            'name',
            'color',
            'description',
            'type',
        ]

但是当我尝试重建索引时,它给出了新的连接错误。下面给出了我用来重建索引的命令 ->

python manage.py search_index --rebuild

首先确保您已正确安装和配置 ElasticSearch 服务(您可以使用此 document)。此错误表明您的代码无法使用您当前的 ElasticSearch 服务配置。检查您的弹性服务是否已启动 运行。在基于 Linux 的 OS 中,您可以通过 $ sudo systemctl status elasticsearch.service 检查它,在结果页面中您应该看到行 Active: active (running)。完成此步骤后检查 url http://localhost:9200/(如果您没有更改默认设置),如果一切正常,您应该会看到以下结果:

{
  "name" : "some_name-System-Product-Name",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "erP_jdhLRvuwqLkx1_tehw",
  "version" : {
    "number" : "7.9.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
    "build_date" : "2020-08-11T21:36:48.204330Z",
    "build_snapshot" : false,
    "lucene_version" : "8.6.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

您也可以使用 $ sudo systemctl start elasticsearch.service 启动此服务。另一种可能性是您可能以错误的方式配置了弹性变量。例如,使用错误的端口而不是默认端口(即 9200)也会引发相同的错误。在下面的示例中,您可以看到一个 Elastic 配置示例,它将进入您项目的 settings.py:

# ElasticSearch configs
ELK_BASE_URL = 'elasticsearch://{username}:{password}@{host_ip}:{host_port}'
ELASTIC_SEARCH_URL = ELK_BASE_URL.format(
    username='ELASTICSEARCH_USER',
    password='ELASTICSEARCH_PASS',
    host_ip='ELASTICSEARCH_HOST',
    host_port='ELASTICSEARCH_PORT'
)
ELASTICSEARCH_DSL = {
    'default': {
        'hosts': [ELASTIC_SEARCH_URL]
    },
}