Value Count Aggregationedit

A single-value metrics aggregation that counts the number of values that are extracted from the aggregated documents. These values can be extracted either from specific fields in the documents, or be generated by a provided script. Typically, this aggregator will be used in conjunction with other single-value aggregations. For example, when computing the avg one might be interested in the number of values the average is computed over.

value_count does not de-duplicate values, so even if a field has duplicates (or a script generates multiple identical values for a single document), each value will be counted individually.

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

Response:

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

The name of the aggregation (types_count above) also serves as the key by which the aggregation result can be retrieved from the returned response.

Scriptedit

Counting the values generated by a script:

response = client.search(
  index: 'sales',
  size: 0,
  body: {
    aggregations: {
      type_count: {
        value_count: {
          script: {
            source: "doc['type'].value"
          }
        }
      }
    }
  }
)
puts response
res, err := es.Search(
	es.Search.WithIndex("sales"),
	es.Search.WithBody(strings.NewReader(`{
	  "aggs": {
	    "type_count": {
	      "value_count": {
	        "script": {
	          "source": "doc['type'].value"
	        }
	      }
	    }
	  }
	}`)),
	es.Search.WithSize(0),
	es.Search.WithPretty(),
)
fmt.Println(res, err)
POST /sales/_search?size=0
{
  "aggs": {
    "type_count": {
      "value_count": {
        "script": {
          "source": "doc['type'].value"
        }
      }
    }
  }
}

This will interpret the script parameter as an inline script with the painless script language and no script parameters. To use a stored script use the following syntax:

response = client.search(
  index: 'sales',
  size: 0,
  body: {
    aggregations: {
      types_count: {
        value_count: {
          script: {
            id: 'my_script',
            params: {
              field: 'type'
            }
          }
        }
      }
    }
  }
)
puts response
res, err := es.Search(
	es.Search.WithIndex("sales"),
	es.Search.WithBody(strings.NewReader(`{
	  "aggs": {
	    "types_count": {
	      "value_count": {
	        "script": {
	          "id": "my_script",
	          "params": {
	            "field": "type"
	          }
	        }
	      }
	    }
	  }
	}`)),
	es.Search.WithSize(0),
	es.Search.WithPretty(),
)
fmt.Println(res, err)
POST /sales/_search?size=0
{
  "aggs": {
    "types_count": {
      "value_count": {
        "script": {
          "id": "my_script",
          "params": {
            "field": "type"
          }
        }
      }
    }
  }
}
NOTE
Because value_count is designed to work with any field it internally treats all values as simple bytes. Due to this implementation, if _value script variable is used to fetch a value instead of accessing the field directly (e.g. a "value script"), the field value will be returned as a string instead of it’s native format.

Histogram fieldsedit

When the value_count aggregation is computed on histogram fields, the result of the aggregation is the sum of all numbers in the counts array of the histogram.

For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:

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.index(
  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" }
    }
  }
}

For each histogram field the value_count aggregation will sum all numbers in the counts array <1>. Eventually, it will add all values for all histograms and return the following result:

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