ninesalt
ninesalt

Reputation: 4354

Aggregate counting/sum in elasticsearch

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, ..

Upvotes: 3

Views: 1883

Answers (1)

Ashish Tiwari
Ashish Tiwari

Reputation: 2277

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.

Upvotes: 1

Related Questions