ad_views (time, user_id, gender, age, country and similar fields). ad_views document type has ads as parent.

The wanted end result is search form where user can find ads by choosing ad_views parameters. For example, find all ads who have been viewed by male, aged 18-25, living in UK. I also want to display "faceted search" form (like in amazon.com and many other sites) where you can see number of items (ads) under each filter/search option.

The searching (getting filtered list of docs) part is clear, but not the aggregations. I'm trying to figure out how to use aggregations on the child document properties, but instead of getting child document counts I would like to get counts for ads documents.

The search will return me three ads. And in aggregations I will get male=5 and female=1 (that is - it will return total term counts in sub documents), but what I want is to get ads counts, that is, male=3, female=1 (there are three ads documents who have one or more male ad view, and one ads document who have one or more female ad view).

If the number of values in child documents is limited, you could use filter aggregation with has_child filter as a workaround. In your particular case, two filter aggregations, one for male another for female.

Thanks, Masaru, will try your proposed workaround on limited value fields.
Any ideas on workaround on not-so-limited value fields? For example, if there would be a "city" field with potentially hundreds of values?

I'm looking into nested / reverse nested aggregations (I think it will not be a big problem for me to switch to nested docs instead of parents/childs), but not sure exactly if these aggregations will do what I want. Having a bit hard time to grasp the concepts.

Now I'm trying to count ads per gender, first by filtering on nested docs(title and date of birth fields).

Added reverse_nested to get this doc count. The problem is that "gender_count" aggregation on the nested documents "ad_views.account.gender" returns me TOTAL number of the documents who has a ad_views with this gender. It ignores date of birth in the nested documents.