Hi all,
I'm trying to map the aggregations returned by ES through NEST into our own model, but I see that those aggregates within NEST are defined as being of type IAggregate.
Since different types of aggregates (for example BucketAggregate, MatrixAggregate, etc) have different properties (specifically, BucketAggregate has Items property, while the IAggregate interface does not have it), I coded an ugly conditional to check the actual type of the Aggregate, and mapping some properties or others depending on that type.
A similar thing happens with the Buckets themselves: different implementations of IBucket have different properties, so I need to check the type, and based on that, map some properties or other ones.
Something like the following:
if (aggr.Value.GetType() == typeof(Nest.BucketAggregate))
{
foreach (IBucket bucket in ((Nest.BucketAggregate) aggr.Value).Items)
{
if (bucket.GetType() == typeof(KeyedBucket<object>))
{
object valueKey = ((KeyedBucket<object>) bucket).Key;
This indeed works, but it seems a really bad looking code to me.
Is there a more elegant way of achieving the same thing without having to put those type conditions one by one?
Thanks!