# Facet: define and use a function instead of the traditional count?

**URL:** <https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947>\
**Category:** Elasticsearch\
**Created:** [October 14, 2013, 9:16pm UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947 "2013-10-14T21:16:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![didier\_baquier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didier_baquier/32/2055_2.png) [@didier\_baquier](https://discuss.elastic.co/u/didier_baquier)\
**Post date:** [October 14, 2013, 9:16pm UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947/1 "2013-10-14T21:16:20Z")

</div>

Hi folks,

I am wondering if it is possible, for any kind of facets, to define and use  
a function instead of the count ?

Quick example:

I have a term facet on a field which can be Paid or Unpaid, and I got the  
number of hits.

term: Paid  
count: 12

term: Unpaid  
count: 10

I also have another field "amount", and I am wondering if I can define a  
function to say : for each match, add the amount to the sum.

term: Paid  
count: 120 (for an amount of 10)

term: Unpaid  
count: 100

Thanks for your help

--  
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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [October 14, 2013, 9:23pm UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947/2 "2013-10-14T21:23:16Z")

</div>

Look into using the value\_script with the terms stats facet.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--  
Ivan

On Mon, Oct 14, 2013 at 2:16 PM, [didier.baquier@gmail.com](mailto:didier.baquier@gmail.com) wrote:

> Hi folks,
> 
> I am wondering if it is possible, for any kind of facets, to define and  
> use a function instead of the count ?
> 
> Quick example:
> 
> I have a term facet on a field which can be Paid or Unpaid, and I got the  
> number of hits.
> 
> term: Paid  
> count: 12
> 
> term: Unpaid  
> count: 10
> 
> I also have another field "amount", and I am wondering if I can define a  
> function to say : for each match, add the amount to the sum.
> 
> term: Paid  
> count: 120 (for an amount of 10)
> 
> term: Unpaid  
> count: 100
> 
> Thanks for your help
> 
> --  
> 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).

