Sitemap

How to connect python to elastic search deployed on ES8

1 min readApr 3, 2024

--

  • python script

Get Palakgarg’s stories in your inbox

Join Medium for free to get updates from this writer.

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 >>

--

--