# Aggregation in another scope with dynamic value

**URL:** <https://discuss.elastic.co/t/aggregation-in-another-scope-with-dynamic-value/43442>\
**Category:** Elasticsearch\
**Created:** [March 3, 2016, 9:02pm UTC](https://discuss.elastic.co/t/aggregation-in-another-scope-with-dynamic-value/43442 "2016-03-03T21:02:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinpereira](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@vinpereira](https://discuss.elastic.co/u/vinpereira)\
**Post date:** [March 3, 2016, 9:02pm UTC](https://discuss.elastic.co/t/aggregation-in-another-scope-with-dynamic-value/43442/1 "2016-03-03T21:02:37Z")

</div>

Hi everyone,

I'm new to ES and I have a few questions. This is one of them.

First, a short version of the mapping with only what matters:

> <https://gist.github.com/vinpereira/ffd9b0351fb2b590e025>

I need to run a filter to return some documents. Each document has a nested type called `variables` and inside it I have a protocol number. I need to get this number and count where it appears in other documents.

I have two problems here:  
(1) Scope problem: The filter return me documents from type A. But I need to count in every type of documents. Please, do not confuse with the type from /index/type. Each document has a `processDefinition.key` that holds the type value like `"processDefinition.key": "A"`. How can I perform the aggregation in documents that are 'outside' the filter result?

(2) Dynamic value: To access the protocol value, I need to combine two field at `variables`: `"variables.name": "protocol"` and `"variables.rawValue": "<the dynamic value>"`. I need to pass these two values to the aggregation calculate the correct value for each document. How can I find the dynamic value in the filter and them use it in the aggregation?

By solving these two problems, I could run one filter with aggregation. My perfect world =)  
But solving only problem 2 then I could run one filter to find the 'values' and a separated aggregation passing the 'values' as a parameter.

Best regards and sorry for my poor English.

---

<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:** [July 5, 2017, 11:11pm UTC](https://discuss.elastic.co/t/aggregation-in-another-scope-with-dynamic-value/43442/2 "2017-07-05T23:11:27Z")

</div>


