Combine value for a common field from search results and remove one of the result

Hi dear,

We have a some documents saved in Elasticsearch with a common value, like group_id. When we do the search, we'd like to combine the value of specific field if the documents are having the same group_id. Then we want to return the primary document and exclude the other documents to keep the aggregations and search results clean.

For example, in Elasticsearch index, we have documents like below:

{
	"id": 111,
	"group_id": 1,
	"variants": [
		{
			"title": "test title 1.1",
			"description": "test description 1.1"
		}
	]
}

{
	"id": 222,
	"group_id": 1,
	"variants": [
		{
			"title": "test title 1.2",
			"description": "test description 1.2"
		}
	]
}

{
	"id": 333,
	"group_id": 2,
	"variants": [
		{
			"title": "test title 2",
			"description": "test description 2"
		}
	]
}

When I do search, I'd like to see the variant object can be combined if the documents are having the same group_id, like below

{
	"id": 111,
	"group_id": 1,
	"variants": [
		{
			"title": "test title 1.1",
			"description": "test description 1.1"
		},
		{
			"title": "test title 1.2",
			"description": "test description 1.2"
		}
	]
}

{
	"id": 333,
	"group_id": 2,
	"variants": [
		{
			"title": "test title 2",
			"description": "test description 2"
		}
	]
}

Note, document 222 is gone, because we have combined the variant object of document 222 to 111.

Is there a way to achieve the above goal while we are using the search query? We don't want to update how those documents are saved in Elasticsearch index, so we are expecting the smart logic is happening in the search query. Also, each search response contains the total object, which has the aggregations like value. We also expecting the total.value tells us the truth from the results perspective, so total.value should be 2 rather then 3.

Again, I'm not sure whether the search query can reach our goal, as it looks pretty complex to me

Welcome to our community! :smiley:

Does the collapse option help?

Thank you @warkolm for your response :slightly_smiling_face:
I can see collapse does reduced the number of documents from the search result, but it doesn't combine the variant object :smiling_face_with_tear:

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