# Merging buckets after top\_hits

**URL:** <https://discuss.elastic.co/t/merging-buckets-after-top-hits/356609>\
**Category:** Elasticsearch\
**Created:** [April 2, 2024, 11:29am UTC](https://discuss.elastic.co/t/merging-buckets-after-top-hits/356609 "2024-04-02T11:29:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vivek\_Burman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vivek_burman/32/118310_2.png) [@Vivek\_Burman](https://discuss.elastic.co/u/Vivek_Burman)\
**Post date:** [April 2, 2024, 11:29am UTC](https://discuss.elastic.co/t/merging-buckets-after-top-hits/356609/1 "2024-04-02T11:29:49Z")

</div>

Hi, I've a use case which I need help with. The mapping is as shown below of an elastic index:

```auto
{
   "test_case":{
      "type":"nested",
      "properties":{
         "end_timestamp":{
            "type":"date"
         },
         "pid":{
            "type":"integer"
         },
         "run_status":{
            "type":"keyword"
         },
         "start_timestamp":{
            "type":"date"
         }
      }
   },
   "test_info":{
      "properties":{
         "entity_type":{
            "type":"keyword"
         },
         "name":{
            "type":"keyword"
         },
         "pid":{
            "type":"integer"
         },
         "test_type":{
            "type":"keyword"
         }
      }
   }
}

```

Here each document can have same test\_case repeated. Meaning doc1: has 3 testcases and doc2: has 5 testcases. But 2 testcases are common in them [common in `pid`].

The question is: Get the latest unique testcases from each document and then group them by `test_info.test_type`. This would be like find the latest document for each testcase based on `test_case.start_timestamp` and then group them into a fresh set of buckets based on `test_info.test_type`.

I'm open to any possible answer.

---

<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:** [April 30, 2024, 11:30am UTC](https://discuss.elastic.co/t/merging-buckets-after-top-hits/356609/2 "2024-04-30T11:30:10Z")

</div>

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