I have a Django
project where we’re going to use Elasticsearch
for a full-text search. I have a task to connect it with the existing Django
project. The first thing I found django-elasticsearch-dsl
package. I did everything like in tutorial and all worked fine, but the idea was in using elasticsearch-dsl
. I don’t understand how to create indexes right now. If in django-elasticsearch-dsl
the only thing I need is to run python3 manage.py search_index --rebuild
inside Django
container, but here I have no idea. I store all code in documents.py.
documents.py
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Document, Text
connections.create_connection(hosts=['http://elasticsearch:9200'])
class FilmWorkDocument(Document):
title = Text()
description = Text()
class Index:
name = 'film'
FilmWorkDocument.init()
first = FilmWorkDocument(title='Example1', description='Example description')
first.meta.id = 47
first.save()
docker-compose.yml
elasticsearch:
image: elasticsearch:8.13.0
container_name: elasticsearch
environment:
- "ES_JAVA_OPTS=-Xms200m -Xmx200m"
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- 9200:9200
Sending a request http://localhost:9200
shows that everything is ok.
{
"name" : "eeb958274241",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "wUQjIKoLTNGKtFH7A1tzSw",
"version" : {
"number" : "8.13.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "09df99393193b2c53d92899662a8b8b3c55b45cd",
"build_date" : "2024-03-22T03:35:46.757803203Z",
"build_snapshot" : false,
"lucene_version" : "9.10.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
But after sending a request http://localhost:9200/film/
it shows that index doesn’t exist.
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [film]","resource.type":"index_or_alias","resource.id":"film","index_uuid":"_na_","index":"film"}],"type":"index_not_found_exception","reason":"no such index [film]","resource.type":"index_or_alias","resource.id":"film","index_uuid":"_na_","index":"film"},"status":404}
So, the question are:
- How to create and populate index (especially if there are a lot of data in the database and I’m going to index all with
bulks
)? - Is it a good approach to use
elasticsearch-dsl
instead ofdjango-elasticsearch-dsl
inDjango
project?