i have a product document and that product is published into various eCommerce website like amazon , flipkart as shown in location field. The product published is a nested field in Elasticsearch. now i want to aggregate that like in a month how many places i published and i need data for last 6 months including current month. like in month of August i published in Amazon and Flipkart then it should show count as 2 for month August.
"product_published" : [
{
"id" : "71",
"published_on" : 1687769266,
"location" : "amazon",
},
{
"publisher_id" : "71",
"published_on" : 1687770750,
"location" : "flipkart",
},
{
"publisher_id" : 71,
"published_on" : 1687806928,
"location" : "ebay"
},
{
"publisher_id" : 71,
"published_on" : 1687806928,
"location" : "amazon-india"
},
{
"publisher_id" : 71,
"published_on" : 1687806928,
"location" : "walmart"
},
{
"publisher_id" : "71",
"published_on" : 1687771848,
"location" : "daraz",
},
{
"publisher_id" : "71",
"published_on" : 1687771955,
"location" : "bigbasket",
},
{
"publisher_id" : "71",
"published_on" : 1687772089,
"location" : "blinkit",
}
]
My Aggregation query :
"aggs": {
"count_product_published": {
"nested": {
"path": "product_published"
},
"aggs": {
"product_published": {
"date_histogram": {
"script": "if (doc.containsKey('product_published')) { return Instant.ofEpochMilli(doc['product_published.published_on'].value).atZone(ZoneId.of('UTC')).toLocalDate();} ",
"calendar_interval": "month",
"format": "yyyy-MM",
"time_zone": "UTC",
"extended_bounds": {
"min": "now-6M/M",
"max": "now/M"
}
},
"aggs": {
"published_count": {
"terms": {
"field": "product_published.location.keyword"
}
}
}
}
}
}
}
Result returned :
"aggregations" : {
"count_product_published" : {
"doc_count" : 1796211,
"product_published" : {
"buckets" : [
{
"key_as_string" : "2023-02",
"key" : 1675209600000,
"doc_count" : 0,
"published_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
},
{
"key_as_string" : "2023-03",
"key" : 1677628800000,
"doc_count" : 0,
"published_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
It is returning document count as 0.