# How to count?

**URL:** <https://discuss.elastic.co/t/how-to-count/16047>\
**Category:** Elasticsearch\
**Created:** [February 27, 2014, 6:51am UTC](https://discuss.elastic.co/t/how-to-count/16047 "2014-02-27T06:51:39Z")\
**Posts on this page:** 6\
**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:** [February 27, 2014, 6:51am UTC](https://discuss.elastic.co/t/how-to-count/16047/1 "2014-02-27T06:51:39Z")

</div>

Hello

I want to analysis " who read the how many books?"

When bookid is duplicate, I don't want to count.

POST /bookdatas/\_search  
{  
"size": 0,  
"facets": {  
"ips\_stats": {  
"terms\_stats": {  
"key\_field": "userId",  
"value\_field": "duration",  
"size": 5  
},  
"facet\_filter": {  
"fquery": {  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"should": [  
{  
"query\_string": {  
"query": ""(Google)bbb@gmail.com""  
}  
}  
]  
}  
}  
}  
}  
}  
}  
}  
}  
}

result  
"facets": {  
"ips\_stats": {  
"\_type": "terms\_stats",  
"missing": 0,  
"terms": [  
{  
"term": "(Google)bbb@gmail.com",  
"count": 64,  
"total\_count": 3,  
"min": 2,  
"max": 139436,  
"total": 140733,  
"mean": 46911  
},

userId prdId duration chapId(Google)bbb@gmail.com010020244421  
(Google)bbb@gmail.com01002024441394361(Google)bbb@gmail.com010020244412951  
A user only read a book, but count is three.

Can you give me a suggestion??

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/6e865a48-54ad-4f95-aa60-732598d35de8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6e865a48-54ad-4f95-aa60-732598d35de8%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:** [February 27, 2014, 9:33am UTC](https://discuss.elastic.co/t/how-to-count/16047/2 "2014-02-27T09:33:44Z")

</div>

I mean like mysql query

select count( distinct id ) from tablename;

Thanks

Nick Chang於 2014年2月27日星期四UTC+8下午2時51分39秒寫道：

> Hello
> 
> I want to analysis " who read the how many books?"
> 
> When bookid is duplicate, I don't want to count.
> 
> POST /bookdatas/\_search  
> {  
> "size": 0,  
> "facets": {  
> "ips\_stats": {  
> "terms\_stats": {  
> "key\_field": "userId",  
> "value\_field": "duration",  
> "size": 5  
> },  
> "facet\_filter": {  
> "fquery": {  
> "query": {  
> "filtered": {  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "query\_string": {  
> "query": ""(Google)bbb@gmail.com""  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> result  
> "facets": {  
> "ips\_stats": {  
> "\_type": "terms\_stats",  
> "missing": 0,  
> "terms": [  
> {  
> "term": "(Google)bbb@gmail.com",  
> "count": 64,  
> "total\_count": 3,  
> "min": 2,  
> "max": 139436,  
> "total": 140733,  
> "mean": 46911  
> },
> 
> userId prdId duration chapId(Google)bbb@gmail.com010020244421(Google)  
> bbb@gmail.com01002024441394361(Google)bbb@gmail.com010020244412951  
> A user only read a book, but count is three.
> 
> Can you give me a suggestion??
> 
> 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/8accddc1-9c11-459b-b384-f2b430af39ac%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8accddc1-9c11-459b-b384-f2b430af39ac%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:** [February 27, 2014, 12:58pm UTC](https://discuss.elastic.co/t/how-to-count/16047/3 "2014-02-27T12:58:51Z")

</div>

Field collapsing is coming in a future version of ES. For now, you can use  
a terms facet and the all\_terms option:

{  
"query" : {  
"match\_all" : { }  
},  
"facets" : {  
"tag" : {  
"terms" : {  
"field" : "distinctid",  
"all\_terms" : true  
}  
}  
}  
}

And then in the result, just count the total number of entries in the terms  
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/7aafecc3-2f7f-4537-a5c2-c7fd38c4b2b6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7aafecc3-2f7f-4537-a5c2-c7fd38c4b2b6%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\_2](https://avatars.discourse-cdn.com/v4/letter/n/7c8e57/32.png) [@Nick\_Chang\_2](https://discuss.elastic.co/u/Nick_Chang_2)\
**Post date:** [February 27, 2014, 2:57pm UTC](https://discuss.elastic.co/t/how-to-count/16047/4 "2014-02-27T14:57:26Z")

</div>

Hello Binh

Thanks.

It's my query

{  
"query" : {  
"bool": {  
"must": [  
{"query\_string": {  
"query": ""(Google)bbb@gmail.com""  
}}  
],  
"must": [  
{"query\_string": {  
"query": "ReadingLog.ChapLog"  
}}  
]  
}  
},  
"facets" : {  
"tag" : {  
"terms" : {  
"field" : "prdId",  
"all\_terms" : true  
}  
}  
},  
"size": 0  
}

But result  
"facets": {  
"tag": {  
"\_type": "terms",  
"missing": 0,  
"total": 3,  
"other": 0,  
"terms": [  
{  
"term": "0100202444",  
"count": 3

Not count:1.  
Maybe I lose some pattern, Do you have any suggest??

Thanks

Binh Ly於 2014年2月27日星期四UTC+8下午8時58分51秒寫道：

> Field collapsing is coming in a future version of ES. For now, you can use  
> a terms facet and the all\_terms option:
> 
> {  
> "query" : {  
> "match\_all" : { }  
> },  
> "facets" : {  
> "tag" : {  
> "terms" : {  
> "field" : "distinctid",  
> "all\_terms" : true  
> }  
> }  
> }  
> }
> 
> And then in the result, just count the total number of entries in the  
> terms 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/e115cc35-260a-4db2-819a-ee239d1a2c6e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e115cc35-260a-4db2-819a-ee239d1a2c6e%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:** [February 27, 2014, 3:12pm UTC](https://discuss.elastic.co/t/how-to-count/16047/5 "2014-02-27T15:12:33Z")

</div>

Your distinct count should be the size of the terms array:

facets.tag.terms.length

--  
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/878d7653-591e-47a6-ac74-f3f6b4e92067%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/878d7653-591e-47a6-ac74-f3f6b4e92067%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:46am UTC](https://discuss.elastic.co/t/how-to-count/16047/6 "2017-07-06T01:46:53Z")

</div>

Closed on July 6, 2017, 1:46am UTC


