2

I try to itergrate elasticsearch in django project. But when I execute command python manage.py search_index --rebuild
it made this error
elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x7fde186b2b50>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7fde186b2b50>: Failed to establish a new connection: [Errno 111] Connection refused)

After run docker-compose, I can access to elasticsearch http://localhost:9200 via chrome.

This is setting.py file:

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_elasticsearch_dsl',
]
ELASTICSEARCH_DSL={
    'default': {
        'hosts': 'localhost:9200'
    },
}

And document.py file:

from django_elasticsearch_dsl import Document
from django_elasticsearch_dsl.registries import registry

from .models import Question

@registry.register_document
class QuestionDocument(Document):
    class Index:
        #name of elasticsearch index
        name = 'questions'
        setting = { 'number_of_shard' : 1,
                    'number_of_replicas' : 0}

    class Django:
        model = Question
        fields = [
            'question_text',
            'pub_date',
        ]

How can I resolve it?

n = int(input("N = "))
a = int(input("A = "))
b = int(input("B = "))
sum = 0
c = a*b
if n > a:
    min_a=0
    max_a=0
    total=0
    for i in range (1, n+1):
        if i%a == 0:
            min_a = i
            break
    for i in range (n, 1, -1):
        if i%a == 0:
            max_a = i
            break
    total = (max_a - min_a)/a + 1
    sum += (max_a+min_a)*total/2
if n > b:
    min_b=0
    max_b=0
    total=0
    for i in range (1, n+1):
        if i%b == 0:
            min_b = i
            break
    for i in range (n, 1, -1):
        if i%b == 0:
            max_b = i
            break
    total = (max_b - min_b)/b + 1
    sum += (max_b+min_b)*total/2
if n > c:
    min_c=0
    max_c=0
    total=0
    for i in range (1, n+1):
        if i%c == 0:
            min_c = i
            break
    for i in range (n, 1, -1):
        if i%c == 0:
            max_c = i
            break
    total = (max_c - min_c)/c + 1
    sum -= (max_c+min_c)*total/2
print("Ket qua = {}".format(int(sum)))
1
  • 1
    not sure if I am correct, but going by this : sunscrapers.com/blog/how-to-use-elasticsearch-with-django you may need to put ELASTICSEARCH_DSL={'default':{'hosts':'elasticsearch:9200'},} instead of localhost, where elasticsearch is name of the docker container there. But please check this though - commenting basis my 3 sec glance at it.
    – khanna
    Commented Sep 1, 2020 at 11:00

1 Answer 1

5

I had the same error in my project. In settings.py you need change hosts from 'localhost:9200', to name of your service in docker-compose. Example:

    ELASTICSEARCH_DSL={
    'default': {
        'hosts': 'elasticsearch'
    },
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.