public void Aggregations_Nested_Terms_Range()
        {
            var q1 = new Query <IndexedClass>().NestedAggregation(
                ic => ic.Children,
                AggregationsHelper.TermsAggregation <IndexedClass>(x => x.Children.PropertyName(p => p.AString), 10,
                                                                   new AggsOrder(AggsOrderBy.Key, AggsOrderDirection.Desc),
                                                                   AggregationsHelper.SumAggregation <IndexedClass>(sum => sum.SomeNumber)),
                AggregationsHelper.RangeAggregation <IndexedClass, int?>(
                    x => x.Children.PropertyName(p => p.SomeNumber), new RangeAggrValue <int?>
            {
                Key = "SomeKey",
                To  = 24
            }),
                AggregationsHelper.RangeAggregation <IndexedClass, int?>(
                    x => x.Children.PropertyName(p => p.SomeNumber),
                    new RangeAggrValue <int?>
            {
                Key = "SomeKey1",
                To  = 12,
            },
                    new RangeAggrValue <int?>
            {
                Key  = "SomeKey2",
                From = 31,
            })
                );

            var json = q1.ToJson();

            Assert.AreEqual(
                "{\"query\":{\"bool\":{\"should\":[{\"type\":{\"value\":\"h73.Elastic.Search.Tests.IndexedClass\"}},{\"type\":{\"value\":\"h73.Elastic.Search.Tests.InheritedGenericIndexedClass`1\"}},{\"type\":{\"value\":\"h73.Elastic.Search.Tests.InheritedIndexedClass\"}}],\"minimum_should_match\":1}},\"aggs\":{\"nested_Children_0\":{\"nested\":{\"path\":\"Children\"},\"aggs\":{\"nested\":{\"terms\":{\"field\":\"Children.AString\",\"size\":10,\"order\":{\"_key\":\"desc\"}},\"aggs\":{\"Sum\":{\"sum\":{\"field\":\"SomeNumber\"}},\"nested\":{\"range\":{\"ranges\":[{\"key\":\"SomeKey\",\"to\":24}],\"field\":\"Children.SomeNumber\"},\"aggs\":{\"nested\":{\"range\":{\"ranges\":[{\"key\":\"SomeKey1\",\"to\":12},{\"key\":\"SomeKey2\",\"from\":31}],\"field\":\"Children.SomeNumber\"}}}}}}}}}}",
                json);
        }
        public void Aggregations_Sum()
        {
            var q1 = new Query <IndexedClass>().TermsAggregation(x => x.AString, 5,
                                                                 AggregationsHelper.SumAggregation <IndexedClass>(sum => sum.SomeNumber));
            var          json = q1.ToJson();
            const string r1   =
                "{\"query\":{\"bool\":{\"should\":[{\"type\":{\"value\":\"h73.Elastic.Search.Tests.IndexedClass\"}}," +
                "{\"type\":{\"value\":\"h73.Elastic.Search.Tests.InheritedGenericIndexedClass`1\"}}," +
                "{\"type\":{\"value\":\"h73.Elastic.Search.Tests.InheritedIndexedClass\"}}],\"minimum_should_match\":1}}," +
                "\"aggs\":{\"terms_AString\":{\"terms\":{\"field\":\"AString\",\"size\":5},\"aggs\":{\"Sum\":{\"sum\":{\"field\":\"SomeNumber\"}}}}}}";

            Assert.AreEqual(r1, json);
        }
        public void FilterAggregation_With_Nested()
        {
            var queryAsFilter = new Query <IndexedClass>(true).Term("TEST", ic => ic.AString, BooleanQueryType.MustNot)
                                .CreateBooleanQueryRoot();
            var queryagg = new Query <IndexedClass>(true).TermsAggregation(x => x.AString, 5,
                                                                           AggregationsHelper.SumAggregation <IndexedClass>(sum => sum.SomeNumber));
            var q = new Query <IndexedClass>(true).FilterAggregation(queryAsFilter, queryagg.Aggregations);

            var json = q.ToJson();

            Assert.AreEqual(
                "{\"query\":{\"bool\":{}},\"aggs\":{\"agg_filter_0\":{\"aggs\":{\"terms_AString\":{\"terms\":" +
                "{\"field\":\"AString\",\"size\":5},\"aggs\":{\"Sum\":{\"sum\":{\"field\":\"SomeNumber\"}}}}}," +
                "\"filter\":{\"bool\":{\"must_not\":[{\"term\":{\"AString\":\"TEST\"}}]}}}}}", json);
        }
        public void AddAggregation_Helper()
        {
            var q = new Query <Customer>(true).SetSize(0);

            var aggr = AggregationsHelper
                       .DateHistogram <Customer>(ic => ic.Created, "week",
                                                 order: new AggsOrder(AggsOrderBy.Key, AggsOrderDirection.Desc))
                       .TermsAggregation("_id", order: new AggsOrder("sum", AggsOrderDirection.Desc));

            aggr.Aggregations["nested"].Add("sum",
                                            AggregationsHelper.SumAggregation <Customer>(ic =>
                                                                                         ic.Age));
            aggr.Aggregations["nested"].Add("above25percent_filter",
                                            AggregationsHelper.BucketSelector("sum", "sum", "params.sum > 180"));

            q.AddAggregation(aggr);

            var result = new DocumentSearch <Customer>().Search(_client, q);
            var b      = result.Aggregations["agg_DateHistogram"].Buckets.Where(x => x.Nested.Buckets.Any());

            Assert.IsNotNull(b);
        }
Example #5
0
        public void Ren3_gt25percent()
        {
            var gt25percent = new Query <IndexedClass>(true).SetSize(0);
            var aggr        = AggregationsHelper
                              .DateHistogram <IndexedClass>(ic => ic.Children.PropertyName(c => c.DateTimeNotNullable), "week",
                                                            order: new AggsOrder(AggsOrderBy.Key, AggsOrderDirection.Desc))
                              .TermsAggregation("_id", order: new AggsOrder("sum", AggsOrderDirection.Desc));

            aggr.Aggregations["nested"].Add("sum",
                                            AggregationsHelper.SumAggregation <IndexedClass>(ic =>
                                                                                             ic.Children.PropertyName(c => c.DateTimeNotNullable)));
            aggr.Aggregations["nested"].Add("above25percent_filter",
                                            AggregationsHelper.BucketSelector("sum", "sum", "params.sum > 2520"));

            gt25percent.NestedAggregation(ic => ic.Children, aggr);
            Assert.AreEqual(
                "{\"query\":{\"bool\":{}},\"aggs\":{\"nested_Children_0\":{\"nested\":{\"path\":\"Children\"},\"aggs\":{\"nested\":{\"date_histogram\":" +
                "{\"interval\":\"week\",\"field\":\"Children.DateTimeNotNullable\",\"order\":{\"_key\":\"desc\"}},\"aggs\":{\"nested\":{\"terms\":" +
                "{\"field\":\"_id\",\"order\":{\"sum\":\"desc\"}},\"aggs\":{\"sum\":{\"sum\":{\"field\":\"Children.DateTimeNotNullable\"}},\"above25percent_filter\":" +
                "{\"bucket_selector\":{\"buckets_path\":{\"sum\":\"sum\"},\"script\":\"params.sum > 2520\"}}}}}}}}},\"size\":0}",
                gt25percent.ToJson());
        }