# Accessing parent aggregation or inject aggregation into other aggregation

**URL:** <https://discuss.elastic.co/t/accessing-parent-aggregation-or-inject-aggregation-into-other-aggregation/163712>\
**Category:** Elasticsearch\
**Created:** [January 10, 2019, 10:56am UTC](https://discuss.elastic.co/t/accessing-parent-aggregation-or-inject-aggregation-into-other-aggregation/163712 "2019-01-10T10:56:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Zahn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marc_zahn/32/44302_2.png) [@Marc\_Zahn](https://discuss.elastic.co/u/Marc_Zahn)\
**Post date:** [January 10, 2019, 10:56am UTC](https://discuss.elastic.co/t/accessing-parent-aggregation-or-inject-aggregation-into-other-aggregation/163712/1 "2019-01-10T10:56:07Z")

</div>

Hi,

I am not sure if this is already solved but I could not find it.

In the index there docs (cards) which contain a question\_id (uuid) and a numeric rating.

I have a query which looks like this:

```
    {
    "size": 0,
   "query": {...}
    "aggs": {
    "standard_deviations": {
        "global": {},
        "aggs": {
            "group_by_question": {
                "aggs": {
                    "stats": {
                        "extended_stats": {"field": "answer.rating", "sigma": 1}
                    }
                }
            }
        }
    },
    "group_by_question": {
        "terms": {
            "field": "answer.question_id",
                "size": 10000
        },
        "aggs": {
            "filter_questions": {
                "bucket_selector": {
                    "buckets_path": {
                        "std_upper": "standard_deviations>stats.std_upper",
                            "question_avg": "avg_rating"
                    },
                    "script": "params.question_avg < params.std_upper"
                }
            },
            "avg_rating" : {
                "avg" : { "field" : "answer.rating" }
            }
        }
    }
}
}

```

The usecase: I want to filter a filtered set of cards ("group\_by\_question") which are above or below the standard deviation of all _unfiltered_ cards (see global under "standard\_deviations").

What I need now are the stats from "standard\_deviations" within "bucket\_selector" . On a file system the buckets path would look like

```
../../standard_deviations/group_by_question/<...somehow select the bucket item with the correct question id...>.std_upper

```

I came to the conclusion that it is only possible to access buckets on the same or a lower level.

Or maybe there is an even better way to that at all. I could image getting the standard deviations in a dedicated query and use them here statically.

Hopefully someone can help me.

---

<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:** [February 7, 2019, 10:56am UTC](https://discuss.elastic.co/t/accessing-parent-aggregation-or-inject-aggregation-into-other-aggregation/163712/2 "2019-02-07T10:56:22Z")

</div>

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