# Elasticsearch facet count problem in subdocuments

**URL:** https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556
**Category:** Elasticsearch
**Created:** [November 5, 2012, 8:11am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556 "2012-11-05T08:11:19Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)
#### Post date: [November 5, 2012, 8:11am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/1 "2012-11-05T08:11:19Z")

</div>

Hi,

I have a faceted search for sub document but it returns wrong count. I have  
2 match subdocuments but it counts as 1. You can see an example gist here  
[https://gist.github.com/4015950](https://gist.github.com/4015950) .

Thanks in advance

--

---

<div class="post-metadata">

### Author: ![Tanguy1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy1/32/1749_2.png) [@Tanguy1](https://discuss.elastic.co/u/Tanguy1)
#### Post date: [November 5, 2012, 8:32am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/2 "2012-11-05T08:32:19Z")

</div>

Facets agreggates data based on search results. Counts are provided at  
document level (here is my/terms). In your sample, 1 document among all  
the results has the field translation.lang with the value "tr".

If you were expecting a count value of 2, you should split your document  
into parent (terms)/child (translations) documents and running faceted  
search on child documents.

-- Tanguy

Le lundi 5 novembre 2012 09:11:19 UTC+1, Hüseyin BABAL a écrit :

> Hi,
> 
> I have a faceted search for sub document but it returns wrong count. I  
> have 2 match subdocuments but it counts as 1. You can see an example gist  
> here [Elasticsearch subdocument facet count · GitHub](https://gist.github.com/4015950) .
> 
> Thanks in advance

--

---

<div class="post-metadata">

### Author: ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)
#### Post date: [November 5, 2012, 12:58pm UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/3 "2012-11-05T12:58:40Z")

</div>

I have used nested document and it has dangerous to change nested to  
parent&child in my application. I put nested documents directly to the page  
view. It would be better if facet search on nested documents is available,  
i will try to find another way to solve my problem.

Thanks

On Mon, Nov 5, 2012 at 10:32 AM, Tanguy [tlrx.dev@gmail.com](mailto:tlrx.dev@gmail.com) wrote:

> Facets agreggates data based on search results. Counts are provided at  
> document level (here is my/terms). In your sample, 1 document among all  
> the results has the field translation.lang with the value "tr".
> 
> If you were expecting a count value of 2, you should split your document  
> into parent (terms)/child (translations) documents and running faceted  
> search on child documents.
> 
> -- Tanguy
> 
> Le lundi 5 novembre 2012 09:11:19 UTC+1, Hüseyin BABAL a écrit :
> 
> > Hi,
> > 
> > I have a faceted search for sub document but it returns wrong count. I  
> > have 2 match subdocuments but it counts as 1. You can see an example gist  
> > here [https://gist.github.com/\*\*4015950](https://gist.github.com/**4015950) [https://gist.github.com/4015950](https://gist.github.com/4015950)  
> > .
> > 
> > Thanks in advance
> 
> --

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [November 5, 2012, 9:35pm UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/4 "2012-11-05T21:35:39Z")

</div>

Facet search on nested documents  
is available [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)

On Monday, November 5, 2012 7:58:46 AM UTC-5, Hüseyin BABAL wrote:

> I have used nested document and it has dangerous to change nested to  
> parent&child in my application. I put nested documents directly to the page  
> view. It would be better if facet search on nested documents is available,  
> i will try to find another way to solve my problem.
> 
> Thanks
> 
> On Mon, Nov 5, 2012 at 10:32 AM, Tanguy \<[tlrx...@gmail.com](mailto:tlrx...@gmail.com) \<javascript:\>\>wrote:
> 
> > Facets agreggates data based on search results. Counts are provided at  
> > document level (here is my/terms). In your sample, 1 document among all  
> > the results has the field translation.lang with the value "tr".
> > 
> > If you were expecting a count value of 2, you should split your document  
> > into parent (terms)/child (translations) documents and running faceted  
> > search on child documents.
> > 
> > -- Tanguy
> > 
> > Le lundi 5 novembre 2012 09:11:19 UTC+1, Hüseyin BABAL a écrit :
> > 
> > > Hi,
> > > 
> > > I have a faceted search for sub document but it returns wrong count. I  
> > > have 2 match subdocuments but it counts as 1. You can see an example gist  
> > > here [https://gist.github.com/\*\*4015950](https://gist.github.com/**4015950) [https://gist.github.com/4015950](https://gist.github.com/4015950)  
> > > .
> > > 
> > > Thanks in advance
> > 
> > --

--

---

<div class="post-metadata">

### Author: ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)
#### Post date: [November 6, 2012, 6:35am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/5 "2012-11-06T06:35:45Z")

</div>

Thanks Igor, i have already saw that documentation but couldn't manage to  
construct query according to that document. I want to make facet search on  
nested documents' one field called "lang" but in docs it is like;

"facets": {  
"facet1": {  
"terms\_stats": {  
"key\_field": "obj1.name",  
"value\_field": "obj1.count"  
},  
"scope": "my\_scope"  
}  
}

my current query is like this here [Elasticsearch subdocument facet count · GitHub](https://gist.github.com/4015950)  
.However it returns per document facet count not nested documents count.

On Mon, Nov 5, 2012 at 11:35 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> Facet search on nested documents is available  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> 
> On Monday, November 5, 2012 7:58:46 AM UTC-5, Hüseyin BABAL wrote:
> 
> > I have used nested document and it has dangerous to change nested to  
> > parent&child in my application. I put nested documents directly to the page  
> > view. It would be better if facet search on nested documents is available,  
> > i will try to find another way to solve my problem.
> > 
> > Thanks
> > 
> > On Mon, Nov 5, 2012 at 10:32 AM, Tanguy [tlrx...@gmail.com](mailto:tlrx...@gmail.com) wrote:
> > 
> > > Facets agreggates data based on search results. Counts are provided at  
> > > document level (here is my/terms). In your sample, 1 document among all  
> > > the results has the field translation.lang with the value "tr".
> > > 
> > > If you were expecting a count value of 2, you should split your document  
> > > into parent (terms)/child (translations) documents and running faceted  
> > > search on child documents.
> > > 
> > > -- Tanguy
> > > 
> > > Le lundi 5 novembre 2012 09:11:19 UTC+1, Hüseyin BABAL a écrit :
> > > 
> > > > Hi,
> > > > 
> > > > I have a faceted search for sub document but it returns wrong count. I  
> > > > have 2 match subdocuments but it counts as 1. You can see an example gist  
> > > > here [https://gist.github.com/\*\*4\*\*015950](https://gist.github.com/ **4** 015950)[https://gist.github.com/4015950](https://gist.github.com/4015950)  
> > > > .
> > > > 
> > > > Thanks in advance
> > > 
> > > --
> > 
> > --

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [November 6, 2012, 11:13am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/6 "2012-11-06T11:13:55Z")

</div>

That facet runs on the parent documents and the fact that it works, might  
indicate one of two things: your translation documents are not mapped as  
nested or you have include\_in\_parent flag turned on. The nested facet  
should look like this:

curl -XPOST [http://localhost:9200/my/terms/\_search?pretty=true](http://localhost:9200/my/terms/_search?pretty=true) -d '  
{  
"query" : { "wildcard" : {"term" : "fire"} },  
"facets" : {  
"langs" : {  
"terms" : {"field" : "translations.lang"},  
"nested": "translations"  
}  
}  
}  
'

On Tuesday, November 6, 2012 1:35:51 AM UTC-5, Hüseyin BABAL wrote:

> Thanks Igor, i have already saw that documentation but couldn't manage to  
> construct query according to that document. I want to make facet search on  
> nested documents' one field called "lang" but in docs it is like;
> 
> "facets": {  
> "facet1": {  
> "terms\_stats": {  
> "key\_field": "obj1.name",  
> "value\_field": "obj1.count"  
> },  
> "scope": "my\_scope"  
> }  
> }
> 
> my current query is like this here [Elasticsearch subdocument facet count · GitHub](https://gist.github.com/4015950)  
> .However it returns per document facet count not nested documents count.
> 
> On Mon, Nov 5, 2012 at 11:35 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Facet search on nested documents is available  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > 
> > On Monday, November 5, 2012 7:58:46 AM UTC-5, Hüseyin BABAL wrote:
> > 
> > > I have used nested document and it has dangerous to change nested to  
> > > parent&child in my application. I put nested documents directly to the page  
> > > view. It would be better if facet search on nested documents is available,  
> > > i will try to find another way to solve my problem.
> > > 
> > > Thanks
> > > 
> > > On Mon, Nov 5, 2012 at 10:32 AM, Tanguy [tlrx...@gmail.com](mailto:tlrx...@gmail.com) wrote:
> > > 
> > > > Facets agreggates data based on search results. Counts are provided at  
> > > > document level (here is my/terms). In your sample, 1 document among all  
> > > > the results has the field translation.lang with the value "tr".
> > > > 
> > > > If you were expecting a count value of 2, you should split your  
> > > > document into parent (terms)/child (translations) documents and running  
> > > > faceted search on child documents.
> > > > 
> > > > -- Tanguy
> > > > 
> > > > Le lundi 5 novembre 2012 09:11:19 UTC+1, Hüseyin BABAL a écrit :
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I have a faceted search for sub document but it returns wrong count. I  
> > > > > have 2 match subdocuments but it counts as 1. You can see an example gist  
> > > > > here [https://gist.github.com/\*\*4\*\*015950](https://gist.github.com/ **4** 015950)[https://gist.github.com/4015950](https://gist.github.com/4015950)  
> > > > > .
> > > > > 
> > > > > Thanks in advance
> > > > 
> > > > --
> > > 
> > > --

--

---

<div class="post-metadata">

### Author: ![cubuzoa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cubuzoa/32/2648_2.png) [@cubuzoa](https://discuss.elastic.co/u/cubuzoa)
#### Post date: [November 7, 2012, 11:05am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/7 "2012-11-07T11:05:32Z")

</div>

Hi Igor,

Currently my document is nested but i haven't mapped as nested type  
explicitly, i got error when i used your query above. However, i have  
configured the mapping for nested type and it works.

Thank you

On Tue, Nov 6, 2012 at 1:13 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> That facet runs on the parent documents and the fact that it works, might  
> indicate one of two things: your translation documents are not mapped as  
> nested or you have include\_in\_parent flag turned on. The nested facet should  
> look like this:
> 
> curl -XPOST [http://localhost:9200/my/terms/\_search?pretty=true](http://localhost:9200/my/terms/_search?pretty=true) -d '  
> {  
> "query" : { "wildcard" : {"term" : "fire"} },  
> "facets" : {  
> "langs" : {  
> "terms" : {"field" : "translations.lang"},  
> "nested": "translations"  
> }  
> }  
> }  
> '
> 
> On Tuesday, November 6, 2012 1:35:51 AM UTC-5, Hüseyin BABAL wrote:
> 
> > Thanks Igor, i have already saw that documentation but couldn't manage to  
> > construct query according to that document. I want to make facet search on  
> > nested documents' one field called "lang" but in docs it is like;
> > 
> > "facets": {  
> > "facet1": {  
> > "terms\_stats": {  
> > "key\_field": "obj1.name",  
> > "value\_field": "obj1.count"  
> > },  
> > "scope": "my\_scope"  
> > }  
> > }
> > 
> > my current query is like this here [Elasticsearch subdocument facet count · GitHub](https://gist.github.com/4015950)  
> > .However it returns per document facet count not nested documents count.
> > 
> > On Mon, Nov 5, 2012 at 11:35 PM, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > 
> > > Facet search on nested documents is available  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/index.html)
> > > 
> > > On Monday, November 5, 2012 7:58:46 AM UTC-5, Hüseyin BABAL wrote:
> > > 
> > > > I have used nested document and it has dangerous to change nested to  
> > > > parent&child in my application. I put nested documents directly to the page  
> > > > view. It would be better if facet search on nested documents is available, i  
> > > > will try to find another way to solve my problem.
> > > > 
> > > > Thanks
> > > > 
> > > > On Mon, Nov 5, 2012 at 10:32 AM, Tanguy [tlrx...@gmail.com](mailto:tlrx...@gmail.com) wrote:
> > > > 
> > > > > Facets agreggates data based on search results. Counts are provided at  
> > > > > document level (here is my/terms). In your sample, 1 document among all the  
> > > > > results has the field translation.lang with the value "tr".
> > > > > 
> > > > > If you were expecting a count value of 2, you should split your  
> > > > > document into parent (terms)/child (translations) documents and running  
> > > > > faceted search on child documents.
> > > > > 
> > > > > -- Tanguy
> > > > > 
> > > > > Le lundi 5 novembre 2012 09:11:19 UTC+1, Hüseyin BABAL a écrit :
> > > > > 
> > > > > > Hi,
> > > > > > 
> > > > > > I have a faceted search for sub document but it returns wrong count. I  
> > > > > > have 2 match subdocuments but it counts as 1. You can see an example gist  
> > > > > > here [Elasticsearch subdocument facet count · GitHub](https://gist.github.com/4015950) .
> > > > > > 
> > > > > > Thanks in advance
> > > > > 
> > > > > --
> > > 
> > > --
> 
> --

--

---

<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:05am UTC](https://discuss.elastic.co/t/elasticsearch-facet-count-problem-in-subdocuments/9556/8 "2017-07-06T03:05:48Z")

</div>


