1

I'm trying to use elasticsearch for my django project and following this tutorial with my own model:

https://django-elasticsearch-dsl.readthedocs.io/en/latest/quickstart.html#populate

My model:

class Product(models.Model):
    STATUS = (
        ('True', 'True'),
        ('False', 'False'),
    )
    title = models.CharField(max_length=150)
    keywords = models.CharField(max_length=255)
    description = models.TextField(max_length=255)
    price = models.DecimalField(max_digits=12, decimal_places=2,default=0)
    image=models.ImageField(blank=True)
    amount=models.IntegerField(default=0)
    minamount=models.IntegerField(default=3)
    slug = models.SlugField(null=False, unique=True)
    status=models.CharField(max_length=10,choices=STATUS)
    create_at=models.DateTimeField(auto_now_add=True)
    update_at=models.DateTimeField(auto_now=True)
    rating = models.IntegerField(default=0)
    def __str__(self):
        return self.title

my documents.py:

# documents.py

from django_elasticsearch_dsl import Document
from django_elasticsearch_dsl.registries import registry
from .models import Product


@registry.register_document
class CarDocument(Document):
    class Index:
        # Name of the Elasticsearch index
        name = 'products'
        # See Elasticsearch Indices API reference for available settings
        settings = {'number_of_shards': 1,
                    'number_of_replicas': 0}

    class Django:
        model = Product # The model associated with this Document

        # The fields of the model you want to be indexed in Elasticsearch
        fields = [
            'title',
            'keywords',
            'status',
            'description',
        ]

        # 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

and setting part :

ELASTICSEARCH_DSL={
    'default': {
        'hosts': 'localhost:9200'
    },
}

but when I populate with manage.py search_index --rebuild I got this error :

elasticsearch.exceptions.ConnectionError: ConnectionError(<urllib3.connection.HTTPConnection object at 0x00000224FDF07370>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x00000224FDF07370>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it)
4
  • Possible Duplicate: stackoverflow.com/questions/56833936/…: stackoverflow.com/questions/65704216/… Commented Jun 3, 2021 at 11:59
  • @JohnDoe I'm using with django Commented Jun 3, 2021 at 12:05
  • @JohnDoe It is because I have to run elasticsearch with port 9200 in parallel. how can I deploy django with that ? Commented Jun 3, 2021 at 15:12
  • 1
    Did you start Elasticsearch? Generally, you deploy another machine running ES, and Django points to that. Should be no problem to test by bringing up Elasticsearch locally on the same machine, but I wouldn't do that in production. I just followed the same tutorial, and it worked with no problems. Commented Aug 3, 2021 at 17:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.