Updating document and adding new field


(vinay) #1

Hi All,

We have usecase that data will be updated daily. Some of attributes of document changes and some of new record is there. Is it possible to reindex data with updated value, which is already there and add new reocord.
if yes, please explain how.

Is it with update API?


(vinay) #2

I am indexing like this

 String json = getJsonMapper().writeValueAsString(data);
    bulkRequestBuilder.add(getClient().prepareIndex(indexName, typeName).setSource(json));

I am not passing any id. How can i update this. What is best way


(David Pilato) #3

If you are not setting by yourself the _id and are not reading the generated _id by elasticsearch so you can't really update the document itself.

You can use the Update by query feature though.


(vinay) #4

Hi David,

Thanks for reply. If i set id with below code. So ideally, i should delete those document which i need to update then index again.
Is this the best approach? What you suggest.

public void add(String indexName, String type, String id, String parent, String routing, String ts, String json) {

    // make sure that these are not null
    Preconditions.checkNotNull(indexName);
    Preconditions.checkNotNull(type);
    Preconditions.checkNotNull(json);

    IndexRequestBuilder indexRequestBuilder = manager.getClient()
            .prepareIndex(indexName, type)
            .setSource(json);

    // / They didn't specify an ID, so we will create one for them.
    if(id != null)
        indexRequestBuilder.setId(id);

    if(ts != null)
        indexRequestBuilder.setTimestamp(ts);

    if(parent != null)
        indexRequestBuilder.setParent(parent);

    if(routing != null)
        indexRequestBuilder.setRouting(routing);

    add(indexRequestBuilder.request());
}

(David Pilato) #5

If you set the _id then you don't need to delete the document and index it again but just send the index operation again using the same index/type/id and you're done.

Behind the scene, elasticsearch will delete the document and index a new version.


(vinay) #6

Is it. Then its cool. I will try that.

Then when we should use update and reindex api. What the use case for using that


(system) #7