Extract data from elasticsearch and store into mongodb

I'm looking for an option to extract data from Elasticsearch and store into the mongodb using some python script. Please help me with that.

Hi Chinmoy,

Does it have to be done using Python? There is a similar question from February that suggests trying Logstash with an http output which might help:

Thanks @carly.richmond , by the way here what I have tried after googling,
can you suggest the script how it can be improved or
how can I query from an index pattern

from elasticsearch import Elasticsearch
from elasticsearch.helpers import scan
import pandas as pd
import pymongo
from pymongo import MongoClient

esClient = Elasticsearch('http://localhost:9200/')

def get_data_from_elastic():
    query = {
        "query": {
            "match": {
                 "state": "failed"
    res=scan(client = esClient,
        query = query,
        scroll = '1m',
        index = 'test-2022-11-09',
    result = list(res)
    temp = []
    for hit in result:
    df = pd.DataFrame(temp)
    return df
df = get_data_from_elastic()

client = MongoClient('mongodb://admin:vbox@')
db = client['elasticDB']
elastic_data = db['elastic_data']
data_dict = df.to_dict("records")

Hi Chinmoy,

I've not used the Python client personally, but looking at the example in the Python docs for the scan method you've used it looks like it might be valid to provide the index pattern to the index property of the scan method:


Have you given it a try?

Awesome!!! That's what I was looking for. Thanks a lot @carly.richmond

1 Like