Multi Terms Aggregation Usage

edit

A multi-bucket value source based aggregation where buckets are dynamically built - one per unique set of values.

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

Fluent DSL example

edit
a => a
.MultiTerms("states", st => st
    .CollectMode(TermsAggregationCollectMode.BreadthFirst)
    .Terms(t => t.Field(f => f.Name), t => t.Field(f => f.NumberOfCommits).Missing(0))
    .MinimumDocumentCount(1)
    .Size(5)
    .ShardSize(100)
    .ShardMinimumDocumentCount(1)
    .ShowTermDocCountError(true)
    .Order(o => o
        .KeyAscending()
        .CountDescending()
    )
    .Meta(m => m
        .Add("foo", "bar")
    )
)

Object Initializer syntax example

edit
new MultiTermsAggregation("states")
{
    CollectMode = TermsAggregationCollectMode.BreadthFirst,
    Terms = new List<Term>
    {
        new() {Field = Field<Project>(f => f.Name) },
        new() {Field = Field<Project>(f => f.NumberOfCommits), Missing = 0 }
    },
    MinimumDocumentCount = 1,
    Size = 5,
    ShardSize = 100,
    ShardMinimumDocumentCount = 1,
    ShowTermDocCountError = true,
    Order = new List<TermsOrder>
    {
        TermsOrder.KeyAscending,
        TermsOrder.CountDescending
    },
    Meta = new Dictionary<string, object>
    {
        { "foo", "bar" }
    }
}

Example json output.

{
  "states": {
    "meta": {
      "foo": "bar"
    },
    "multi_terms": {
      "collect_mode": "breadth_first",
      "terms": [
        {
          "field": "name"
        },
        {
          "field": "numberOfCommits",
          "missing": 0
        }
      ],
      "min_doc_count": 1,
      "shard_min_doc_count": 1,
      "size": 5,
      "shard_size": 100,
      "show_term_doc_count_error": true,
      "order": [
        {
          "_key": "asc"
        },
        {
          "_count": "desc"
        }
      ]
    }
  }
}

Handling Responses

edit
response.ShouldBeValid();
var states = response.Aggregations.MultiTerms("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);
    item.KeyAsString.Should().NotBeNullOrEmpty();
}
states.Meta.Should().NotBeNull().And.HaveCount(1);
states.Meta["foo"].Should().Be("bar");