Parent Aggregation Usage

edit

A special single bucket aggregation that selects parent documents that have the specified type, as defined in a join field.

Be sure to read the Elasticsearch documentation on Parent Aggregation.

Fluent DSL example

edit
a => a
.Parent<Project>("name_of_parent_agg", parent => parent 
    .Aggregations(parentAggs => parentAggs
        .Average("average_commits", avg => avg.Field(p => p.NumberOfCommits))
        .Max("max_commits", avg => avg.Field(p => p.NumberOfCommits))
        .Min("min_commits", avg => avg.Field(p => p.NumberOfCommits))
    )
)

sub-aggregations are on the type determined from the generic type parameter. In this example, the search is against CommitActivity type and Project is a parent of CommitActivity

Object Initializer syntax example

edit
new ParentAggregation("name_of_parent_agg", typeof(CommitActivity)) 
{
    Aggregations =
        new AverageAggregation("average_commits", Field<Project>(f => f.NumberOfCommits)) 
        && new MaxAggregation("max_commits", Field<Project>(f => f.NumberOfCommits))
        && new MinAggregation("min_commits", Field<Project>(f => f.NumberOfCommits))
}

join field is determined from the child type. In this example, it is CommitActivity

sub-aggregations are on the type determined from the join field. In this example, a Project is a parent of CommitActivity

Example json output.

{
  "size": 0,
  "aggs": {
    "name_of_parent_agg": {
      "parent": {
        "type": "commits"
      },
      "aggs": {
        "average_commits": {
          "avg": {
            "field": "numberOfCommits"
          }
        },
        "max_commits": {
          "max": {
            "field": "numberOfCommits"
          }
        },
        "min_commits": {
          "min": {
            "field": "numberOfCommits"
          }
        }
      }
    }
  }
}

Handling Responses

edit
response.ShouldBeValid();

var parentAgg = response.Aggregations.Parent("name_of_parent_agg");
parentAgg.Should().NotBeNull();
parentAgg.DocCount.Should().BeGreaterThan(0);
parentAgg.Min("average_commits").Should().NotBeNull();
parentAgg.Min("min_commits").Should().NotBeNull();
parentAgg.Max("max_commits").Should().NotBeNull();