# Multi term faceting on multiple field by 'AND' instead of 'OR'

**URL:** <https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405>\
**Category:** Elasticsearch\
**Created:** [January 18, 2013, 9:26am UTC](https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405 "2013-01-18T09:26:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmet\_DAL](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_dal/32/2516_2.png) [@Ahmet\_DAL](https://discuss.elastic.co/u/Ahmet_DAL)\
**Post date:** [January 18, 2013, 9:26am UTC](https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405/1 "2013-01-18T09:26:17Z")

</div>

Hi,  
I need to get count of two field. I gues, I need a term facet which should  
correspond to SQL part which is like "group by field1,field2". I saw the  
elasticsearch multi field term facet examples but it is not what I look  
for. Because it is faceting on fields as seperated. I read a related  
question which is at  
[https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/7NwoiR87Tuw](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/7NwoiR87Tuw) before.  
There is a suggestion about scripting. I might have misunderstood but if  
this is the solution how can I do it with scripting or is there another way  
to get count of two field together ?

Thank you.

--

---

<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:** [January 18, 2013, 9:28pm UTC](https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405/2 "2013-01-18T21:28:46Z")

</div>

Here is an example of how to do it with  
scripting [https://groups.google.com/d/topic/elasticsearch/L5RVcreJSbs/discussion · GitHub](https://gist.github.com/4568750)

On Friday, January 18, 2013 4:26:17 AM UTC-5, Whispered wrote:

> Hi,  
> I need to get count of two field. I gues, I need a term facet which should  
> correspond to SQL part which is like "group by field1,field2". I saw the  
> elasticsearch multi field term facet examples but it is not what I look  
> for. Because it is faceting on fields as seperated. I read a related  
> question which is at  
> [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/7NwoiR87Tuw) before.  
> There is a suggestion about scripting. I might have misunderstood but if  
> this is the solution how can I do it with scripting or is there another way  
> to get count of two field together ?
> 
> Thank you.

--

---

<div class="post-metadata">

**Author:** ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)\
**Post date:** [January 20, 2013, 10:02pm UTC](https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405/3 "2013-01-20T22:02:52Z")

</div>

Igor,

Any performance implications of using this approach?

Thank you,  
Alex

On Friday, January 18, 2013 4:28:46 PM UTC-5, Igor Motov wrote:

> Here is an example of how to do it with scripting  
> [https://groups.google.com/d/topic/elasticsearch/L5RVcreJSbs/discussion · GitHub](https://gist.github.com/4568750)
> 
> On Friday, January 18, 2013 4:26:17 AM UTC-5, Whispered wrote:
> 
> > Hi,  
> > I need to get count of two field. I gues, I need a term facet which  
> > should correspond to SQL part which is like "group by field1,field2". I  
> > saw the elasticsearch multi field term facet examples but it is not what I  
> > look for. Because it is faceting on fields as seperated. I read a  
> > related question which is at  
> > [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/7NwoiR87Tuw) before.  
> > There is a suggestion about scripting. I might have misunderstood but if  
> > this is the solution how can I do it with scripting or is there another way  
> > to get count of two field together ?
> > 
> > Thank you.

--

---

<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:** [January 20, 2013, 10:52pm UTC](https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405/4 "2013-01-20T22:52:32Z")

</div>

It's going to be slow. This script will be executed for every single record  
in the results list. That's why indexing this two fields together would be  
a faster solution as it was already mentioned in the linked discussion.

On Sunday, January 20, 2013 5:02:52 PM UTC-5, AlexR wrote:

> Igor,
> 
> Any performance implications of using this approach?
> 
> Thank you,  
> Alex
> 
> On Friday, January 18, 2013 4:28:46 PM UTC-5, Igor Motov wrote:
> 
> > Here is an example of how to do it with scripting  
> > [https://groups.google.com/d/topic/elasticsearch/L5RVcreJSbs/discussion · GitHub](https://gist.github.com/4568750)
> > 
> > On Friday, January 18, 2013 4:26:17 AM UTC-5, Whispered wrote:
> > 
> > > Hi,  
> > > I need to get count of two field. I gues, I need a term facet which  
> > > should correspond to SQL part which is like "group by field1,field2". I  
> > > saw the elasticsearch multi field term facet examples but it is not what I  
> > > look for. Because it is faceting on fields as seperated. I read a  
> > > related question which is at  
> > > [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/7NwoiR87Tuw) before.  
> > > There is a suggestion about scripting. I might have misunderstood but if  
> > > this is the solution how can I do it with scripting or is there another way  
> > > to get count of two field together ?
> > > 
> > > Thank you.

--

---

<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:55am UTC](https://discuss.elastic.co/t/multi-term-faceting-on-multiple-field-by-and-instead-of-or/10405/5 "2017-07-06T02:55:27Z")

</div>


