# Custom facet

**URL:** <https://discuss.elastic.co/t/custom-facet/9716>\
**Category:** Elasticsearch\
**Created:** [November 14, 2012, 2:10pm UTC](https://discuss.elastic.co/t/custom-facet/9716 "2012-11-14T14:10:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cristian\_Toader](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Cristian\_Toader](https://discuss.elastic.co/u/Cristian_Toader)\
**Post date:** [November 14, 2012, 2:10pm UTC](https://discuss.elastic.co/t/custom-facet/9716/1 "2012-11-14T14:10:01Z")

</div>

Hi,

Do you have any idea how I could obtain from:

{"timestamp": "random\_date", "tag":"red", "counter": "3"}  
{"timestamp": "random\_date", "tag":"blue", "counter": "2"}  
{"timestamp": "random\_date", "tag":"red", "counter":"7"}

a facet that would show:  
tag: red  
count: 10

tag: blue  
count: 2

aka do the sum for the counter for each tag in only one request ?

Thank you,

Cristian

--

---

<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:** [November 14, 2012, 5:58pm UTC](https://discuss.elastic.co/t/custom-facet/9716/2 "2012-11-14T17:58:26Z")

</div>

Never tried, but perhaps the terms stat facet using the counter field as  
the value field? It will return too many stats, but one of them will be the  
sum of all values.

Cheers,

Ivan

On Wed, Nov 14, 2012 at 6:10 AM, Cristian Toader [ticosan@gmail.com](mailto:ticosan@gmail.com) wrote:

> Hi,
> 
> Do you have any idea how I could obtain from:
> 
> {"timestamp": "random\_date", "tag":"red", "counter": "3"}  
> {"timestamp": "random\_date", "tag":"blue", "counter": "2"}  
> {"timestamp": "random\_date", "tag":"red", "counter":"7"}
> 
> a facet that would show:  
> tag: red  
> count: 10
> 
> tag: blue  
> count: 2
> 
> aka do the sum for the counter for each tag in only one request ?
> 
> Thank you,
> 
> Cristian
> 
> --

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [November 15, 2012, 7:57am UTC](https://discuss.elastic.co/t/custom-facet/9716/3 "2012-11-15T07:57:19Z")

</div>

Hi Cristi 🙂

Yep, terms stats should do the trick:

> <https://gist.github.com/radu-gheorghe/4077281>

But please note that the "counter" field needs to be numeric. If you  
index it as a string it won't work:

> <https://gist.github.com/radu-gheorghe/4077293>

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Nov 14, 2012 at 4:10 PM, Cristian Toader [ticosan@gmail.com](mailto:ticosan@gmail.com) wrote:

> Hi,
> 
> Do you have any idea how I could obtain from:
> 
> {"timestamp": "random\_date", "tag":"red", "counter": "3"}  
> {"timestamp": "random\_date", "tag":"blue", "counter": "2"}  
> {"timestamp": "random\_date", "tag":"red", "counter":"7"}
> 
> a facet that would show:  
> tag: red  
> count: 10
> 
> tag: blue  
> count: 2
> 
> aka do the sum for the counter for each tag in only one request ?
> 
> Thank you,
> 
> Cristian

--

---

<div class="post-metadata">

**Author:** ![Cristian\_Toader](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Cristian\_Toader](https://discuss.elastic.co/u/Cristian_Toader)\
**Post date:** [November 16, 2012, 1:00pm UTC](https://discuss.elastic.co/t/custom-facet/9716/4 "2012-11-16T13:00:18Z")

</div>

Thank you both for the answer 🙂

I had to do some tweaking in the after code, since the facet results are  
ordered by count and not by the stat I need. I'd which to be able to sort  
such facet by any of its component stats ;).

Cristian

On Thursday, November 15, 2012 9:57:24 AM UTC+2, Radu Gheorghe wrote:

> Hi Cristi 🙂
> 
> Yep, terms stats should do the trick:  
> [Elasticsearch terms facet · GitHub](https://gist.github.com/4077281)
> 
> But please note that the "counter" field needs to be numeric. If you  
> index it as a string it won't work:  
> [Elasticsearch terms facet string · GitHub](https://gist.github.com/4077293)
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Wed, Nov 14, 2012 at 4:10 PM, Cristian Toader \<[tic...@gmail.com](mailto:tic...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> > Hi,
> > 
> > Do you have any idea how I could obtain from:
> > 
> > {"timestamp": "random\_date", "tag":"red", "counter": "3"}  
> > {"timestamp": "random\_date", "tag":"blue", "counter": "2"}  
> > {"timestamp": "random\_date", "tag":"red", "counter":"7"}
> > 
> > a facet that would show:  
> > tag: red  
> > count: 10
> > 
> > tag: blue  
> > count: 2
> > 
> > aka do the sum for the counter for each tag in only one request ?
> > 
> > Thank you,
> > 
> > Cristian

--

---

<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:03am UTC](https://discuss.elastic.co/t/custom-facet/9716/5 "2017-07-06T03:03:58Z")

</div>


