Sublevel or Nested Aggregations


(jk) #1

I have a query where First level of aggregation is done for the field "Name" (fetches max('Id') and min('Id') from aggregation by Name) and sub aggregation is done for the field named 'IsInline'.
The below Fluent DSL is used which works perfectly fine.
searchResult = _ElasticClient.Search(x => x
.Index("MyIndex")
.Type("MyIndex")
.Query(p => +_commandFields.Query) //_commandFields.Query is a bool query
.Size(1000)
.Aggregations(a => a
.Terms("AggName", st => st
.Field("AssetType.Name")
.Size(0)
.Aggregations(aa => aa
.Max("max_id", m => m
.Field("Id")
)
.Min("min_id", m => m
.Field("Id")
)
.Terms("AggIsInline", st1 => st1
.Field("AssetType.IsInline")
.Size(0)
)
)
)
)
);

searchResult from the above code has the expected results.

The same query tried to execute using Object Intitializers as below.
Indices indices = Indices.Parse(_commandFields.IndexName.ToLower());
Nest.Types types = Nest.Types.Parse(_commandFields.Type);

                Dictionary<string, AggregationContainer> container1 = new Dictionary<string, AggregationContainer>();
                Dictionary<string, AggregationContainer> innerContainer1 = new Dictionary<string, AggregationContainer>();
                innerContainer1.Add("max_id", new AggregationContainer() { Max = new MaxAggregation("max_id", "Id") });

                AggregationContainer aggContainer1 = new AggregationContainer()
                {
                    Terms = new TermsAggregation("IsInline")
                    {
                        Field = new Field() { Name = "AssetType.IsInline" }
                        ,
                        Size = 0
                    }
                    ,
                    Aggregations = innerContainer1
                };
                container1.Add("name2", aggContainer1);


                Dictionary<string, AggregationContainer> container = new Dictionary<string, AggregationContainer>();
                Dictionary<string, AggregationContainer> innerContainer = new Dictionary<string, AggregationContainer>();
                innerContainer.Add("max", new AggregationContainer() { Max = new MaxAggregation("max_id", "Id") });
                innerContainer.Add("min", new AggregationContainer() { Min = new MinAggregation("min_id", "Id") }); 
                innerContainer.Add("IsInline", new AggregationContainer() { Aggregations = new AggregationDictionary(container1) });

AggregationContainer aggContainer = new AggregationContainer()
{
Terms = new TermsAggregation("name")
{
Field = new Field() { Name = "AssetType.Name" }
,
Size = 0
}
,
Aggregations=innerContainer
};
container.Add("name", aggContainer);

                var searchRequest = new SearchRequest(indices, types)
                {
                    Query = new QueryContainer(_commandFields.Query),
                    Size = 0, //_commandFields.Limit,
                    Preference = _commandFields.ReadPreference.GetDescription(),
                    Scroll = _scrollExpiryTime,
                    Aggregations = new AggregationDictionary(container), 
                };
                searchResult = _client.Search<dynamic>(searchRequest);

Gettting the attached error while the above lines of code is being executed.

Can someone help me with how the sub aggregation can be executed.

Version Used:
Elastic Search : 2.3.4
ElasticSearch.Net 2.0.0.0
Nest.dll 2.0.0.0

Thanks in advance.


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.