Query to get sales done by employees

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.

You can do a Terms aggregation on the employee name

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
	}]
}

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

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