← Back to list

How to connect python to elastic search deployed on ES8

python script

Palakgarg · 2024-04-03 10:25 · 0 claps · 0.7 min read
#python #elasticsearch #es8 #coding
Open on Medium ↗
Wiki topics: 💻 · Programming

How to connect python to elastic search deployed on ES8

  • python script
from elasticsearch import Elasticsearch

es = Elasticsearch(
    hosts=["http://localhost:8085"],
)

source_index = 'source_index'
destination_index = 'destination_index'

 # Query the source index to retrieve documents
source_query = {
        "query": {
            "match_all": {}
        }
    }
source_results = es.search(index=source_index, body=source_query)

for hit in source_results["hits"]["hits"]:
        source_doc = hit["_source"]
        date = source_doc["date"] # take fields you want to query out in destination index
        ulb = source_doc["ulb"]


        target_query = {
                    "query": {
                        "bool": {
                            "must": [
                                {"term": {"date": date}},
                                {"term": {"ulb": ulb}}
                            ]
                        }
                    }
                }
        target_results = es.search(index=destination_index, body=target_query)

        for target_hit in target_results["hits"]["hits"]:
                    target_doc_id = target_hit["_id"]
                    target_doc = target_hit["_source"]

                    # Update fields in target document with values from source document
                    target_doc.update({
                        "todaysApprovedApplicationsWithinSLA": source_doc["todaysApprovedApplicationsWithinSLA"],
                        "todaysApplications": source_doc["todaysApplications"],
                        "adhocPenalty": source_doc["adhocPenalty"],
                        "transactions": source_doc["transactions"],
                        "avgDaysForApplicationApproval": source_doc["avgDaysForApplicationApproval"],
                        "todaysApprovedApplications": source_doc["todaysApprovedApplications"],
                        "todaysLicenseIssuedWithinSLA": source_doc["todaysLicenseIssuedWithinSLA"],
                        # Add other fields to update as needed
                    })

                    print(target_doc)

                    # Update the document in the target index
                    es.index(index=destination_index, id=target_doc_id, body=target_doc) 
  • port forward the elastic search master pod using the below command
kubectl port-forward elasticsearch-master-v1–0 8085:9200 -n es-cluster - kubeconfig << Path to kube config file >>

메타데이터
post_id
7977ba1b9d59
slug
how-to-connect-python-to-elastic-search-deployed-on-es8-7977ba1b9d59
url
https://medium.com/@palakgarg8818/how-to-connect-python-to-elastic-search-deployed-on-es8-7977ba1b9d59
canonical_url
https://medium.com/@palakgarg8818/how-to-connect-python-to-elastic-search-deployed-on-es8-7977ba1b9d59
author_url
https://medium.com/@palakgarg8818
status
ok
fetched_at
2026-07-24 03:07:57