Updating Indexed Entities

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

{
	"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

{
	"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

{
	"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

{
	"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

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 to do your update (which yes, will overwrite documents), but should have used the Update API. 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!

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.

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.

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