Help on pipeline aggregation

Here documents:

{"ip" : "", "event" : "click"}

Where IP is the user IP, EVENT the event (click | buy | home | basket ...).

I want to know the average count of each event, distinct by IP. Using SparkSQL with ES, this is done by this query:

SELECT AVG(total), event 
FROM (SELECT COUNT(*) AS total, client.ip, AS event 
    FROM my
    GROUP BY, client.ip HAVING total < 100) my2
GROUP BY event

That gives:


In ES, I try to use the new pipeline aggregation but I didn't manage to find the right aggs imbrication to make it working ;-(