0

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 !

3
  • 2
    Please share the mapping of you index. GET /index/type/_mapping Commented Mar 22, 2016 at 7:14
  • 1
    It would probably be helpful if you shared your mapping and one or two sample documents. Also what kind of query you currently have. Commented Mar 22, 2016 at 7:14
  • { "ESIndex": { "mappings": { "user_posts": { "properties": { "post": { "type": "string" }, "userid": { "type": "long" } } } } } } Commented Mar 22, 2016 at 8:20

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

It does not return any error but it does not return the expected results either

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.