Aggregation in another scope with dynamic value

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:

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: "": "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.