Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch delay in store and search immediately

I am using elasticsearch with python. and use dsl driver in python.

My script is as below.

import time
from elasticsearch_dsl import DocType, String
from elasticsearch import exceptions as es_exceptions
from elasticsearch_dsl.connections import connections

ELASTICSEARCH_INDEX = 'test'

class StudentDoc(DocType):
    student_id = String(required=True)
    tags = String(null_value=[])

    class Meta:
        index = ELASTICSEARCH_INDEX

    def save(self, **kwargs):
        '''
        Override to set metadata id
        '''
        self.meta.id = self.student_id
        return super(StudentDoc, self).save(**kwargs)

# Define a default Elasticsearch client
connections.create_connection(hosts=['localhost:9200'])

# create the mappings in elasticsearch
StudentDoc.init()

student_doc_obj = \
    StudentDoc(
        student_id=str(1),
        tags=['test'])

try:
    student_doc_obj.save()
except es_exceptions.SerializationError as ex:
    # catch both exception raise by elasticsearch
    LOGGER.error('Error while creating elasticsearch data')
    LOGGER.exception(ex)
else:
    print "*"*80
    print "Student Created:", student_doc_obj
    print "*"*80


search_docs = \
    StudentDoc \
    .search().query('ids',
                    values=["1"])
try:
     student_docs = search_docs.execute()
except es_exceptions.NotFoundError as ex:
    LOGGER.error('Unable to get data from elasticsearch')
    LOGGER.exception(ex)
else:
    print "$"*80
    print student_docs
    print "$"*80

time.sleep(2)

search_docs = \
    StudentDoc \
    .search().query('ids',
                    values=["1"])
try:
     student_docs = search_docs.execute()
except es_exceptions.NotFoundError as ex:
    LOGGER.error('Unable to get data from elasticsearch')
    LOGGER.exception(ex)
else:
    print "$"*80
    print student_docs
    print "$"*80

In this script, I am creating StudentDoc and try to access same doc when create. I get empty response when do search on the record.

OUTPUT

********************************************************************************
Student Created: {'student_id': '1', 'tags': ['test']}
********************************************************************************
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<Response: []>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
<Response: [{u'student_id': u'1', u'tags': [u'test']}]>
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

save command execute and store data, then also why search not return tat data. after 2 second sleep, it return data. :(

Tried same with curl commands, same output.

echo "Create Data"
curl http://localhost:9200/test/student_doc/2 -X PUT -d '{"student_id": "2", "tags": ["test"]}' -H 'Content-type: application/json'

echo
echo "Search ID"
curl http://localhost:9200/test/student_doc/_search -X POST -d '{"query": {"ids": {"values": ["2"]}}}' -H 'Content-type: application/json'
echo

Is there any delay in storing data to elasticsearch?

like image 539
Nilesh Avatar asked Mar 12 '23 15:03

Nilesh


1 Answers

Yes, once you index a new document, it is not available until a refresh of the index occurs. You have a few options, though, the main ones being.

A. You can refresh the test index using the underlying connection just after saving student_doc_obj and before searching for it:

connections.get_connection.indices.refresh(index= ELASTICSEARCH_INDEX)

B. You can get the document instead of searching for it, as a get is completely real-time and doesn't need to wait for a refresh:

student_docs = StudentDoc.get("1")

Similarly, using curl, you can simply add the refresh query string parameter in your PUT call

echo "Create Data"
curl 'http://localhost:9200/test/student_doc/2?refresh=true' -X PUT -d '{"student_id": "2", "tags": ["test"]}' -H 'Content-type: application/json'

Or you can simply GET the document by id

echo "GET ID"
curl -XGET http://localhost:9200/test/student_doc/2
like image 74
Val Avatar answered Mar 28 '23 23:03

Val