wa

🧩 Syntax:
from elasticsearch import Elasticsearch

# Define the Elasticsearch servers
dev_host = "http://dev_elasticsearch_host:9200"
prod_host = "http://prod_elasticsearch_host:9200"

# Create Elasticsearch clients for dev and prod servers
dev_client = Elasticsearch(dev_host)
prod_client = Elasticsearch(prod_host)

# Define the prefix of the indexes you want to copy
index_prefix = "lsfhost_"

# Fetch a list of indexes in dev that match the prefix
indexes_to_copy = [index for index in dev_client.indices.get(index=index_prefix + "*")]

# Function to transform attributes (modify as needed)
def transform_attributes(doc):
    # Example: Change keyword field 'my_field' to an integer
    if 'my_field' in doc['_source']:
        doc['_source']['my_field'] = int(doc['_source']['my_field'])
    # You can add more transformations here

    return doc

# Loop through each index and copy documents to prod
for index_name in indexes_to_copy:
    print(f"Copying index: {index_name}")
    index_settings = dev_client.indices.get_settings(index=index_name)
    index_mappings = dev_client.indices.get_mapping(index=index_name)
    
    # Create the index with the same settings and mappings in prod
    prod_client.indices.create(index=index_name, body={
        "settings": index_settings[index_name]["settings"],
        "mappings": index_mappings[index_name]["mappings"]
    })
    
    # Scroll through the documents in dev and copy to prod
    scroll = dev_client.search(index=index_name, scroll='2m', size=1000)
    while len(scroll['hits']['hits']) > 0:
        for doc in scroll['hits']['hits']:
            # Transform attributes before uploading to prod (if needed)
            transformed_doc = transform_attributes(doc)
            # Upload the transformed document to prod
            prod_client.index(index=index_name, body=transformed_doc['_source'])
        # Continue scrolling
        scroll = dev_client.scroll(scroll_id=scroll['_scroll_id'], scroll='2m')
    
    # Clear the scroll context
    dev_client.clear_scroll(scroll_id=scroll['_scroll_id'])
    
    print(f"Finished copying index: {index_name}")

print("All indexes copied.")