# Java High Level REST Client - order of fields in document during saving and retrieval

**URL:** <https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621>\
**Category:** Elasticsearch\
**Created:** [February 3, 2020, 12:02pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621 "2020-02-03T12:02:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jb79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jb79/32/61949_2.png) [@jb79](https://discuss.elastic.co/u/jb79)\
**Post date:** [February 3, 2020, 12:02pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/1 "2020-02-03T12:02:41Z")

</div>

1. Is the order of the saved document structure guaranteed in elasticsearch? Currently I see that it is, but I don't know if it's because of some default value of some option in the es-rest-client - generally I'd like to know what is the reason it is working 😉 Nevertheless apart of rest-client there could be still on elasticsearch site 'problem', when the order cannot be guaranteed by the database itself (or should be configured) - so does elasticsearch make maybe such guarantee?

2. The structure of obtained document in Kibana is different, then the one retrieved by es-rest-client - is there a possibility to retrieve it 1:1 as it is in elasticsearch (so as it is displayed in Kibana) ?  
If it's not possible then, is maybe the order of an array guaranteed?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 3, 2020, 3:15pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/2 "2020-02-03T15:15:29Z")

</div>

can you be more specific what order you are talking about? Is it the structure of the JSON document? That is not changed. However your serialization library (or the one frmo kibana) may freely chose how to serialize an object to JSON, as long as it is valid.

May I ask what the background is for this question? It seems that there is a question hidden beneath a question...

---

<div class="post-metadata">

**Author:** ![jb79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jb79/32/61949_2.png) [@jb79](https://discuss.elastic.co/u/jb79)\
**Post date:** [February 7, 2020, 10:26am UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/3 "2020-02-07T10:26:47Z")

</div>

Thanks,

So one topic is clear.  
Ths issue is that when I get doc via 'Java High Level REST Client' it's deserialization process changes the doc structure - so as you've written "your serialization library (or the one frmo kibana) may freely chose how to serialize an object to JSON, as long as it is valid.", so Kibana returns same object I've sent to index, f.e.:  
INPUT doc:  
{  
"a": {  
"aa": "1",  
"aaa": "11"  
},  
"b": {  
"bb": "2",  
"bbb": "22"  
}  
}

--- Save doc in ES with Java\_HL\_REST\_Client

OUTPUT doc visible in Kibana:

{  
"a": {  
"aa": "1",  
"aaa": "11"  
},  
"b": {  
"bb": "2",  
"bbb": "22"  
}  
}

but OUTPUT doc returned by Java\_HL\_REST\_Client, can be f.e. like this (actually it's object returned by getters of GetResponse, but just to show loosing the original doc structure) :  
{  
"b": {  
"bb": "2",  
"bbb": "22"  
},  
"a": {  
"aaa": "11",  
"aa": "1"  
}  
}  
As you wrote "Is it the structure of the JSON document? That is not changed.", the question is now about the Java\_HL\_REST\_Client. Can the deserializer by somehow controlled, so it does not change the structure of the doc? Seems like f.e. HashMap and not LinkedHashMap is used in deserialization.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 7, 2020, 2:12pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/4 "2020-02-07T14:12:56Z")

</div>

as I said already, this depends on the components indexing and I would absolutely not worry about it. Can you explain, why this is an issue?

---

<div class="post-metadata">

**Author:** ![jb79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jb79/32/61949_2.png) [@jb79](https://discuss.elastic.co/u/jb79)\
**Post date:** [February 7, 2020, 2:56pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/5 "2020-02-07T14:56:47Z")

</div>

Let's say we have a service that sends object to be saved in elastic, this object contract is that the order of append(key,value) method invocation will be the order of elements on UI. We also don't know what will be put into append().  
This contract was fulfilled till rest-client library change : io.searchbox.client.jest-client -\> elasticsearch-rest-high-level-client. So now we are figuring out if we can somehow configure new rest-client or change structure of the object -\> instead of a map an array of tuples, or some other solution.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 7, 2020, 3:13pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/6 "2020-02-07T15:13:16Z")

</div>

can you share a code snippet how you are creating that JSON? If you are using the xcontent builder classes, this should not be an issue.

---

<div class="post-metadata">

**Author:** ![jb79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jb79/32/61949_2.png) [@jb79](https://discuss.elastic.co/u/jb79)\
**Post date:** [February 7, 2020, 4:38pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/7 "2020-02-07T16:38:05Z")

</div>

private final RestHighLevelClient elasticsearchClient;

```
public SearchTemplateResponse executeTemplateQuery(String query, Map<String, Object> scriptParams, String indexName) throws IOException {
    SearchTemplateRequest request = new SearchTemplateRequest();
    request.setRequest(new SearchRequest(indexName));
    request.setScriptType(ScriptType.INLINE);
    request.setScript(query);
    request.setScriptParams(scriptParams);
    return elasticsearchClient.searchTemplate(request, getRequestOptions());
}

SearchTemplateResponse searchResponse = executeTemplateQuery(query, queryParams, indexName);
return searchResponse.getResponse().toString();

```

// I also tried following, but searchResponse.getSource() is null , although response is available therefore we use searchResponse.getResponse().toString()  
//  
// Tuple\<XContentType, Map\<String, Object\>\> xContentTypeMapTuple = XContentHelper  
// .convertToMap(searchResponse.getSource(), true, XContentType.JSON);  
// Map\<String, Object\> stringObjectMap = xContentTypeMapTuple.v2();

---

<div class="post-metadata">

**Author:** ![jb79](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jb79/32/61949_2.png) [@jb79](https://discuss.elastic.co/u/jb79)\
**Post date:** [February 16, 2020, 10:04pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/8 "2020-02-16T22:04:16Z")

</div>

Any chances to make progress on this? Can you maybe suggest to try some other approach? maybe with the low-level client?

---

<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:** [March 15, 2020, 10:04pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-order-of-fields-in-document-during-saving-and-retrieval/217621/9 "2020-03-15T22:04:29Z")

</div>

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