根据出现次数进行计数

根据出现次数进行计数

本文介绍了根据出现次数进行计数-ElasticSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ElasticSearch索引中为大约3万个文档建立了索引.这些文档中的每一个都有一个称为严重性"的字段,它是一个整数值.这些整数的范围为0到5,增量为1(0,1,2,3,4,5).我想对严重性为0,严重性为1,严重性为2等的文档数进行计数.

I have indexed around 30K documents in an ElasticSearch index. Each of these documents has a field called severity, which is an Integer value. These integers can range from 0 to 5, in increments of 1 (0,1,2,3,4,5). I would like to get a count of the number of documents with severity 0, severity 1, severity 2, etc....

我尝试了value_count和range,但是它们似乎都不适合我的目的.

I have tried value_count and range, but they both seem to be unsuitable for my purpose.

这是我的一个文档的样子.一些值已被删除,但最重要的是根据严重性进行汇总

This is what one of my documents looks like. Some values have been removed, but the essential thing is to aggregate based on severity

{
   "_index": "incident_db",
   "_type": "incidents",
   "_source": {
      "incident": {
         "name": "something",
         "severity": 3
      }
   }
}

推荐答案

为什么不简单地使用 条款聚合,如下所示:

Why not simply using a terms aggregation like this:

POST /incident_db/_search
{
  "size": 0,
  "aggs": {
    "counts": {
      "terms": {
        "field": "incident.severity"
      }
    }
  }
}

这篇关于根据出现次数进行计数-ElasticSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 16:17