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

**URL:** <https://discuss.elastic.co/t/combine-value-for-a-common-field-from-search-results-and-remove-one-of-the-result/316831>\
**Category:** Elasticsearch\
**Created:** [October 17, 2022, 10:30pm UTC](https://discuss.elastic.co/t/combine-value-for-a-common-field-from-search-results-and-remove-one-of-the-result/316831 "2022-10-17T22:30:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Calvin\_Feng](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@Calvin\_Feng](https://discuss.elastic.co/u/Calvin_Feng)\
**Post date:** [October 17, 2022, 10:30pm UTC](https://discuss.elastic.co/t/combine-value-for-a-common-field-from-search-results-and-remove-one-of-the-result/316831/1 "2022-10-17T22:30:03Z")

</div>

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:

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

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

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 18, 2022, 12:33am UTC](https://discuss.elastic.co/t/combine-value-for-a-common-field-from-search-results-and-remove-one-of-the-result/316831/2 "2022-10-18T00:33:13Z")

</div>

Welcome to our community! 😃

Does the [`collapse`](https://www.elastic.co/guide/en/elasticsearch/reference/8.4/collapse-search-results.html) option help?

---

<div class="post-metadata">

**Author:** ![Calvin\_Feng](https://avatars.discourse-cdn.com/v4/letter/c/7feea3/32.png) [@Calvin\_Feng](https://discuss.elastic.co/u/Calvin_Feng)\
**Post date:** [October 18, 2022, 12:46am UTC](https://discuss.elastic.co/t/combine-value-for-a-common-field-from-search-results-and-remove-one-of-the-result/316831/3 "2022-10-18T00:46:49Z")

</div>

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

---

<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:** [November 15, 2022, 12:47am UTC](https://discuss.elastic.co/t/combine-value-for-a-common-field-from-search-results-and-remove-one-of-the-result/316831/4 "2022-11-15T00:47:38Z")

</div>

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