# How to upadate value with java api without merging it, and only append that's not same

**URL:** https://discuss.elastic.co/t/how-to-upadate-value-with-java-api-without-merging-it-and-only-append-thats-not-same/166075
**Category:** Elasticsearch
**Created:** [January 29, 2019, 3:45am UTC](https://discuss.elastic.co/t/how-to-upadate-value-with-java-api-without-merging-it-and-only-append-thats-not-same/166075 "2019-01-29T03:45:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Satri\_Wantara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satri_wantara/32/40205_2.png) [@Satri\_Wantara](https://discuss.elastic.co/u/Satri_Wantara)
#### Post date: [January 29, 2019, 3:45am UTC](https://discuss.elastic.co/t/how-to-upadate-value-with-java-api-without-merging-it-and-only-append-thats-not-same/166075/1 "2019-01-29T03:45:42Z")

</div>

Excuse me Sir, I’am still new in elastic, I use java as client, I have a problem that how to update data without merging it, and if the data is there, it did’nt update it,,but increase/append it, and if data is same, it did’nt append again, so there is no multiple with same value,this is the simple example

this is my Food.java  
\<  
private string id  
private string name  
private string alias

public String getId() {  
return Id;  
}

```
public void setId(String Id) {
    this.Id = Id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String[] getAlias() {
	return alias;
}

public void setAlias(String[] alias) {
    this.alias = alias;
}

```

/\>

this is my FoodController.java  
\<  
@PutMapping("/upsert/{id}")  
public String upsert(@PathVariable final String id, @RequestBody Food food) throws IOException {  
try {  
IndexRequest indexRequest = new IndexRequest("foods", "foodtype", id)  
.source(jsonBuilder()  
.startObject()  
.field("name", food.getName())  
.field("alias", food.getAlias())  
.endObject());  
UpdateRequest updateRequest = new UpdateRequest("foods", "foodtype", id)  
.doc(jsonBuilder()  
.startObject()  
.field("name", food.getName())  
.field("alias", food.getAlias())  
.endObject())  
.upsert(indexRequest);  
client.update(updateRequest).get();  
System.out.println(client.update(updateRequest).get().status());  
return client.update(updateRequest).get().status().toString();  
} catch (InterruptedException | ExecutionException e){  
System.out.println(e);  
}  
return "Exception";  
}  
/\>

So how to update it with java api?, if I have the data like this  
\<  
{  
“name”:”ChikenCheese”,  
“alias”: [“CC”,”CCK”,”CCKH”]  
}  
/\>

and when I send with PUT method from request body with this  
\<  
{  
“alias”: [“CC”,”CCK”,”CCCBBB”]  
}  
/\>

it will be like this  
\<  
{  
“name”:”ChikenCheese”,  
“alias”: [“CC”,”CCK”,”CCKH”,”CCCBBB”]  
}  
/\>

it only append to "alias" that is not same, and the name is not changed to null, i’am sorry for a long question.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 29, 2019, 4:14am UTC](https://discuss.elastic.co/t/how-to-upadate-value-with-java-api-without-merging-it-and-only-append-thats-not-same/166075/2 "2019-01-29T04:14:36Z")

</div>

You can't do that.

Either you provide the new version of the full document and use index API, either you provide a new version of some fields and use the update API.

You can't provide a sub part of a field and expect it to be merged with existing values for this field.

So this is something you need to solve in your application by running a GET of the full document the update it in your application and the send it back entirely to elasticsearch with the index API.

---

<div class="post-metadata">

### Author: ![Satri\_Wantara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satri_wantara/32/40205_2.png) [@Satri\_Wantara](https://discuss.elastic.co/u/Satri_Wantara)
#### Post date: [January 29, 2019, 4:17am UTC](https://discuss.elastic.co/t/how-to-upadate-value-with-java-api-without-merging-it-and-only-append-thats-not-same/166075/3 "2019-01-29T04:17:23Z")

</div>

Thank you sir for your fast respons, i'll try it

---

<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: [February 26, 2019, 4:17am UTC](https://discuss.elastic.co/t/how-to-upadate-value-with-java-api-without-merging-it-and-only-append-thats-not-same/166075/4 "2019-02-26T04:17:28Z")

</div>

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