# Statistical Facet - Does it only aggregate distinct numerical values

**URL:** <https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485>\
**Category:** Elasticsearch\
**Created:** [June 19, 2013, 10:26pm UTC](https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485 "2013-06-19T22:26:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Butterfield](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@Tim\_Butterfield](https://discuss.elastic.co/u/Tim_Butterfield)\
**Post date:** [June 19, 2013, 10:26pm UTC](https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485/1 "2013-06-19T22:26:39Z")

</div>

So I have an index containing a 82 documents like the following (sample  
data to familiarise me with the api).

{"\_index":"foo","\_type":"bar","\_id":"3675293","\_score":1.0, "\_source" : {  
"companyId": 3675293,  
"companyName": "ABC",  
"companyOwner": {  
"ownerFirstName": "Billy",  
"ownerLastName": "Jean"  
},  
"created": "2013-05-22T13:45:49",  
"accounts": [  
{  
"year": 2013,  
"amount": 1.0,  
"currency": "GBP"  
},  
{  
"year": 2012,  
"amount": 1.0,  
"currency": "GBP"  
}  
]  
}}

The problem I have is that I'm trying to total all of the amounts. When I  
use a Statistical Facet via the NEST .Net client I get an answer I wouldn't  
expect.

82 documents, each with a collection containing 2 amount of £1 in my mind  
aggregates to a total of 2 X 82 = 164. However, the facet returns a total  
of 82. When I change one of the amounts in each of the two documents to be  
2 instead of 1, I get the correct result.

Does Elastic Search only aggregate unique numerical values per document?  
Can this be overriden?

The above isn't the real world example, but the collection will contain a  
monetary field which I need to aggregate for all matching documents, and  
the amounts in the collection for one or more items may match. I need to  
aggreate everything, not just distinct values. Is this possible?

Incidentally I'm using the NEST .Net client if that has any bearing.

Thanks

Regards

Tim

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Boaz\_Leskes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boaz_leskes/32/723_2.png) [@Boaz\_Leskes](https://discuss.elastic.co/u/Boaz_Leskes)\
**Post date:** [June 20, 2013, 7:48am UTC](https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485/2 "2013-06-20T07:48:08Z")

</div>

Hi Tim,

By default ES "collapses" all inner document when indexing them. It creates  
indexes which are "namespaced" according to your document structure. For  
example, for this document you will have an index which is called  
"accounts.currency" with the value "GBP" for this document. This is done  
because it results in the best search & indexing performance but it does  
have the downside of loosing internal arrayed objects, which causes only  
distinct values to be stored.

Normally, this is solved by indicating that the accounts field should have  
the type "nested"  
(see [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type/) ).  
Sadly, I just discovered a problem with nested types and the statistical  
facet. I opened an issue here:  
[Statistical facet ignores nested documents · Issue #3209 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/3209) so it will be  
fixed.

Cheers,  
Boaz

On Thursday, June 20, 2013 12:26:39 AM UTC+2, Tim Butterfield wrote:

> So I have an index containing a 82 documents like the following (sample  
> data to familiarise me with the api).
> 
> {"\_index":"foo","\_type":"bar","\_id":"3675293","\_score":1.0, "\_source" : {  
> "companyId": 3675293,  
> "companyName": "ABC",  
> "companyOwner": {  
> "ownerFirstName": "Billy",  
> "ownerLastName": "Jean"  
> },  
> "created": "2013-05-22T13:45:49",  
> "accounts": [  
> {  
> "year": 2013,  
> "amount": 1.0,  
> "currency": "GBP"  
> },  
> {  
> "year": 2012,  
> "amount": 1.0,  
> "currency": "GBP"  
> }  
> ]  
> }}
> 
> The problem I have is that I'm trying to total all of the amounts. When I  
> use a Statistical Facet via the NEST .Net client I get an answer I wouldn't  
> expect.
> 
> 82 documents, each with a collection containing 2 amount of £1 in my mind  
> aggregates to a total of 2 X 82 = 164. However, the facet returns a total  
> of 82. When I change one of the amounts in each of the two documents to be  
> 2 instead of 1, I get the correct result.
> 
> Does Elastic Search only aggregate unique numerical values per document?  
> Can this be overriden?
> 
> The above isn't the real world example, but the collection will contain a  
> monetary field which I need to aggregate for all matching documents, and  
> the amounts in the collection for one or more items may match. I need to  
> aggreate everything, not just distinct values. Is this possible?
> 
> Incidentally I'm using the NEST .Net client if that has any bearing.
> 
> Thanks
> 
> Regards
> 
> Tim

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Boaz\_Leskes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boaz_leskes/32/723_2.png) [@Boaz\_Leskes](https://discuss.elastic.co/u/Boaz_Leskes)\
**Post date:** [June 21, 2013, 11:36am UTC](https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485/3 "2013-06-21T11:36:46Z")

</div>

Hi Tim,

A colleague pointed out that the source of the issue is I forgot to  
indicate that the facet should run on nested documents.

To re-iterated : you should mark "accounts" as a nested object in your  
mappings, then you can facet like this:

curl -XPOST "[http://localhost:9200/bar/\_search](http://localhost:9200/bar/_search)" -d'  
{  
"facets": {  
"test": {  
"statistical": {  
"field": "amount"  
},  
"nested": "accounts"  
}  
},  
"size":0  
}'

More info about faceting and nested objects is to be found here:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/) (search for  
nested) .

Cheers,  
Boaz

On Thursday, June 20, 2013 9:48:08 AM UTC+2, Boaz Leskes wrote:

> Hi Tim,
> 
> By default ES "collapses" all inner document when indexing them. It  
> creates indexes which are "namespaced" according to your document  
> structure. For example, for this document you will have an index which is  
> called "accounts.currency" with the value "GBP" for this document. This is  
> done because it results in the best search & indexing performance but it  
> does have the downside of loosing internal arrayed objects, which causes  
> only distinct values to be stored.
> 
> Normally, this is solved by indicating that the accounts field should have  
> the type "nested" (see  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type/) ).  
> Sadly, I just discovered a problem with nested types and the statistical  
> facet. I opened an issue here:  
> [Statistical facet ignores nested documents · Issue #3209 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/3209) so it will be  
> fixed.
> 
> Cheers,  
> Boaz
> 
> On Thursday, June 20, 2013 12:26:39 AM UTC+2, Tim Butterfield wrote:
> 
> > So I have an index containing a 82 documents like the following (sample  
> > data to familiarise me with the api).
> > 
> > {"\_index":"foo","\_type":"bar","\_id":"3675293","\_score":1.0, "\_source" : {  
> > "companyId": 3675293,  
> > "companyName": "ABC",  
> > "companyOwner": {  
> > "ownerFirstName": "Billy",  
> > "ownerLastName": "Jean"  
> > },  
> > "created": "2013-05-22T13:45:49",  
> > "accounts": [  
> > {  
> > "year": 2013,  
> > "amount": 1.0,  
> > "currency": "GBP"  
> > },  
> > {  
> > "year": 2012,  
> > "amount": 1.0,  
> > "currency": "GBP"  
> > }  
> > ]  
> > }}
> > 
> > The problem I have is that I'm trying to total all of the amounts. When I  
> > use a Statistical Facet via the NEST .Net client I get an answer I wouldn't  
> > expect.
> > 
> > 82 documents, each with a collection containing 2 amount of £1 in my mind  
> > aggregates to a total of 2 X 82 = 164. However, the facet returns a total  
> > of 82. When I change one of the amounts in each of the two documents to be  
> > 2 instead of 1, I get the correct result.
> > 
> > Does Elastic Search only aggregate unique numerical values per document?  
> > Can this be overriden?
> > 
> > The above isn't the real world example, but the collection will contain a  
> > monetary field which I need to aggregate for all matching documents, and  
> > the amounts in the collection for one or more items may match. I need to  
> > aggreate everything, not just distinct values. Is this possible?
> > 
> > Incidentally I'm using the NEST .Net client if that has any bearing.
> > 
> > Thanks
> > 
> > Regards
> > 
> > Tim

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jimmy\_Thomas](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jimmy\_Thomas](https://discuss.elastic.co/u/Jimmy_Thomas)\
**Post date:** [October 1, 2013, 3:15pm UTC](https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485/4 "2013-10-01T15:15:11Z")

</div>

Is it possible to filter the nested documents used in the statistical  
counts ?

In this case for example, how could I get statistics only for the accounts  
matching year 2013 ?

I tried setting a filter as a facet\_filter but it's only filtering root  
documents, not used nested docs.

Thanks  
Regards  
Jimmy

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:14am UTC](https://discuss.elastic.co/t/statistical-facet-does-it-only-aggregate-distinct-numerical-values/12485/5 "2017-07-06T02:14:05Z")

</div>


