Handling Responses

edit

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 Responses

edit
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 Filter

edit

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

Fluent DSL example

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

Object Initializer syntax example

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

Example json output.

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

Handling Responses

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

Inline Script Filter

edit

Fluent DSL example

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

Object Initializer syntax example

edit
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 Responses

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