I suddenly remembered when using facets that I had to apply the same query
filter as a facet filter with the join option disabled. Turns out it is
somewhat identical with aggregations. My problem was that the scope of my
nested aggregation with not under the scope of the filter aggregation. I
hope #3022 and related issues can bring about less ambiguous aggregations.
Nested aggregations on pre-filtered nested documents should work as is. If
not, the global scope aggregation should be used.

-- 

Ivan


On Mon, Nov 10, 2014 at 3:43 PM, Ivan Brusic <i...@brusic.com> wrote:

> Reproducible gist: https://gist.github.com/brusic/81e1552ffd49a1f6a7aa
>
> Surely I cannot be the only one to have encountered this issue.
>
> --
> Ivan
>
> On Mon, Nov 10, 2014 at 12:53 PM, Ivan Brusic <i...@brusic.com> wrote:
>
>> Is it possible to aggregate only on the nested documents that are
>> returned by a (filtered) query? For what I can tell when using a nested
>> aggregation, it will function on all nested documents of the parent
>> documents whose nested document satisfy a nested query/filter. Did that
>> make sense? :) Is this the same limitation as issue #3022? I know that
>> number by heart by now.
>>
>> For example, I have 3 simple documents, where the nstd object is defined
>> as nested:
>>
>> {
>>   "name" : "foo",
>>   "nstd" : [
>>     {
>>         "ID" : 1
>>     }
>>   ]
>> }
>> '
>>
>> {
>>   "name" : "bar",
>>   "nstd" : [
>>     {
>>         "ID" : 2
>>     }
>>   ]
>> }
>> '
>>
>> {
>>   "name" : "baz",
>>   "nstd" : [
>>     {
>>         "ID" : 1
>>     },
>>     {
>>         "ID" : 2
>>     }
>>   ]
>> }
>> '
>>
>> I then execute a simple nested query:
>>
>>    "query": {
>>       "filtered": {
>>          "query": {
>>             "match_all": {}
>>          },
>>          "filter": {
>>             "nested": {
>>                "path": "nstd",
>>                "filter": {
>>                   "term": {
>>                      "nstd.ID": 1
>>                   }
>>                }
>>             }
>>          }
>>       }
>>    }
>>
>> If I aggregate on the nstd.ID field, I will always get back results for
>> nested documents that were excluded by the filter:
>>
>>             "buckets": [
>>                {
>>                   "key": 1,
>>                   "doc_count": 2
>>                },
>>                {
>>                   "key": 2,
>>                   "doc_count": 1
>>                }
>>             ]
>>
>> Since the ID:2 field does not match the filter, it should not be returned
>> with the aggregation. I have tried using a filter aggregation with the same
>> filter used in the filtered query, but I receive the same results.
>>
>> Cheers,
>>
>> Ivan
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elasticsearch+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCqV%2BVtExWQ%2B15V6ywZ6pMog91ajB%3DEyN81ueditLGZKg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to