# How do I transfer mappings from one index to another with Java Api Client

**URL:** <https://discuss.elastic.co/t/how-do-i-transfer-mappings-from-one-index-to-another-with-java-api-client/320489>\
**Category:** Elasticsearch\
**Created:** [December 5, 2022, 5:22pm UTC](https://discuss.elastic.co/t/how-do-i-transfer-mappings-from-one-index-to-another-with-java-api-client/320489 "2022-12-05T17:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JDev64](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jdev64/32/114248_2.png) [@JDev64](https://discuss.elastic.co/u/JDev64)\
**Post date:** [December 5, 2022, 5:22pm UTC](https://discuss.elastic.co/t/how-do-i-transfer-mappings-from-one-index-to-another-with-java-api-client/320489/1 "2022-12-05T17:22:39Z")

</div>

Hello, I'm currently building a Spring Java application and I use the [Elastic Java Client] ([Elasticsearch Java API Client [8.5] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/index.html)) (Version: 8.3.3) to communicate between my application and my elastic cluster.

I've come to notice that the client is not that well documented and I regularly encounter development roadblocks because I can't figure out how to "translate" my request that I would use in kibana to a request for the java client.

My current problem revolves around copying the mappings of one index to another one.  
Since I noticed some problems using the reindex api with my data I came to the conclusion to transfer the mappings beforehand and reindex after that.

I tested it in kibana and it seemed to work. The request I want to create using the Java Client is the following:

```auto
PUT /my-index-000001/_mapping
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}

```

I'm currently getting the mappings of my source index via the Get Mapping api but the problem is I dont know how exactly I use the list of mappings to put them into the Put Mapping Request.  
My mappings are in the form of a json string but how do I take a single field and convert it to a Property object that is required for request?

So how do I copy the mappings of one index to another using the Java Client without copying the data (e.g: documents)?

---

<div class="post-metadata">

**Author:** ![JDev64](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jdev64/32/114248_2.png) [@JDev64](https://discuss.elastic.co/u/JDev64)\
**Post date:** [December 6, 2022, 10:31pm UTC](https://discuss.elastic.co/t/how-do-i-transfer-mappings-from-one-index-to-another-with-java-api-client/320489/2 "2022-12-06T22:31:38Z")

</div>

If anyone should ever encounter the same problem here is the solution I found:

In the elastic documentation I came across this article: [Creating API objects from JSON data](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/loading-json.html)  
The Java API Client uses a simple HTTP client under the hood so you can just create request from a json input here is an example of how I used this method to transfer the mappings of one index to another:

In the following example the variable source are my index mappings as a string in valid json format and I'm using the Gson library to get the properties part of my mappings

```java
Gson gson = new Gson();
JsonElement jsonObj = gson.fromJson(mappings, JsonElement.class);
JsonObject properties = jsonObj.getAsJsonObject().get(source).getAsJsonObject().get("mappings").getAsJsonObject();
InputStream inputStream = new ByteArrayInputStream(properties.toString().getBytes());
PutMappingResponse response = client.indices().putMapping(p -> p.index(dest).withJson(inputStream))

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 3, 2023, 10:32pm UTC](https://discuss.elastic.co/t/how-do-i-transfer-mappings-from-one-index-to-another-with-java-api-client/320489/3 "2023-01-03T22:32:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
