# Facet count on nested documents

**URL:** https://discuss.elastic.co/t/facet-count-on-nested-documents/8101
**Category:** Elasticsearch
**Created:** [June 14, 2012, 9:43am UTC](https://discuss.elastic.co/t/facet-count-on-nested-documents/8101 "2012-06-14T09:43:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pierre\_Durand](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre_durand/32/1895_2.png) [@Pierre\_Durand](https://discuss.elastic.co/u/Pierre_Durand)
#### Post date: [June 14, 2012, 9:43am UTC](https://discuss.elastic.co/t/facet-count-on-nested-documents/8101/1 "2012-06-14T09:43:28Z")

</div>

Hi!

I have a problem with facets on nested documents.

Here is my code (PHP):  
[https://gist.github.com/2929265](https://gist.github.com/2929265)  
The populate() method creates the mapping and indexes documents.  
The search method returns results.

My mapping:  
[https://gist.github.com/2929280](https://gist.github.com/2929280)  
My PHP code creates 5000 players and 100-200 balls per player.

My query:  
[https://gist.github.com/2929285](https://gist.github.com/2929285)  
The "and" filter is not really useful, but it allows to combine filters on  
the root documents and the nested documents.  
Currently, filter are only applied on nested documents.  
There are 3 facets: 1 on the root documents, 2 on the nested documents.

Result:  
[https://gist.github.com/2929312](https://gist.github.com/2929312)  
The results and the first facet are ok, but the others facets aren't.  
The "count" values are not what i expected: too high.  
It counts the number of matching nested documents, instead of the number of  
documents with matching nested documents.

Do you have a idea to solve my problem?

I found a solution, but it causes really bad performances...

I changed my mapping, and added "include\_in\_root" : true on nested documents  
New mapping: [https://gist.github.com/2929319](https://gist.github.com/2929319)  
I also changed facets, the "nested" option is not  
required: [https://gist.github.com/2929321](https://gist.github.com/2929321)

Result:  
[https://gist.github.com/2929324](https://gist.github.com/2929324)  
Facets counts are now ok,  
but the execution time is awful: 8559ms!!!!!  
And it takes a lot of memory.

The performance problem is bound to the "geo\_distance" filter.  
If I disable it, the execution time is ok.  
But of course, this filter is essential for me 😛

I tried to enable the cache on the "geo\_distance" filter, but it changes  
nothing ☹

Can you help me?

---

<div class="post-metadata">

### Author: ![Pierre\_Durand](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pierre_durand/32/1895_2.png) [@Pierre\_Durand](https://discuss.elastic.co/u/Pierre_Durand)
#### Post date: [June 19, 2012, 10:04am UTC](https://discuss.elastic.co/t/facet-count-on-nested-documents/8101/2 "2012-06-19T10:04:47Z")

</div>

Up? 😃

On Thursday, June 14, 2012 11:43:28 AM UTC+2, Pierre Durand wrote:

> Hi!
> 
> I have a problem with facets on nested documents.
> 
> Here is my code (PHP):  
> [https://gist.github.com/2929265](https://gist.github.com/2929265)  
> The populate() method creates the mapping and indexes documents.  
> The search method returns results.
> 
> My mapping:  
> [https://gist.github.com/2929280](https://gist.github.com/2929280)  
> My PHP code creates 5000 players and 100-200 balls per player.
> 
> My query:  
> [https://gist.github.com/2929285](https://gist.github.com/2929285)  
> The "and" filter is not really useful, but it allows to combine filters on  
> the root documents and the nested documents.  
> Currently, filter are only applied on nested documents.  
> There are 3 facets: 1 on the root documents, 2 on the nested documents.
> 
> Result:  
> [https://gist.github.com/2929312](https://gist.github.com/2929312)  
> The results and the first facet are ok, but the others facets aren't.  
> The "count" values are not what i expected: too high.  
> It counts the number of matching nested documents, instead of the number  
> of documents with matching nested documents.
> 
> Do you have a idea to solve my problem?
> 
> I found a solution, but it causes really bad performances...
> 
> I changed my mapping, and added "include\_in\_root" : true on nested  
> documents  
> New mapping: [https://gist.github.com/2929319](https://gist.github.com/2929319)  
> I also changed facets, the "nested" option is not required:  
> [https://gist.github.com/2929321](https://gist.github.com/2929321)
> 
> Result:  
> [https://gist.github.com/2929324](https://gist.github.com/2929324)  
> Facets counts are now ok,  
> but the execution time is awful: 8559ms!!!!!  
> And it takes a lot of memory.
> 
> The performance problem is bound to the "geo\_distance" filter.  
> If I disable it, the execution time is ok.  
> But of course, this filter is essential for me 😛
> 
> I tried to enable the cache on the "geo\_distance" filter, but it changes  
> nothing ☹
> 
> Can you help me?

---

<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:23am UTC](https://discuss.elastic.co/t/facet-count-on-nested-documents/8101/3 "2017-07-06T03:23:26Z")

</div>


