ElasticSearch中的多个最大/最小聚合(C#Nest)

时间:2016-02-15 23:58:17

标签: c# elasticsearch max aggregation nest

我一直在这里搜索,但我没有找到类似的东西...但是如果它逃脱了我,我会道歉,我希望你能帮助找到正确的方向。

我正在寻找一种方法在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"
      }
   }
}

意思是我想在同一个声明中执行:

“sys_created_on”字段的最大和最小聚合值 并且 “sys_updated_on”字段的最大和最小聚合值

谢谢!

2 个答案:

答案 0 :(得分:1)

你想要的是Stats Aggregation

以下是输入/输出示例

<强> INPUT

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

<强>结果

{
  "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"
    }
  }
}

答案 1 :(得分:0)

我已经弄清楚了。如果有人有同样的疑问:

1)创建AggregationContainerDescriptor:

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

2)填写:

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)查询:

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

干杯!