K.Sudha Snigdha
K.Sudha Snigdha

Reputation: 19

nested count aggregations in elasticsearch

I have a type in elasticsearch where each user can post any number of posts(fields being "userid" and "post").Now I need the count of users who posted 0 post,1 post,2 posts and so on....how do I do it? I think it needs some nested aggregations implemented but I don't know how to proceed. Thanks in advance !

Upvotes: 0

Views: 146

Answers (2)

mbudnik
mbudnik

Reputation: 2107

The best way of doing this is to add a separate field to store the number of posts.

Scripts are not too efficient (values are getting re-evaluated each time a query executes) and you get the value indexed properly which makes queries and aggregations very fast. Of course you need to be sure you update this count each time you update the document.

Upvotes: 1

Richa
Richa

Reputation: 7649

You can use script in aggregation:

POST index_name/type_name/_search
{
"aggs": {
  "group By Post Count": {
     "terms": {
         "script" : "doc['post'].size()"
       }
     }
   }
 }

Make sure you enable scriptig

Hope this helps you.

Upvotes: 0

Related Questions