Facebooktwittergoogle_plusredditpinterestlinkedinmail

Update Document in Elasticsearch

Documents in Elasticsearch are always immutable we cannot replace or change them directly. If we require updating an existing document, we need to reindex or replace it. We can change an existing document by using Index API as well. Elasticsearch delete the old document automatically and add a new document internally.

Lets start step by step process to understand how document index and update will work.

Step 1. Add new document

curl -XPUT 'localhost:9200/roopendra/post/1' -d '{
    "title" : "Test this test post",
    "text": "Testing update",
    "date": "2015-10-04 12:22:22"
 }'

Response:

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "1",
    "_version": 1,
    "created": true
}

In first time elasticsearch assign _version to the document and “created” flag is true.

Step 2: Update already index document:

curl -XPUT 'localhost:9200/roopendra/post/1' -d '{
    "title" : "Test this test post",
    "text": "Testing update document in Elasticsearch",
    "date": "2015-10-04 12:22:22"
 }'

Response:

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "1",
    "_version": 2,
    "created": false
}

When we update an existing document then elasticsearch internally marked delete old document and create new document. now version of this document is 2 and “created” is false.

Step 3: Add new field in existing document:

We can add new field in existing document. Elasticsearch automatically create mapping for new field.

curl -XPUT 'localhost:9200/roopendra/post/1' -d '{
    "title" : "Test this test post",
    "text": "Testing update document in Elasticsearch",
    "date": "2015-10-04 12:22:22",
    "author": "roop"
 }'

Response:

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "1",
    "_version": 3,
    "created": false
}

Now new field “author” is added in existing document and _version is 3.

Verify document

curl -XGET 'http://localhost:9200/roopendra/post/1/'

Response:

{
    "_index": "roopendra",
    "_type": "post",
    "_id": "1",
    "_version": 3,
    "found": true,
    "_source": {
        "title": "Test this test post",
        "text": "Testing update document in Elasticsearch",
        "date": "2015-10-04 12:22:22",
        "author": "roop"
    }
}
Updating Whole Document in Elasticsearch Roopendra DevOPSElasticsearch,
Update Document in Elasticsearch Documents in Elasticsearch are always immutable we cannot replace or change them directly. If we require updating an existing document, we need to reindex or replace it. We can change an existing document by using Index API as well. Elasticsearch delete the old document automatically and...
<h2>Update Document in Elasticsearch</h2> Documents in Elasticsearch are always <em>immutable</em> we cannot replace or change them directly. If we require updating an existing document, we need to reindex or replace it. We can change an existing document by using Index API as well. Elasticsearch delete the old document automatically and add a new document internally.Lets start step by step process to understand how document index and update will work. <h3>Step 1. Add new document</h3> <pre>curl -XPUT 'localhost:9200/roopendra/post/1' -d '{ "title" : "Test this test post", "text": "Testing update", "date": "2015-10-04 12:22:22" }'</pre> <strong>Response:</strong> <pre>{ "_index": "roopendra", "_type": "post", "_id": "1", "_version": 1, "created": true }</pre> In first time elasticsearch assign <em>_version</em> to the document and "<em>created</em>" flag is true. <h3>Step 2: Update already index document:</h3> <pre>curl -XPUT 'localhost:9200/roopendra/post/1' -d '{ "title" : "Test this test post", "text": "Testing update document in Elasticsearch", "date": "2015-10-04 12:22:22" }'</pre> <strong>Response:</strong> <pre>{ "_index": "roopendra", "_type": "post", "_id": "1", "_version": 2, "created": false }</pre> When we update an existing document then elasticsearch internally marked delete old document and create new document. now <em>version</em> of this document is 2 and "<em>created</em>" is false. <h3>Step 3: Add new field in existing document:</h3> We can add new field in existing document. Elasticsearch automatically create mapping for new field. <pre>curl -XPUT 'localhost:9200/roopendra/post/1' -d '{ "title" : "Test this test post", "text": "Testing update document in Elasticsearch", "date": "2015-10-04 12:22:22", "author": "roop" }'</pre> <strong>Response:</strong> <pre>{ "_index": "roopendra", "_type": "post", "_id": "1", "_version": 3, "created": false }</pre> Now new field "author" is added in existing document and _version is 3. <h3>Verify document</h3> <pre>curl -XGET 'http://localhost:9200/roopendra/post/1/'</pre> <strong>Response:</strong> <pre>{ "_index": "roopendra", "_type": "post", "_id": "1", "_version": 3, "found": true, "_source": { "title": "Test this test post", "text": "Testing update document in Elasticsearch", "date": "2015-10-04 12:22:22", "author": "roop" } }</pre>
(Visited 388 times, 15 visits today)
Facebooktwittergoogle_pluslinkedinrss