# Query to get sales done by employees

**URL:** <https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324>\
**Category:** Elasticsearch\
**Created:** [February 24, 2018, 6:43am UTC](https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324 "2018-02-24T06:43:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tushar\_chevulkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_chevulkar/32/46270_2.png) [@tushar\_chevulkar](https://discuss.elastic.co/u/tushar_chevulkar)\
**Post date:** [February 24, 2018, 6:43am UTC](https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324/1 "2018-02-24T06:43:54Z")

</div>

I need to write a query where I search any product name it should show me who all has sold this products and the quantity that they have sold.

> ```
> {
> "user": {
> "id": "1",
> "full_name": "John"
> },
> "product": {
> "id": "1",
> "product_name": "Pen"
> },		
> "create_date": "2015-09-18T07:02:46+02:00",
> },	{
> "user": {
> "id": "2",
> "full_name": "Joe"
> },
> "product": {
> "id": "1",
> "product_name": "Pen"
> },		
> "create_date": "2015-10-18T07:02:46+02:00",
> },{
> "user": {
> "id": "2",
> "full_name": "Joe"
> },
> "product": {
> "id": "3",
> "product_name": "Pencil"
> },		
> "create_date": "2015-12-18T07:02:46+02:00",
> },{
> "user": {
> "id": "3",
> "full_name": "Tim"
> },
> "product": {
> "id": "3",
> "product_name": "Pencil"
> },		
> "create_date": "2015-12-18T07:02:46+02:00",
> },{
> "user": {
> "id": "1",
> "full_name": "John"
> },
> "product": {
> "id": "3",
> "product_name": "Pencil"
> },		
> "create_date": "2015-12-18T07:02:46+02:00",
> },{
> "user": {
> "id": "1",
> "full_name": "John"
> },
> "product": {
> "id": "1",
> "product_name": "Pen"
> },		
> "create_date": "2015-12-18T07:02:46+02:00",
> }
> 
> ```

Like in the above case if i search for pen it should show me results for pen and pencil and who has sold those and what quantity.  
I have no idea if this is possible or how do it start.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 24, 2018, 8:18am UTC](https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324/2 "2018-02-24T08:18:55Z")

</div>

You can do a Terms aggregation on the employee name

---

<div class="post-metadata">

**Author:** ![tushar\_chevulkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_chevulkar/32/46270_2.png) [@tushar\_chevulkar](https://discuss.elastic.co/u/tushar_chevulkar)\
**Post date:** [February 24, 2018, 9:54am UTC](https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324/3 "2018-02-24T09:54:43Z")

</div>

I can do that but if I search for pen i need results for pen and pencil and need to know which user has matched which query.I need something like

```
{
	"Pen": [{
		"Name": "John",
		"count": 1
	}, {
		"Name": "Joe",
		"count": 1
	}],

	"Pencil": [{
		"Name": "John",
		"count": 2
	}, {
		"Name": "Joe",
		"count": 1
	}, {
		"Name": "Tim",
		"count": 1
	}]
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 24, 2018, 10:38am UTC](https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324/4 "2018-02-24T10:38:02Z")

</div>

So a 1st level of agg with `terms` on `product`, then a sub agg with `terms` on `Name`.

---

<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:** [March 24, 2018, 10:38am UTC](https://discuss.elastic.co/t/query-to-get-sales-done-by-employees/121324/5 "2018-03-24T10:38:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
