# Updating Indexed Entities

**URL:** https://discuss.elastic.co/t/updating-indexed-entities/348287
**Category:** Elasticsearch
**Created:** [November 30, 2023, 4:59am UTC](https://discuss.elastic.co/t/updating-indexed-entities/348287 "2023-11-30T04:59:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Muhammad\_namjas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_namjas/32/126607_2.png) [@Muhammad\_namjas](https://discuss.elastic.co/u/Muhammad_namjas)
#### Post date: [November 30, 2023, 4:59am UTC](https://discuss.elastic.co/t/updating-indexed-entities/348287/1 "2023-11-30T04:59:52Z")

</div>

I created a new entity connected to Hibernate Elastic Search and indexed it. Upon retrieving the indexed data, I noticed that updating the entity using the student ID resulted in deleting the existing data and re-inserting the updated data. To preserve the existing data along with the updated data, I need to merge the updated data with the existing data before indexing the entity.

Student insertion request

```auto
{
	"studentId": "1",
	"studentMark": "10",
	"studentName": "LMN",
	"data": [
		{
			"id": "1",
			"mark": "10",
			"name": "ABC",
			"status": "Inprogress"
		},
		{
			"id": "5",
			"mark": "10",
			"name": "XYZ",
			"status": "READY_TO_PROCESS"
		}
	]
}

```

Upon completing the data insertion, I fetched the latest data from the Elasticsearch server.  
**Response**

```auto
{
	"took": 8,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.0,
		"hits": [
			{
				"_index": "student-000001",
				"_type": "_doc",
				"_id": "1",
				"_score": 1.0,
				"_source": {
					"data": [
						{
							"id": "1",
							"mark": "10",
							"name": "ABC",
							"status": "Inprogress"
						},
						{
							"id": "5",
							"mark": "10",
							"name": "XYZ",
							"status": "READY_TO_PROCESS"
						}
					],
					"studentId": "1",
					"studentMark": "10",
					"studentName": "LMN",
					"_entity_type": "StudentEntity"
				}
			}
		]
	}
}

```

Update the entity using the student ID.  
**Request**

```auto
{
	"studentId": "1",
	"studentMark": "10",
	"studentName": "OPQ",
	"data": [
		{
			"id": "1",
			"mark": "10",
			"name": "HIJ",
			"status": "READY_TO_PROCESS"
		}
	]
}

```

After updating the data, I retrieved it again from the Elasticsearch server to verify the changes.  
**Response**

```auto
{
	"took": 8,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 1.0,
		"hits": [
			{
				"_index": "student-000001",
				"_type": "_doc",
				"_id": "1",
				"_score": 1.0,
				"_source": {
					"data": [
						{
							"id": "1",
							"mark": "10",
							"name": "HIJ",
							"status": "READY_TO_PROCESS"
						}
					],
					"studentId": "1",
					"studentMark": "10",
					"studentName": "OPQ",
					"_entity_type": "StudentEntity"
				}
			}
		]
	}
}

```

i found that after updating the entity defiantly its deleting the existing data and re-inserting the new changes.  
How to avoid this change. i need to get existing as well as the updated data. Please help me too fix this issue

---

<div class="post-metadata">

### Author: ![Sean\_Story](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sean_story/32/69987_2.png) [@Sean\_Story](https://discuss.elastic.co/u/Sean_Story)
#### Post date: [December 6, 2023, 7:38pm UTC](https://discuss.elastic.co/t/updating-indexed-entities/348287/2 "2023-12-06T19:38:10Z")

</div>

Hi @Muhammad_namjas ,

Your post contains the _bodies_ you used in your requests, but doesn't say what endpoints you POST'd them to. My guess though, is that you used the [docs index API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#docs-index-api-example) to do your update (which yes, will overwrite documents), but should have used the [Update API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html). This API is addative, and will not replace the existing document, it will only replace existing fields that have new values provided in your POST body.

Hope that helps!

---

<div class="post-metadata">

### Author: ![Muhammad\_namjas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/muhammad_namjas/32/126607_2.png) [@Muhammad\_namjas](https://discuss.elastic.co/u/Muhammad_namjas)
#### Post date: [December 27, 2023, 5:55am UTC](https://discuss.elastic.co/t/updating-indexed-entities/348287/3 "2023-12-27T05:55:48Z")

</div>

Hi @Sean_Story  
While I'm exclusively performing insertions and updates within the database, Hibernate Search is seamlessly reflecting these changes in the Elasticsearch server synchronously. I'm not explicitly invoking any Elasticsearch APIs directly. Instead, I believe Hibernate Search is implicitly handling those API calls internally.  
so how can i handle this in code level. can u pls help me.

---

<div class="post-metadata">

### Author: ![Sean\_Story](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sean_story/32/69987_2.png) [@Sean\_Story](https://discuss.elastic.co/u/Sean_Story)
#### Post date: [January 2, 2024, 8:14pm UTC](https://discuss.elastic.co/t/updating-indexed-entities/348287/4 "2024-01-02T20:14:56Z")

</div>

Ah, I see, I misunderstood your situation.

You may want to reach out to Hibernate to ask this question. I have not used Hibernate Search before and am not sure how to advise you for 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: [January 30, 2024, 8:15pm UTC](https://discuss.elastic.co/t/updating-indexed-entities/348287/5 "2024-01-30T20:15:47Z")

</div>

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