3

I'm trying to figure out how to sum up many different counts in an elasticsearch index. A document in the index looks like this:

{
  '_source': {
    'my_field': 'Robert and Alex went with Robert to play in London and then Robert went to London',
    'ner': {
      'persons': {
        'Alex': 1,
        'Robert': 3
      },
      'organizations': {},
      'dates': {},
      'locations': {
        'London': 2
      }
    }
  }
}

How can I sum up all the different words in location, dates and persons in the index? For example if another document had 2 occurrences of Alex, I'd get Alex: 3, Robert: 3, ..

4
  • You need count of occurrence or sum ? Both are different and both have different ways Commented Jan 21, 2019 at 5:44
  • @ashishtiwari Trying to get the sum Commented Jan 21, 2019 at 10:17
  • i guess sum aggregation will work for you elastic.co/guide/en/elasticsearch/reference/current/… Commented Jan 21, 2019 at 12:07
  • @ashishtiwari how will this work if I can't explicitly write down the name of the field (Alex, Robert, London, etc)? Commented Jan 21, 2019 at 12:18

1 Answer 1

1

This will give you sum of specific field

curl -X POST "localhost:9200/yourindex/_search?size=0" -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "person_Alex" : { "sum" : { "field" : "person.Alex" } },
        "person_Robert" : { "sum" : { "field" : "person.Robert" } },
        "person_Alex" : { "sum" : { "field" : "person.Alex" } }
    }
}
'

This will give you count

curl -X GET "localhost:9200/yourindex/_search" -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "count_Alex" : {
            "terms" : { "field" : "person.Alex" }
        },
        "count_Robert" : {
            "terms" : { "field" : "person.Alex" }
       }
    }
}'

You need to specify fields.

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

1 Comment

I dont think you understand my question. I won't know the names of the field because they're automatically extracted and each document will have different fields. I want to sum the fields they have in common.

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.