Elasticsearch中的总计数/总和

时间:2019-01-20 17:12:57

标签: elasticsearch elasticsearch-aggregation

我试图弄清楚如何在Elasticsearch索引中总结许多不同的计数。索引中的文档如下所示:

{
  '_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
      }
    }
  }
}

如何总结索引中locationdatespersons中所有不同的词?例如,如果另一个文档出现了两次Alex,我将得到Alex: 3, Robert: 3, ..

1 个答案:

答案 0 :(得分:0)

这将为您提供特定字段的总和

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" } }
    }
}
'

这会给你计数

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" }
       }
    }
}'

您需要指定字段。