Terms Aggregation Usageedit

Fluent DSL Exampleedit

s => s
.Aggregations(a => a
    .Terms("states", st => st
        .Field(p => p.State)
        .MinimumDocumentCount(2)
        .Size(5)
        .ShardSize(100)
        .ExecutionHint(TermsAggregationExecutionHint.Map)
        .Missing("n/a")
        .Script(ss => ss.Inline("'State of Being: '+_value").Lang("groovy"))
        .Order(TermsOrder.TermAscending)
        .Order(TermsOrder.CountDescending)
        .Meta(m => m
            .Add("foo", "bar")
        )
    )
)

Object Initializer Syntax Exampleedit

new SearchRequest<Project>
{
    Aggregations = new TermsAggregation("states")
    {
        Field = Field<Project>(p => p.State),
        MinimumDocumentCount = 2,
        Size = 5,
        ShardSize = 100,
        ExecutionHint = TermsAggregationExecutionHint.Map,
        Missing = "n/a",
        Script = new InlineScript("'State of Being: '+_value") { Lang = "groovy" },
        Order = new List<TermsOrder>
        {
            TermsOrder.TermAscending,
            TermsOrder.CountDescending
        },
        Meta = new Dictionary<string, object>
        {
            { "foo", "bar" }
        }
    }
}

Example json output. 

{
  "aggs": {
    "states": {
      "meta": {
        "foo": "bar"
      },
      "terms": {
        "field": "state",
        "min_doc_count": 2,
        "size": 5,
        "shard_size": 100,
        "execution_hint": "map",
        "missing": "n/a",
        "script": {
          "inline": "'State of Being: '+_value",
          "lang": "groovy"
        },
        "order": [
          {
            "_term": "asc"
          },
          {
            "_count": "desc"
          }
        ]
      }
    }
  }
}

Handling Responsesedit

response.IsValid.Should().BeTrue();
var states = response.Aggs.Terms("states");
states.Should().NotBeNull();
states.DocCountErrorUpperBound.Should().HaveValue();
states.SumOtherDocCount.Should().HaveValue();
states.Buckets.Should().NotBeNull();
states.Buckets.Count.Should().BeGreaterThan(0);

foreach (var item in states.Buckets)
{
    item.Key.Should().NotBeNullOrEmpty();
    item.DocCount.Should().BeGreaterOrEqualTo(1);
}

states.Meta.Should().NotBeNull().And.HaveCount(1);
states.Meta["foo"].Should().Be("bar");

Fluent DSL Exampleedit

s => s
.Aggregations(a => a
    .Terms("commits", st => st
        .Field(p => p.NumberOfCommits)
    )
)

Object Initializer Syntax Exampleedit

new SearchRequest<Project>
{
    Aggregations = new TermsAggregation("commits")
    {
        Field = Field<Project>(p => p.NumberOfCommits),
    }
}

Example json output. 

{
  "aggs": {
    "commits": {
      "terms": {
        "field": "numberOfCommits"
      }
    }
  }
}

Handling Responsesedit

response.IsValid.Should().BeTrue();
var commits = response.Aggs.Terms<int>("commits");
commits.Should().NotBeNull();
commits.DocCountErrorUpperBound.Should().HaveValue();
commits.SumOtherDocCount.Should().HaveValue();
commits.Buckets.Should().NotBeNull();
commits.Buckets.Count.Should().BeGreaterThan(0);

foreach (var item in commits.Buckets)
{
    item.Key.Should().BeGreaterThan(0);
    item.DocCount.Should().BeGreaterOrEqualTo(1);
}