# Problem with terms facet multi fields?

**URL:** <https://discuss.elastic.co/t/problem-with-terms-facet-multi-fields/16101>\
**Category:** Elasticsearch\
**Created:** [March 3, 2014, 9:30am UTC](https://discuss.elastic.co/t/problem-with-terms-facet-multi-fields/16101 "2014-03-03T09:30:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Chang](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@Nick\_Chang](https://discuss.elastic.co/u/Nick_Chang)\
**Post date:** [March 3, 2014, 9:30am UTC](https://discuss.elastic.co/t/problem-with-terms-facet-multi-fields/16101/1 "2014-03-03T09:30:59Z")

</div>

Hello

Why does not like Mysql count(DISTINCT userId, prdId) ??

{  
"facets": {  
"book": {  
"terms": {  
"fields": [  
"userId","prdId"  
],  
"size": 10,  
"order": "count"  
}  
}  
},  
"size": 0  
}

result :  
"facets": {  
"book": {  
"\_type": "terms",  
"missing": 299410,  
"total": 1765354,  
"other": 1483806,  
"terms": [  
{  
"term": "(Facebook)abc@gmail.com",  
"count": 119710  
},  
{  
"term": "guest",  
"count": 76341  
},  
{  
"term": "0100051552",  
"count": 18563  
},

Is't my misunderstanding?? or I lost other parameter??

Thanks

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/529a0f7d-5b1a-4f5a-b76e-6fc7b920dcb6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/529a0f7d-5b1a-4f5a-b76e-6fc7b920dcb6%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 3, 2014, 8:16pm UTC](https://discuss.elastic.co/t/problem-with-terms-facet-multi-fields/16101/2 "2014-03-03T20:16:42Z")

</div>

The terms facet will use a combo of term values from all the fields as if  
it was a single field. What you want to do is probably a script that  
combines userId and prodId, and then does a terms facet on the full combo.  
Assuming that userId and prodId are full single tokens (i.e. for example  
not\_analyzed"), you can do something like this:

{  
"facets": {  
"user\_prod": {  
"terms": {  
"script": "doc['userId'].value.toString() +  
doc['prodId'].value.toString()"  
}  
}  
}  
}

And if you want the total distinct count, you will need to turn on  
all\_terms and then count the number of terms returned in the facet array.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/18c32f65-00ed-4adf-bb8f-9d8c157b72a6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/18c32f65-00ed-4adf-bb8f-9d8c157b72a6%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Nick\_Chang](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@Nick\_Chang](https://discuss.elastic.co/u/Nick_Chang)\
**Post date:** [March 4, 2014, 6:35am UTC](https://discuss.elastic.co/t/problem-with-terms-facet-multi-fields/16101/3 "2014-03-04T06:35:12Z")

</div>

Thanks Binh

But, In elasticsearch, How to count the number of terms returned in the  
facet array?

I found the doc. Not include facet array length function.

Do you have any suggest??

Thanks again.

Binh Ly於 2014年3月4日星期二UTC+8上午4時16分42秒寫道：

> The terms facet will use a combo of term values from all the fields as if  
> it was a single field. What you want to do is probably a script that  
> combines userId and prodId, and then does a terms facet on the full combo.  
> Assuming that userId and prodId are full single tokens (i.e. for example  
> not\_analyzed"), you can do something like this:
> 
> {  
> "facets": {  
> "user\_prod": {  
> "terms": {  
> "script": "doc['userId'].value.toString() +  
> doc['prodId'].value.toString()"  
> }  
> }  
> }  
> }
> 
> And if you want the total distinct count, you will need to turn on  
> all\_terms and then count the number of terms returned in the facet array.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d45217b7-e7cc-4153-8608-ab6f7b1ff9d1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d45217b7-e7cc-4153-8608-ab6f7b1ff9d1%40googlegroups.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, 1:45am UTC](https://discuss.elastic.co/t/problem-with-terms-facet-multi-fields/16101/4 "2017-07-06T01:45:56Z")

</div>


