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


(Sandeep Verma) #1

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


(Makoto Nozawa) #2

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

(Sandeep Verma) #3

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


(Makoto Nozawa) #4

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

(system) #5

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