# Elasticsearch - count users query

**URL:** <https://discuss.elastic.co/t/elasticsearch-count-users-query/12562>\
**Category:** Elasticsearch\
**Created:** [June 27, 2013, 2:09pm UTC](https://discuss.elastic.co/t/elasticsearch-count-users-query/12562 "2013-06-27T14:09:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oreno](https://avatars.discourse-cdn.com/v4/letter/o/b4bc9f/32.png) [@oreno](https://discuss.elastic.co/u/oreno)\
**Post date:** [June 27, 2013, 2:09pm UTC](https://discuss.elastic.co/t/elasticsearch-count-users-query/12562/1 "2013-06-27T14:09:55Z")

</div>

Hi everyone,

I'm having a problem that I hope someone can help me with:

I have the next mapping for my user index -\> docs(users) containing events  
in which the user bought different products. each event(date) can contain  
several products. I would like to retrieve the count of users that their  
last purchase of TV was TOSHIBA. for that I am willing to have a 'TV' as a  
product by itself.

So in case a have the next events:

user1|2013-05-18|TV,SAMSUNG  
user1|2013-05-19|TV,TOSHIBA  
user1|2013-05-20|PC  
user2|2013-05-18|TV,TOSHIBA  
user2|2013-05-19|TV,SAMSUNG  
user2|2013-05-20|PC

user1 should be counted because its last bought TV was TOSHIBA but user2  
should not.

Does anyone have an idea how to approach this query?

I tried several times but I'm new to elasticsearch so no success yet.

Thanks in advanced,

```
    {

```

"user" : {  
"properties" : {  
"name": {"type" : "string"},  
"events" : {  
"type" : "nested",  
"properties" : {  
"event\_time" : {"type" : "Date"},  
"products" : { "properties" : { "product" : {"type" : "string"} } }  
}  
}  
}  
}  
}

--  
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:29am UTC](https://discuss.elastic.co/t/elasticsearch-count-users-query/12562/2 "2017-07-06T02:29:14Z")

</div>


