【问题标题】:Aggregate counting/sum in elasticsearchelasticsearch中的聚合计数/总和
【发布时间】:2019-06-14 04:33:03
【问题描述】:

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

{
  '_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 中的所有不同单词?例如,如果另一个文档出现 2 次 Alex,我会得到 Alex: 3, Robert: 3, ..

【问题讨论】:

  • 您需要出现次数或总和?两者都是不同的,都有不同的方式
  • @ashishtiwari 试图得到总和
  • @ashishtiwari 如果我不能明确写下该字段的名称(Alex、Robert、London 等),这将如何工作?

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

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

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

您需要指定字段。

【讨论】:

  • 我认为你不明白我的问题。我不知道字段的名称,因为它们是自动提取的,并且每个文档都有不同的字段。我想总结他们共同的领域。
猜你喜欢
  • 2021-11-14
  • 2014-07-27
  • 2016-09-24
  • 2018-04-20
  • 1970-01-01
  • 2016-08-12
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多