# Updating/creating index using high level java client

**URL:** <https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java\
**Created:** [November 30, 2022, 8:19pm UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649 "2022-11-30T20:19:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Din](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@Din](https://forum.opensearch.org/u/Din)\
**Post date:** [November 30, 2022, 8:19pm UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649/1 "2022-11-30T20:19:55Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
opensearch-rest-high-level-client:2.4.0

**Describe the issue** :  
Index is being created as string instead of json

**Configuration** :  
Here is the code snippet  
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, “https”))  
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {  
@Override  
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {  
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);  
}  
});  
try (RestHighLevelClient client = new RestHighLevelClient(builder)😉 {

```
		IndexRequest request = new IndexRequest("record_index_pg"));
		request.id(uuid);

		HashMap<String, String> stringMapping = new HashMap<String, String>();
		
		stringMapping.put("record", json);
		request.source(stringMapping, XContentType.JSON);//this is not being honored and it is being passed as a string instead of json, resulting in the below representation
		IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
	} catch (IOException ex) {
		logger.error("IOException in OpenSearchService ", ex);
	} catch (Exception ex) {
		logger.error("Exception in OpenSearchService ", ex);
	}

```

The following is the index, please note that the double quotes after “record:” “{…}”  
{  
“\_index”: “record\_index”,  
“\_type”: “\_doc”,  
“\_id”: “123456”,  
“\_version”: 1,  
“\_score”: 0,  
“\_source”: {  
“record”: “{\n "uuid": "123456",\n "status": "S",\n "comments": "Test record only"}”  
}  
}

**Relevant Logs or Screenshots** :

---

<div class="post-metadata">

**Author:** ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)\
**Post date:** [November 30, 2022, 11:46pm UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649/2 "2022-11-30T23:46:29Z")

</div>

This seems like a bug. Maybe report it here: [Sign in to GitHub · GitHub](https://github.com/opensearch-project/OpenSearch/issues/new?assignees=&labels=bug%2C+untriaged&template=bug_template.md&title=%5BBUG%5D)

---

<div class="post-metadata">

**Author:** ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)\
**Post date:** [November 30, 2022, 11:48pm UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649/3 "2022-11-30T23:48:33Z")

</div>

There is a more supported client here: [GitHub - opensearch-project/opensearch-java: Java Client for OpenSearch](https://github.com/opensearch-project/opensearch-java)

Specifically, there is an async client implemented in opensearch-java:

> <https://github.com/opensearch-project/opensearch-java/blob/main/java-client/src/main/java/org/opensearch/client/opensearch/indices/OpenSearchIndicesAsyncClient.java>

---

<div class="post-metadata">

**Author:** ![Din](https://avatars.discourse-cdn.com/v4/letter/d/ccd318/32.png) [@Din](https://forum.opensearch.org/u/Din)\
**Post date:** [December 2, 2022, 12:54am UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649/4 "2022-12-02T00:54:48Z")

</div>

Thanks, for your response, is there a way to update part of an existing index with json?  
For eg:  
Current index  
{  
“\_index”: “record\_index”,  
“\_type”: “\_doc”,  
“\_id”: “123456”,  
“\_version”: 1,  
“\_score”: 0,  
“\_source”: {  
“record”: “{\n “uuid”: “123456”,\n “status”: “S”,\n “comments”: “Test record only”}”  
}  
}

update it to  
{  
“\_index”: “record\_index”,  
“\_type”: “\_doc”,  
“\_id”: “123456”,  
“\_version”: 1,  
“\_score”: 0,  
“\_source”: {  
“record”: “XYZ”  
}  
}

where XYZ is a json

something like this  
XContentBuilder jb = XContentFactory.jsonBuilder();  
jb.startObject().field(“record”, XYZ).endObject();

---

<div class="post-metadata">

**Author:** ![wbeckler](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@wbeckler](https://forum.opensearch.org/u/wbeckler)\
**Post date:** [December 5, 2022, 3:12am UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649/5 "2022-12-05T03:12:20Z")

</div>

I’m not sure and I hope someone else can pipe up on this.

---

<div class="post-metadata">

**Author:** ![reta](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@reta](https://forum.opensearch.org/u/reta)\
**Post date:** [December 5, 2022, 1:34pm UTC](https://forum.opensearch.org/t/updating-creating-index-using-high-level-java-client/11649/6 "2022-12-05T13:34:38Z")

</div>

@Din please take a look at Update API: [Update document - OpenSearch documentation](https://opensearch.org/docs/1.0/opensearch/rest-api/document-apis/update-document/)
