Nested Composite Aggregations with ElasticSearch

I have an index that stores sales by salesperson, and each sale has a service rating. Using composite I get these separate ratings within an object by duplicating the seller ID. Is it possible to nest within a single object per seller the count per review?

This is the result I need, organized and easy to locate data:

{
    [
        {
            "operator": 79852,
            "doc_count": 1,
            "reviews": {
                "review": "Good",
                "doc_count": 1
            }
        },
        {
            "operator": 79855,
            "doc_count": 3,
            "reviews": {
                [
                    {
                        "review": "Good",
                        "doc_count": 2
                    },
                    {
                        "review": "Bad",
                        "doc_count": 1
                    }
                ]
            }
        }
    ]
}

My code currently looks like this:

{
    ...
    "aggregations": {
        "groupby": {
            "composite": {
                "size": 1000,
                "sources": [
                    {
                        "operator": {
                            "terms": {
                                "field": "operatorId",
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    },
                    {
                        "review": {
                            "terms": {
                                "field": "review",
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    }
                ]
            }
        }
    }
}

Result:

{
    ...
    "hits": {
        "total": 5,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "groupby": {
            "after_key": {
                "operator": 79855,
                "review": "Bad"
            },
            "buckets": [
                {
                    "key": {
                        "operator": 79852,
                        "review": "Bad"
                    },
                    "doc_count": 2
                },
                {
                    "key": {
                        "operator": 79852,
                        "review": "Good"
                    },
                    "doc_count": 1
                },
                {
                    "key": {
                        "operator": 79852,
                        "review": "Regular"
                    },
                    "doc_count": 1
                },
                {
                    "key": {
                        "operator": 79855,
                        "review": "Bad"
                    },
                    "doc_count": 1
                }
            ]
        }
    }
}

Hi, try sub aggregation.

Thanks.

Two Solutions

{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggs": {
        "datagroup": {
            "terms": {
                "field": "sellerid"
            },
            "aggs": {
                "good": {
                    "sum": {
                        "script": "(doc['review'].value=='G')?1:0"
                    }
                },
                "bad": {
                    "sum": {
                        "script": "(doc['review'].value=='B')?1:0"
                    }
                },
                "regular": {
                    "sum": {
                        "script": "(doc['review'].value=='N')?1:0"
                    }
                }
            }
        }
    }
}
{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggs": {
        "datagroup": {
            "terms": {
                "field": "sellerid"
            },
            "aggs": {
                "review": {
                    "terms": {
                        "field": "review"
                    }
                }
            }
        }
    }
}
1 Like

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