Elasticsearch-net: Bucket and KeyItem dto's have been removed.

Created on 27 Sep 2016  路  6Comments  路  Source: elastic/elasticsearch-net

I was curious what these were renamed too

        private static AggregationResult ToAggregationResult(this Bucket bucket, string field) {
            return new AggregationResult {
                Field = field,
                Terms = new AggregationDictionary<AggregationResult>(bucket.Items.OfType<KeyItem>().ToDictionary(t => t.Key, t => {
                    var termRes = new AggregationResult<AggregationResult> {
                        Total = t.DocCount
                    };

                    if (t.Aggregations?.Count > 0) {
                        termRes.Aggregations = new List<AggregationResult>();
                        foreach (var key in t.Aggregations.Keys) {
                            var nestedBucket = t.Aggregations[key] as Bucket;
                            if (nestedBucket == null)
                                continue;

                            termRes.Aggregations.Add(nestedBucket.ToAggregationResult(key));
                        }
                    }

                    return termRes;
                })),
            };
        }

        public static IReadOnlyCollection<AggregationResult> ToAggregationResult<T>(this ISearchResponse<T> response) where T : class {
            var result = new List<AggregationResult>();
            if (response.Aggregations == null || response.Aggregations.Count == 0)
                return result;

            foreach (var key in response.Aggregations.Keys) {
                var bucket = response.Aggregations[key] as Bucket;
                if (bucket == null)
                    continue;

                result.Add(bucket.ToAggregationResult(key));
            }

            return result;
        }

Most helpful comment

You can also use the https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/Aggregations/Visitor/AggregationVisitor.cs to do something more generic to a whole bunch of them.

All 6 comments

screen shot 2016-09-27 at 3 02 37 pm

Seems like IBucket interface should have DocCount? There is no way to do what I was doing before I think without doing a ton of casts.

KeyItem is now KeyedBucket

Bucket is now BucketAggregate, which is an unfortunate intermediate object we need for deserialization (see #1792). Just a heads up, if #2081 is ever merged, it'll be removed since we won't need it anymore.

Seems like IBucket interface should have DocCount?

Not all buckets in ES return a doc_count, so it would be a bad abstraction for it to be on the base of all bucket types in NEST.

You can also use the https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/Aggregations/Visitor/AggregationVisitor.cs to do something more generic to a whole bunch of them.

What about if you added an interface just for doc count, then you'd know which aggregates support it? (Update: we'd just use the visitor)

Thanks, all this code needs to be rewritten and we plan to use a visitor. In the mean time, I just need this to work the way it was :)

I like the idea of having some shared interfaces for things like doc_count so that we can do things at a more generic level for aggregations that have some commonality.

Was this page helpful?
0 / 5 - 0 ratings