--  
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:** ![didier\_baquier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didier_baquier/32/2055_2.png) [@didier\_baquier](https://discuss.elastic.co/u/didier_baquier)\
**Post date:** [October 14, 2013, 11:19pm UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947/3 "2013-10-14T23:19:58Z")

</div>

Hi Ivan,

thanks for your help, now I got my termsStatsFacet but I don't know how to  
do the sum:  
termsStatsFacet("payment").keyField("payment.code").valueScript(  
"doc['amount'].value")

any clue?

thanks

Le lundi 14 octobre 2013 14:23:16 UTC-7, Ivan Brusic a écrit :

> Look into using the value\_script with the terms stats facet.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html)
> 
> --  
> Ivan
> 
> On Mon, Oct 14, 2013 at 2:16 PM, \<[didier....@gmail.com](mailto:didier....@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi folks,
> > 
> > I am wondering if it is possible, for any kind of facets, to define and  
> > use a function instead of the count ?
> > 
> > Quick example:
> > 
> > I have a term facet on a field which can be Paid or Unpaid, and I got the  
> > number of hits.
> > 
> > term: Paid  
> > count: 12
> > 
> > term: Unpaid  
> > count: 10
> > 
> > I also have another field "amount", and I am wondering if I can define a  
> > function to say : for each match, add the amount to the sum.
> > 
> > term: Paid  
> > count: 120 (for an amount of 10)
> > 
> > term: Unpaid  
> > count: 100
> > 
> > Thanks for your help
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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:** ![didier\_baquier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didier_baquier/32/2055_2.png) [@didier\_baquier](https://discuss.elastic.co/u/didier_baquier)\
**Post date:** [October 16, 2013, 6:03pm UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947/4 "2013-10-16T18:03:32Z")

</div>

I am still blocked on that. I tried to use a param by doing something like  
that (sum is my param) :  
sum = sum + doc['amount'].value; return sum;

but it is not working and I am not really sure that's the good way to do it.

Does anyone can help?

Le lundi 14 octobre 2013 16:19:58 UTC-7, [didier....@gmail.com](mailto:didier....@gmail.com) a écrit :

> Hi Ivan,
> 
> thanks for your help, now I got my termsStatsFacet but I don't know how to  
> do the sum:  
> termsStatsFacet("payment").keyField("payment.code").valueScript(  
> "doc['amount'].value")
> 
> any clue?
> 
> thanks
> 
> Le lundi 14 octobre 2013 14:23:16 UTC-7, Ivan Brusic a écrit :
> 
> > Look into using the value\_script with the terms stats facet.
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html)
> > 
> > --  
> > Ivan
> > 
> > On Mon, Oct 14, 2013 at 2:16 PM, [didier....@gmail.com](mailto:didier....@gmail.com) wrote:
> > 
> > > Hi folks,
> > > 
> > > I am wondering if it is possible, for any kind of facets, to define and  
> > > use a function instead of the count ?
> > > 
> > > Quick example:
> > > 
> > > I have a term facet on a field which can be Paid or Unpaid, and I got  
> > > the number of hits.
> > > 
> > > term: Paid  
> > > count: 12
> > > 
> > > term: Unpaid  
> > > count: 10
> > > 
> > > I also have another field "amount", and I am wondering if I can define a  
> > > function to say : for each match, add the amount to the sum.
> > > 
> > > term: Paid  
> > > count: 120 (for an amount of 10)
> > > 
> > > term: Unpaid  
> > > count: 100
> > > 
> > > Thanks for your help
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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:** ![didier\_baquier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/didier_baquier/32/2055_2.png) [@didier\_baquier](https://discuss.elastic.co/u/didier_baquier)\
**Post date:** [October 16, 2013, 7:04pm UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947/5 "2013-10-16T19:04:28Z")

</div>

I am a bit confused, the termsStatsFacet should return the sum in the  
"total" field. but the total is wrong (the min and max are good).

as you can see below, there are 3 hits, but the stats are wrong (except for  
the min and the max)

I am really confused about the "count" why it's 3 and 7?

here is, how I declare my facet:  
search.addFacet(termsStatsFacet("payment\_stats").keyField("statuses.payment.code").valueField("amount"));

here is the response from elasticsearch:

{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 3,  
"max\_score" : null,  
"hits" : [ {  
"\_index" : "sales",  
"\_type" : "bill",  
"\_id" : "5201\_12",  
"\_score" : null,  
"fields" : {  
"amount" : 2456.0,  
"payment" : {  
"code" : "not.paid"  
},  
....  
},  
"sort" : [1383609600000]  
}, {  
"\_index" : "sales",  
"\_type" : "bill",  
"\_id" : "5203\_12",  
"\_score" : null,  
"fields" : {  
"amount" : 314.96,  
"payment" : {  
"code" : "paid"  
},  
....  
},  
"sort" : [-9223372036854775808]  
}, {  
"\_index" : "sales",  
"\_type" : "bill",  
"\_id" : "5204\_12",  
"\_score" : null,  
"fields" : {  
"amount" : 68.96,  
"payment" : {  
"code" : "paid"  
},  
....  
},  
"sort" : [-9223372036854775808]  
} ]  
},  
"facets" : {  
"payment\_stats" : {  
"\_type" : "terms\_stats",  
"missing" : 0,  
"terms" : [ {  
"term" : "paid",  
"count" : 7,  
"total\_count" : 7,  
"min" : 68.96,  
"max" : 314.96,  
"total" : 1466.72,  
"mean" : 209.53142857142856  
}, {  
"term" : "not.paid",  
"count" : 3,  
"total\_count" : 3,  
"min" : 2456.0,  
"max" : 2456.0,  
"total" : 7368.0,  
"mean" : 2456.0  
} ]  
}  
}  
}

If I declare a simple TermFacet I got the good count:  
"payment" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 3,  
"other" : 0,  
"terms" : [ {  
"term" : "not.paid",  
"count" : 1  
}, {  
"term" : "paid",  
"count" : 2  
} ]  
}

thanks for your help

Le lundi 14 octobre 2013 16:19:58 UTC-7, [didier....@gmail.com](mailto:didier....@gmail.com) a écrit :

> Hi Ivan,
> 
> thanks for your help, now I got my termsStatsFacet but I don't know how to  
> do the sum:  
> termsStatsFacet("payment").keyField("payment.code").valueScript(  
> "doc['amount'].value")
> 
> any clue?
> 
> thanks
> 
> Le lundi 14 octobre 2013 14:23:16 UTC-7, Ivan Brusic a écrit :
> 
> > Look into using the value\_script with the terms stats facet.
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-terms-stats-facet.html)
> > 
> > --  
> > Ivan
> > 
> > On Mon, Oct 14, 2013 at 2:16 PM, [didier....@gmail.com](mailto:didier....@gmail.com) wrote:
> > 
> > > Hi folks,
> > > 
> > > I am wondering if it is possible, for any kind of facets, to define and  
> > > use a function instead of the count ?
> > > 
> > > Quick example:
> > > 
> > > I have a term facet on a field which can be Paid or Unpaid, and I got  
> > > the number of hits.
> > > 
> > > term: Paid  
> > > count: 12
> > > 
> > > term: Unpaid  
> > > count: 10
> > > 
> > > I also have another field "amount", and I am wondering if I can define a  
> > > function to say : for each match, add the amount to the sum.
> > > 
> > > term: Paid  
> > > count: 120 (for an amount of 10)
> > > 
> > > term: Unpaid  
> > > count: 100
> > > 
> > > Thanks for your help
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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:12am UTC](https://discuss.elastic.co/t/facet-define-and-use-a-function-instead-of-the-traditional-count/13947/6 "2017-07-06T02:12:06Z")

</div>


