# Incorrect facet counts when using parent/child documents

**URL:** <https://discuss.elastic.co/t/incorrect-facet-counts-when-using-parent-child-documents/6068>\
**Category:** Elasticsearch\
**Created:** [December 5, 2011, 12:56pm UTC](https://discuss.elastic.co/t/incorrect-facet-counts-when-using-parent-child-documents/6068 "2011-12-05T12:56:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Birbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_birbeck/32/3054_2.png) [@Mark\_Birbeck](https://discuss.elastic.co/u/Mark_Birbeck)\
**Post date:** [December 5, 2011, 12:56pm UTC](https://discuss.elastic.co/t/incorrect-facet-counts-when-using-parent-child-documents/6068/1 "2011-12-05T12:56:04Z")

</div>

Hi there,

I'm trying to use a date histogram facet to count the number of activities  
that have happened to particular objects between two dates. It works fine  
if the actions are stored as nested documents, but the problem with that  
technique is that inserting new actions is very slow. However, whilst  
storing the documents as distinct documents and making use of \_parent makes  
it much easier to manage the data, the facet values returned from queries  
are incorrect.

The following script illustrates the problem:

[https://gist.github.com/1433393](https://gist.github.com/1433393)

The main search is for articles that have both 'One' in the title _and_  
have actions that took place between two dates. This correctly returns one  
article, which in turn has one action. However the facet values are  
calculated across all actions for all articles, like this:

{  
"facets":{  
"created\_facet":{  
"\_type":"date\_histogram",  
"entries":[

{"time":1321574400000,"count":2,"min":1.0,"max":1.0,"total":2.0,"total\_count":2,"mean":1.0},

{"time":1322006400000,"count":1,"min":0.0,"max":0.0,"total":0.0,"total\_count":1,"mean":0.0}  
]  
},  
"published\_facet":{  
"\_type":"date\_histogram",  
"entries":[

{"time":1321574400000,"count":2,"min":0.0,"max":0.0,"total":0.0,"total\_count":2,"mean":0.0},

{"time":1322006400000,"count":1,"min":1.0,"max":1.0,"total":1.0,"total\_count":1,"mean":1.0}  
]  
}  
}  
}

As you can see it's working as if there are three actions -- which there  
are across all of the articles.

According to the documentation, this is not the correct behaviour for  
facets applied against a query, but it _is_ the correct behaviour for  
facets applied against a filter. My guess is that since, according to the  
documentation, the 'has\_child' query is simply a wrapper around the  
'has\_child' filter, then I'm experiencing the filter behaviour whether I  
want it or not.

Is this something that could be changed in ES, especially since I think my  
data layout is probably a fairly common pattern? I'm tempted to raise

Or can anyone think of a different way to structure the query to get what I  
want? For example, a facet filter might appear to provide a solution, but  
the query expressed in the facet filter seems to apply to the 'scope'  
specified in the facet, which in my case is the child document; is there  
any way around that?

Regards,

Mark Birbeck

---

<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 6, 2017, 3:46am UTC](https://discuss.elastic.co/t/incorrect-facet-counts-when-using-parent-child-documents/6068/2 "2017-07-06T03:46:25Z")

</div>

Closed on July 6, 2017, 3:46am UTC


