Insert and Get data from an Elasticsearch index in python

Last updated 4 months, 1 week ago | 220 views 75     5

Python | Insert and Get data from an index in Elasticsearch

To do so we need to import the Elasticsearch package and create an instance of that by providing the host and port.

>>> from elasticsearch import Elasticsearch
>>> es = Elasticsearch("http://localhost:9200")

Here are some JSON data to add to Elasticsearch.

>>> student1 = {"name":"Ram", "roll":101, "country":"India"}
>>> student2 = {"name":"Jon", "roll":102, "country":"USA"}    
>>> student3 = {"name":"Tom", "roll":103, "country":"England"}

To create an index and push data into it, we use the index method and provided values for 4 parameters. it will automatically create the index and push the data into the index.

  1. index: ES index name
  2. doc_type: ES doc_type
  3. id: Unique value for each record
  4. body: Data to push into the ES index
>>> es.index(index="student", doc_type="student_record", id=1, body=student1)
{'_index': 'student', '_type': 'student_record', '_id': '1', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
>>> es.index(index="student", doc_type="student_record", id=2, body=student2) 
{'_index': 'student', '_type': 'student_record', '_id': '2', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}
>>> es.index(index="student", doc_type="student_record", id=3, body=student3) 
{'_index': 'student', '_type': 'student_record', '_id': '3', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}

The get method is used to fetch data from an ES index.

>>> data = es.get(index="student", doc_type="student_record", id=3) 
>>> data
{'_index': 'student', '_type': 'student_record', '_id': '3', '_version': 1, '_seq_no': 2, '_primary_term': 1, 'found': True, '_source': {'name': 'Tom', 'roll': 103, 'country': 'England'}}

In the above data, the actual data is available in _source key and the found key has a True value if the mentioned id does not exist in the ES index then the found key returns with a False value.

Let's call _source to get the actual data

>>> data["_source"]
{'name': 'Tom', 'roll': 103, 'country': 'England'}