Variable Width Histogram Usage

edit

A multi-bucket aggregation similar to Histogram. However, the width of each bucket is not specified. Rather, a target number of buckets is provided and bucket intervals are dynamically determined based on the document distribution.

See the Elasticsearch documentation on multi terms aggregation for more detail.

Fluent DSL example

edit
a => a
.VariableWidthHistogram("commits", v => v
    .Field(f => f.NumberOfCommits)
    .Buckets(2)
    .InitialBuffer(2)
    .ShardSize(100)
    .Meta(m => m
        .Add("foo", "bar")
    ))

Object Initializer syntax example

edit
new VariableWidthHistogramAggregation("commits")
{
    Field = Field<Project>(f => f.NumberOfCommits),
    Buckets = 2,
    InitialBuffer = 2,
    ShardSize = 100,
    Meta = new Dictionary<string, object>
    {
        { "foo", "bar" }
    }
}

Example json output.

{
  "commits": {
    "meta": {
      "foo": "bar"
    },
    "variable_width_histogram": {
      "field": "numberOfCommits",
      "buckets": 2,
      "initial_buffer": 2,
      "shard_size": 100
    }
  }
}

Handling Responses

edit
response.ShouldBeValid();
var counts = response.Aggregations.VariableWidthHistogram("commits");
counts.Should().NotBeNull();
counts.Buckets.Should().HaveCountGreaterThan(0);
var firstBucket = counts.Buckets.First();
firstBucket.Key.Should().BeGreaterOrEqualTo(0);
firstBucket.Minimum.Should().BeGreaterOrEqualTo(0);
firstBucket.Maximum.Should().BeGreaterOrEqualTo(0);
firstBucket.DocCount.Should().BeGreaterOrEqualTo(1);
counts.Meta.Should().NotBeNull().And.HaveCount(1);
counts.Meta["foo"].Should().Be("bar");