如何从Elasticsearch中的一组对象中获取平均水平,最大,最小等



我正在使用elasticsearch dB存储基准的数据。

我需要计算和检索一组值的平均,最大,最小等。

我有类似的东西:

Index = "myindex"
Type = "mytype"

每个对象都有3个字段:

"load_time", "execution_time", "log_time".
然后,我运行基准测试,DB填充了数百个记录。现在,我将检索每个字段的平均值,最大和最小值。

我应该运行什么查询?我尝试了统计方面,但我总是得到"未经见的令牌异常" ...

您应该使用统计刻度(doc)

查询应该是:

{
    "query" : {
        "match_all" : {}
    },
    "facets" : {
        "statload_time" : {
            "statistical" : {
                "field" : "load_time"
            }
        },
        "statexec_time" : {
            "statistical" : {
                "field" : "execution_time"
            }
        },
        "statlog_time" : {
            "statistical" : {
                "field" : "log_time"
            }
        }
    }
}

正如文档所说:

统计数据包括计数,总数,正方和平均值 (平均),最小,最大,方差和标准偏差

索引几个文档后,查询的响应是:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ ]
  },
  "facets" : {
    "statload_time" : {
      "_type" : "statistical",
      "count" : 2,
      "total" : 7.0,
      "min" : 2.0,
      "max" : 5.0,
      "mean" : 3.5,
      "sum_of_squares" : 29.0,
      "variance" : 2.25,
      "std_deviation" : 1.5
    },
    "statexec_time" : {
      "_type" : "statistical",
      "count" : 2,
      "total" : 12.0,
      "min" : 5.0,
      "max" : 7.0,
      "mean" : 6.0,
      "sum_of_squares" : 74.0,
      "variance" : 1.0,
      "std_deviation" : 1.0
    },
    "statlog_time" : {
      "_type" : "statistical",
      "count" : 2,
      "total" : 16.0,
      "min" : 7.0,
      "max" : 9.0,
      "mean" : 8.0,
      "sum_of_squares" : 130.0,
      "variance" : 1.0,
      "std_deviation" : 1.0
    }
  }
}

最新更新