Elasticsearch: How to append data to exist data in array?

Created on 24 Feb 2015  路  3Comments  路  Source: elastic/elasticsearch

Hi ,

I have a document on Elastic search in below format.

{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
}

now I want to update the same document with new value on "tags" array.
new value is "yellow".

after updating "yellow " to exist document, My document should display as below.

{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green","yellow"]
}

Question : How to merge new value with existing values ?
Some one please guide me on this?

All 3 comments

Simply reindex the document, it will then update it. See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html

Hi Markwalkom,

ReIndex the document is overwriting/update the old values to new values.
I mean,I could see the following json after reindexing or calling update and upsert api's.

{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["yellow"]
}

In the output json, I have lost my old values from array tag
"tags": ["home", "green"]

Java Api's to update the exist doc or create new document if not exit.

IndexRequest indexRequest = new IndexRequest("user", "test","1")
.source(builder);
UpdateRequest updateRequest = new UpdateRequest("user", "test","1")
.doc(builder)
.upsert(indexRequest);
client.update(updateRequest).get();

This is exactly what is happening behind the scene. Update API get the previous doc, merge values, index the full new doc.

BTW please ask questions on the mailing list. This space is for issues or feature requests.

Was this page helpful?
0 / 5 - 0 ratings