简体   繁体   中英

Migrate elasticsearch mapping with curl

I would like to migrate an elasticsearch mapping for an index using curl by piping the output of the mapping into curl.

The purpose is to migrate or create a mapping for an index on another host.

Now I am running the following:

curl -X GET "http://$SOURCE_HOSTNAME:9200/$SOURCE_INDEX/_mapping?pretty" | curl -X PUT "http://$DESTINATION_HOSTNAME:9200/$DESTINATION_INDEX/_mapping?pretty" -H 'Content-Type: application/json' -d "$(</dev/stdin)"

But I am getting the following error.

{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: mapping type is missing;"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: mapping type is missing;"
  },
  "status" : 400
}

It seems strange that the output which is correct is claiming here is missing a mapping type.

Same issue occurs when reading from json file directly

There are two issues. The first one is that the result you get from the GET $SOURCE_INDEX/_mapping call contains the mapping but it is wrapped within another property named as the source index.

For instance, the GET _mapping call will return this:

{
   "your_index_name": {     <--- your mapping is wrapped within the source index name
      "mappings": {         <--- and also within the mappings structure
         "doc": {           <--- this is the structure you need when hitting the _mapping endpoint
            ...
         }
      }
   }
}

The second issue is that your destination URL is missing the mapping type name, ie should be $DESTINATION_INDEX/_mapping/$SOME_TYPE .

So you need:

  1. to specify the mapping type in the destination URL
  2. a way to unwrap what you get from the source index. You can do this using (eg) jq easily though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM