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)