# Can I sort product by action value purchase count using aggs?

**URL:** <https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026>\
**Category:** Elasticsearch\
**Created:** [January 27, 2017, 2:23pm UTC](https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026 "2017-01-27T14:23:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeep\_verma1](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@sandeep\_verma1](https://discuss.elastic.co/u/sandeep_verma1)\
**Post date:** [January 27, 2017, 2:23pm UTC](https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026/1 "2017-01-27T14:23:49Z")

</div>

Doc Sample  
[  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "5",  
"\_score": 1,  
"\_source": {  
"product": "B",  
"action": "cart"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "8",  
"\_score": 1,  
"\_source": {  
"product": "B",  
"action": "purchase"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "9",  
"\_score": 1,  
"\_source": {  
"product": "B",  
"action": "purchase"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "2",  
"\_score": 1,  
"\_source": {  
"product": "A",  
"action": "view"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "4",  
"\_score": 1,  
"\_source": {  
"product": "A",  
"action": "cart"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "6",  
"\_score": 1,  
"\_source": {  
"product": "B",  
"action": "cart"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "1",  
"\_score": 1,  
"\_source": {  
"product": "A",  
"action": "view"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "7",  
"\_score": 1,  
"\_source": {  
"product": "B",  
"action": "view"  
}  
},  
{  
"\_index": "abc",  
"\_type": "ef",  
"\_id": "3",  
"\_score": 1,  
"\_source": {  
"product": "A",  
"action": "cart"  
}  
}  
]

---

<div class="post-metadata">

**Author:** ![mnozawa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mnozawa/32/15091_2.png) [@mnozawa](https://discuss.elastic.co/u/mnozawa)\
**Post date:** [February 1, 2017, 12:03pm UTC](https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026/2 "2017-02-01T12:03:13Z")

</div>

Hi,

How about the query below?

```
GET INDEX_NAME/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "purchased_actions": {
      "filter": {
        "term": {
          "action": "purchase"
        }
      },
      "aggs": {
        "product_bucket": {
          "terms": {
            "field": "product",
            "size": 10,
            "order": {
              "_count": "desc"
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![sandeep\_verma1](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@sandeep\_verma1](https://discuss.elastic.co/u/sandeep_verma1)\
**Post date:** [February 7, 2017, 4:11pm UTC](https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026/3 "2017-02-07T16:11:41Z")

</div>

thank mnozawa but problem is ,i need to crete a roll up where i need all product is there purchase action action is zero and sort it on the basis of purchase ,view and cart.  
product view purchase cart  
a 2 1 4  
b 1 1 1  
c 0 0 0

---

<div class="post-metadata">

**Author:** ![mnozawa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mnozawa/32/15091_2.png) [@mnozawa](https://discuss.elastic.co/u/mnozawa)\
**Post date:** [February 7, 2017, 5:48pm UTC](https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026/4 "2017-02-07T17:48:12Z")

</div>

In that case, by the query below, you can get the product \* action matrix.  
(But it include neither zero action nor zero product.)

```
GET INDEX_NAME/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0,
  "aggs": {
    "product_bucket": {
      "terms": {
        "field": "product"
      },
      "aggs": {
        "action_bucket": {
          "terms": {
            "field": "action"
          }
        }
      }
    }
  }
}
```

---

<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 7, 2017, 5:48pm UTC](https://discuss.elastic.co/t/can-i-sort-product-by-action-value-purchase-count-using-aggs/73026/5 "2017-03-07T17:48:24Z")

</div>

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