NEST elasticsearch多级聚合未产生期望的输出

时间:2018-11-26 11:51:06

标签: c# elasticsearch nest elasticsearch-dsl

我是弹性搜索和NEST的新手。

我正在编写一个多级聚合查询。我从直接elasticsearch获得的输出与nest elasticsearch不同。当我在C#NEST流利的dsl中编写以下查询时,我没有得到datehistogram的最终存储桶,而是得到了keyedbucket。

  'aggs': {
    'alert_type': { 'terms': { 'field': 'kind' } },
    'date': {
      'date_histogram': {
        'field': 'chat_date', 'interval': '1d',
        'time_zone': 'Europe/London', 'min_doc_count': 0
      },
      'aggs': {
        'alerts': {
          'terms': {
            'field': 'kind', 'size': 50,
            'order': { '_key': 'asc' }
          }
        }
      }
    }
  }

输出:

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 399,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "date": {
            "buckets": [
                {
                    "key_as_string": "1980-01-01T00:00:00.000Z",
                    "key": 315532800000,
                    "doc_count": 4,
                    "alerts": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Email",
                                "doc_count": 4
                            }
                        ]
                    }
                }
            ]
        },
        "alerts": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": []
        }       
    }
}

C#流利的dsl:

public Func<AggregationContainerDescriptor<T>, IAggregationContainer> Aggregate<T>(
    AggregateCriteria aggregate
    ) where T : class
{
    Func<AggregationContainerDescriptor<T>, IAggregationContainer> aggregateDescriptor = agg => agg;

    aggregateDescriptor = agg => agg.DateHistogram($"{aggregate.Name}" d => d
                                            .Field(new Field(aggregate.GroupBy, null))
                                            .Interval((DateInterval)aggregate.Interval)
                                            .TimeZone(aggregate.TimeZone)
                                            .MinimumDocumentCount(aggregate.MinimumDocCount)
                                            .Aggregations(x => x
                                                .Terms(aggregate.InnerAggregate.Name, t1 => t1
                                                    .Field(new Field(aggregate.InnerAggregate.GroupBy, null))
                                                    .Order(o => o.KeyAscending())
                                                    .Size(50))
                                             ));

    return aggregateDescriptor;
}   

0 个答案:

没有答案