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