Paulo Henrique PH
Paulo Henrique PH

Reputation: 361

Multiple max/min aggregation in ElasticSearch (C# Nest)

I have been searching here and I didn't find anything similar... However I apologize in advanced if it have escaped me, and I hope you can help out finding the correct direction.

I was looking for a way to implement the following in NEST C#:

"aggs": {

  "sys_created_on_max": {
     "max": {
       "field": "sys_created_on"
      }
   },
   "sys_created_on_min":{
      "min": {
        "field": "sys_created_on"
      }
   },
  "sys_updated_on_max": {
     "max": {
       "field": "sys_updated_on"
      }
   },
   "sys_updated_on_min":{
      "min": {
        "field": "sys_updated_on"
      }
   }
}

Meaning that I want to perform, in the same statement:

Max and Min aggregated value for "sys_created_on" field and also Max and Min aggregated value for "sys_updated_on" field

Thanks!

Upvotes: 0

Views: 1480

Answers (2)

Paulo Henrique PH
Paulo Henrique PH

Reputation: 361

I've figured it out. In case of someone have the same doubt:

1) create a AggregationContainerDescriptor:

Func<AggregationContainerDescriptor<dynamic>, IAggregationContainer> aggregationsSelector = null;

2) Fill it up:

foreach (var field in requestList)
{
    aggregationsSelector += ms => ms.Max(field.MaxAggregationAlias, mx => mx.Field(field.Name))
                                    .Min(field.MinAggregationAlias, mx => mx.Field(field.Name));

}

3) Query it:

var esResponse = _esClient.Raw.Search<dynamic>(indexName.ToLower(), new PostData<dynamic>(jsonStr), null);

Cheers!

Upvotes: 0

jhilden
jhilden

Reputation: 12449

What you want is Stats Aggregation.

Here is an example input/output

INPUT

GET devdev/redemption/_search
{
  "size": 0,
  "aggs": {
    "a1": {
      "stats": {
        "field": "reporting.campaign.endDate"
      }
    }
  }
}

Result

{
  "took": 97,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 146,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "a1": {
      "count": 11,
      "min": 1443675599999,
      "max": 1446353999999,
      "avg": 1445607818180.818,
      "sum": 15901685999989,
      "min_as_string": "1443675599999",
      "max_as_string": "1446353999999",
      "avg_as_string": "1445607818180",
      "sum_as_string": "15901685999989"
    }
  }
}

Upvotes: 1

Related Questions