Skip to content

Instantly share code, notes, and snippets.

@erdii
Last active December 28, 2022 10:43
Show Gist options
  • Save erdii/359263c2f24e08b0eda1f6ef04b2f421 to your computer and use it in GitHub Desktop.
Save erdii/359263c2f24e08b0eda1f6ef04b2f421 to your computer and use it in GitHub Desktop.
Copy an elasticsearch index definition (settings and mapping - no data) to another server
# copy index definition
INDEX_NAME="my_index"
curl -X GET "http://source-server:9200/$INDEX_NAME" | curl -X PUT "http://destination-server:9200/$INDEX_NAME" -d @-
# copy mapping for "my_doc_type"
DOC_TYPE="my_doc_type"
curl -X GET "http://source-server:9200/$INDEX_NAME/_mapping" | jq .$INDEX_NAME.mappings.$DOC_TYPE | curl -X PUT "http://destination-server:9200/$INDEX_NAME/_mapping/$DOC_TYPE" -d @-
@tzahij
Copy link

tzahij commented Apr 3, 2022

Thanks

@abegum123
Copy link

abegum123 commented Sep 8, 2022

I am trying to copy the index definition from one index to another index within the same server. I have used copy_elasticsearch_index_to_other_server.sh script and getting below error. please suggest what changes are required here.

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

@abegum123
Copy link

Modified script and added header as content-type header as application-json, but this time different error I am getting

{"error":{"root_cause":[{"type":"parse_exception","reason":"unknown key [indexname] for create index"}],"type":"parse_exception

@ferzerkerx
Copy link

Modified script and added header as content-type header as application-json, but this time different error I am getting

{"error":{"root_cause":[{"type":"parse_exception","reason":"unknown key [indexname] for create index"}],"type":"parse_exception

The response from the index details (curl -X GET "http://source-server:9200/$INDEX_NAME") contains the index as a key which is what the error is saying. In this case the resulting JSON needs to be modified to be able to send it as a request

curl -sX GET "http://source-server:9200/$INDEX_NAME" | jq "to_entries[0].value" | jq "del(.settings.index.creation_date, .settings.index.uuid, .settings.index.provided_name, .settings.index.version.created)" | curl -sX PUT "http://destination-server:9200/$INDEX_NAME" -H 'Content-Type: application/json' -d @-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment