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"}”
}
}
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();