# Add nested and sibling aggregation in data.search() in plugin

**URL:** <https://discuss.elastic.co/t/add-nested-and-sibling-aggregation-in-data-search-in-plugin/340763>\
**Category:** Kibana\
**Tags:** kibana-plugin-development\
**Created:** [August 14, 2023, 2:32pm UTC](https://discuss.elastic.co/t/add-nested-and-sibling-aggregation-in-data-search-in-plugin/340763 "2023-08-14T14:32:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)\
**Post date:** [August 14, 2023, 2:32pm UTC](https://discuss.elastic.co/t/add-nested-and-sibling-aggregation-in-data-search-in-plugin/340763/1 "2023-08-14T14:32:19Z")

</div>

Hi,

I am developing a **custom plugin** in Kibana using **React** , in **Kibana 8.8.1**.

I am using **search (low-level)** of _data plugin_ to query Elasticsearch.

This is the request body:

```auto
const request = {
      id: searchId,
      trackTotalHits: true,
      params: {
        index: indexPattern,
        body: {
          size: 0,
          aggs: data.search.aggs.createAggConfigs(dataView, aggregations).toDsl() 
        },
      },
    };

```

where **aggregations** is aggs in JSON format.

Eg: when I give 2 cardinality aggreagtions,

```auto
const aggregations = [
    {   
          id: 'students_count',
          type: 'cardinality',
          params: { field: 'student.id' },
    },
    {
          id: 'teachers_count',
          type: 'cardinality',
          params: { field: 'teacher.id' },
    },
]

```

For the above, ES treats it as sibling aggregation and I get the output as

```auto
aggs: {
    "students_count": { ... },
    "teachers_count": { ... }
}

```

  
  

But when I give a terms and cardinality aggregation together, it gets treated as nested aggregation.

```auto
const aggregations = [
    {   
          id: 'students_count',
          type: 'cardinality',
          params: { field: 'student.id' },
    },
    {
          id: 'students_gender',
          type: 'terms',
          params: { field: 'student.gender' },
    },
]

```

For the above, ES treats it as sibling aggregation and I get the output as

```auto
aggs: {
    "students_gender": {
         "terms": {...},
         "aggs": {
             "students_count": { ... }
         }
    },
}

```

Why does that happen? I haven't done any other change .

How can I add in nested and siling aggregation together?

Thanks

---

<div class="post-metadata">

**Author:** ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)\
**Post date:** [August 28, 2023, 12:48pm UTC](https://discuss.elastic.co/t/add-nested-and-sibling-aggregation-in-data-search-in-plugin/340763/2 "2023-08-28T12:48:20Z")

</div>

Hi,

Would be great if someone could give some solution for this.

Things get really tough and frustrating when developing plugin in Kibana, becoz there is very very limited resources, most of which are outdated or with incorrect url references.

---

<div class="post-metadata">

**Author:** ![Sheereen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sheereen/32/122005_2.png) [@Sheereen](https://discuss.elastic.co/u/Sheereen)\
**Post date:** [September 18, 2023, 5:45am UTC](https://discuss.elastic.co/t/add-nested-and-sibling-aggregation-in-data-search-in-plugin/340763/3 "2023-09-18T05:45:24Z")

</div>

Hi,

Would appreciate any inputs

---

<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:** [October 16, 2023, 5:46am UTC](https://discuss.elastic.co/t/add-nested-and-sibling-aggregation-in-data-search-in-plugin/340763/4 "2023-10-16T05:46:07Z")

</div>

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