值计数聚合编辑

一种 单值 指标聚合,用于计算从聚合文档中提取的值的数量。这些值可以从文档中的特定字段中提取,也可以由提供的脚本生成。通常,此聚合器将与其他单值聚合器结合使用。例如,在计算 avg 时,您可能会对计算平均值的数值数量感兴趣。

value_count 不会对值进行去重,因此即使字段中有重复值,每个值也会被单独计数。

resp = client.search(
    index="sales",
    size="0",
    body={"aggs": {"types_count": {"value_count": {"field": "type"}}}},
)
print(resp)
response = client.search(
  index: 'sales',
  size: 0,
  body: {
    aggregations: {
      types_count: {
        value_count: {
          field: 'type'
        }
      }
    }
  }
)
puts response
res, err := es.Search(
	es.Search.WithIndex("sales"),
	es.Search.WithBody(strings.NewReader(`{
	  "aggs": {
	    "types_count": {
	      "value_count": {
	        "field": "type"
	      }
	    }
	  }
	}`)),
	es.Search.WithSize(0),
	es.Search.WithPretty(),
)
fmt.Println(res, err)
POST /sales/_search?size=0
{
  "aggs" : {
    "types_count" : { "value_count" : { "field" : "type" } }
  }
}

响应

{
  ...
  "aggregations": {
    "types_count": {
      "value": 7
    }
  }
}

聚合的名称(上面的 types_count)也用作从返回的响应中检索聚合结果的键。

脚本编辑

如果您需要计算比单个字段中的值更复杂的内容,则应在 运行时字段 上运行聚合。

resp = client.search(
    index="sales",
    body={
        "size": 0,
        "runtime_mappings": {
            "tags": {
                "type": "keyword",
                "script": "\n        emit(doc['type'].value);\n        if (doc['promoted'].value) {\n          emit('hot');\n        }\n      ",
            }
        },
        "aggs": {"tags_count": {"value_count": {"field": "tags"}}},
    },
)
print(resp)
response = client.search(
  index: 'sales',
  body: {
    size: 0,
    runtime_mappings: {
      tags: {
        type: 'keyword',
        script: "\n        emit(doc['type'].value);\n        if (doc['promoted'].value) {\n          emit('hot');\n        }\n      "
      }
    },
    aggregations: {
      tags_count: {
        value_count: {
          field: 'tags'
        }
      }
    }
  }
)
puts response
POST /sales/_search
{
  "size": 0,
  "runtime_mappings": {
    "tags": {
      "type": "keyword",
      "script": """
        emit(doc['type'].value);
        if (doc['promoted'].value) {
          emit('hot');
        }
      """
    }
  },
  "aggs": {
    "tags_count": {
      "value_count": {
        "field": "tags"
      }
    }
  }
}

直方图字段编辑

当在 直方图字段 上计算 value_count 聚合时,聚合的结果是直方图的 counts 数组中所有数字的总和。

例如,对于以下存储不同网络的预聚合直方图(包含延迟指标)的索引

resp = client.index(
    index="metrics_index",
    id="1",
    body={
        "network.name": "net-1",
        "latency_histo": {
            "values": [0.1, 0.2, 0.3, 0.4, 0.5],
            "counts": [3, 7, 23, 12, 6],
        },
    },
)
print(resp)

resp = client.index(
    index="metrics_index",
    id="2",
    body={
        "network.name": "net-2",
        "latency_histo": {
            "values": [0.1, 0.2, 0.3, 0.4, 0.5],
            "counts": [8, 17, 8, 7, 6],
        },
    },
)
print(resp)

resp = client.search(
    index="metrics_index",
    size="0",
    body={
        "aggs": {
            "total_requests": {"value_count": {"field": "latency_histo"}}
        }
    },
)
print(resp)
response = client.index(
  index: 'metrics_index',
  id: 1,
  body: {
    'network.name' => 'net-1',
    latency_histo: {
      values: [
        0.1,
        0.2,
        0.3,
        0.4,
        0.5
      ],
      counts: [
        3,
        7,
        23,
        12,
        6
      ]
    }
  }
)
puts response

response = client.index(
  index: 'metrics_index',
  id: 2,
  body: {
    'network.name' => 'net-2',
    latency_histo: {
      values: [
        0.1,
        0.2,
        0.3,
        0.4,
        0.5
      ],
      counts: [
        8,
        17,
        8,
        7,
        6
      ]
    }
  }
)
puts response

response = client.search(
  index: 'metrics_index',
  size: 0,
  body: {
    aggregations: {
      total_requests: {
        value_count: {
          field: 'latency_histo'
        }
      }
    }
  }
)
puts response
{
	res, err := es.Index(
		"metrics_index",
		strings.NewReader(`{
	  "network.name": "net-1",
	  "latency_histo": {
	    "values": [
	      0.1,
	      0.2,
	      0.3,
	      0.4,
	      0.5
	    ],
	    "counts": [
	      3,
	      7,
	      23,
	      12,
	      6
	    ]
	  }
	}`),
		es.Index.WithDocumentID("1"),
		es.Index.WithPretty(),
	)
	fmt.Println(res, err)
}

{
	res, err := es.Index(
		"metrics_index",
		strings.NewReader(`{
	  "network.name": "net-2",
	  "latency_histo": {
	    "values": [
	      0.1,
	      0.2,
	      0.3,
	      0.4,
	      0.5
	    ],
	    "counts": [
	      8,
	      17,
	      8,
	      7,
	      6
	    ]
	  }
	}`),
		es.Index.WithDocumentID("2"),
		es.Index.WithPretty(),
	)
	fmt.Println(res, err)
}

{
	res, err := es.Search(
		es.Search.WithIndex("metrics_index"),
		es.Search.WithBody(strings.NewReader(`{
	  "aggs": {
	    "total_requests": {
	      "value_count": {
	        "field": "latency_histo"
	      }
	    }
	  }
	}`)),
		es.Search.WithSize(0),
		es.Search.WithPretty(),
	)
	fmt.Println(res, err)
}
PUT metrics_index/_doc/1
{
  "network.name" : "net-1",
  "latency_histo" : {
      "values" : [0.1, 0.2, 0.3, 0.4, 0.5],
      "counts" : [3, 7, 23, 12, 6] 
   }
}

PUT metrics_index/_doc/2
{
  "network.name" : "net-2",
  "latency_histo" : {
      "values" :  [0.1, 0.2, 0.3, 0.4, 0.5],
      "counts" : [8, 17, 8, 7, 6] 
   }
}

POST /metrics_index/_search?size=0
{
  "aggs": {
    "total_requests": {
      "value_count": { "field": "latency_histo" }
    }
  }
}

对于每个直方图字段,value_count 聚合将对 counts 数组 <1> 中的所有数字求和。最终,它将添加所有直方图的所有值,并返回以下结果

{
  ...
  "aggregations": {
    "total_requests": {
      "value": 97
    }
  }
}