I am using NEST 2.3.2. And I am trying to build a query with nested aggregations. Basically, I have an index with logs, which have a timestamp and a result code. I want to first put those logs into buckets of minutes, and then further classify them according to result code.
I have the following F# code for generating the query.
/// Generate an aggregation to put buckets by result code
let generateAggregationByResultCode () =
let resultAggregationName = "result_aggregation"
let aggregationByResults = new TermsAggregation(resultAggregationName)
aggregationByResults.Field <- new Field(Name = "Result")
aggregationByResults.ExecutionHint <- new Nullable<TermsAggregationExecutionHint>(TermsAggregationExecutionHint.GlobalOrdinals);
aggregationByResults.MinimumDocumentCount <- new Nullable<int>(0);
aggregationByResults.Size <- new Nullable<int>(bucketSize);
aggregationByResults.Missing <- "-128"
aggregationByResults
/// Generate an aggregation to classify into buckets by minutes and then by result code
let generateNewDateHistogramByMinute () =
let dateHistogramByMinute = new DateHistogramAggregation("by_minute")
dateHistogramByMinute.Field <- new Field(Name = "OperationTime")
dateHistogramByMinute.Interval <- new Union<DateInterval, Time>(DateInterval.Minute) // can also use TimeSpan.FromMinutes(1.0)
dateHistogramByMinute.MinimumDocumentCount <- new Nullable<int>(0)
dateHistogramByMinute.Format <- "strict_date_hour_minute"
let innerAggregations = new AggregationDictionary()
innerAggregations.[resultInnerAggregationName] <- new AggregationContainer(Terms = generateAggregationByResultCode ())
dateHistogramByMinute.Aggregations <- innerAggregations
dateHistogramByMinute
I use this aggregation to set the request by
let dateHistogram = generateNewDateHistogramByMinute ()
let aggregations = new AggregationDictionary()
aggregations.[histogramName] <- new AggregationContainer(DateHistogram = dateHistogram)
(* ... code omitted ... *)
dslRequest.Aggregations <- aggregations
When I print out the request, the aggregation part is like this
"aggs": {
"BucketsByMinutes": {
"date_histogram": {
"field": "OperationTime",
"interval": "minute",
"format": "strict_date_hour_minute",
"min_doc_count": 0
}
}
}
The inner aggregation is completely lost. Does anyone know how should I construct a request properly? And how do I retrieve that inner buckets when the response is returned? I didn't find appropriate properties or methods for that, and the documentation is basically non-existent.