Handling Responsesedit

Using the .Aggs aggregation helper we can fetch our aggregation results easily in the correct type. Be sure to read more about .Aggs vs .Aggregations

Handling Responsesedit

response.ShouldBeValid();

var filterAgg = response.Aggs.Filter("bethels_projects");
filterAgg.Should().NotBeNull();
filterAgg.DocCount.Should().BeGreaterThan(0);
var tags = filterAgg.Terms("project_tags");
tags.Should().NotBeNull();
tags.Buckets.Should().NotBeEmpty();

Empty Filteredit

When the collection of filters is empty or all are conditionless, NEST will serialize them to an empty object.

Fluent DSL exampleedit

s => s
.Aggregations(aggs => aggs
    .Filter("empty_filter", date => date
        .Filter(f => f
            .Bool(b => b
                .Filter(new QueryContainer[0])
            )
        )
    )
)

Object Initializer syntax exampleedit

new SearchRequest<Project>
{
    Aggregations = new FilterAggregation("empty_filter")
    {
        Filter = new BoolQuery
        {
            Filter = new List<QueryContainer>()
        }
    }
}

Example json output.

{
  "aggs": {
    "empty_filter": {
      "filter": {}
    }
  }
}

Handling Responsesedit

response.ShouldBeValid();
response.Aggs.Filter("empty_filter").DocCount.Should().BeGreaterThan(0);

Inline Script Filteredit

Fluent DSL exampleedit

s => s
.Aggregations(aggs => aggs
    .Filter(_aggName, date => date
        .Filter(f => f
            .Script(b => b
                .Inline(_ctxNumberofCommits)
            )
        )
    )
)

Object Initializer syntax exampleedit

new SearchRequest<Project>
{
    Aggregations = new FilterAggregation(_aggName)
    {
        Filter = new ScriptQuery
        {
            Inline = _ctxNumberofCommits
        }
    }
}

Example json output.

{
  "aggs": {
    "script_filter": {
      "filter": {
        "script": {
          "script": {
            "inline": "_source.numberOfCommits > 0"
          }
        }
      }
    }
  }
}

Handling Responsesedit

response.ShouldBeValid();
response.Aggs.Filter(_aggName).DocCount.Should().BeGreaterThan(0);