Exemplo n.º 1
0
        /// <summary>
        /// Aggregate resuts
        /// </summary>
        /// <param name="key">Key identifier of results</param>
        /// <param name="field">Id field uppon the aggregation will be used</param>
        /// <param name="aggType">Type of Aggregation</param>
        public FindRequest <T> Aggregation(string key, Expression <Func <T, object> > field, EnAggregationType aggType)
        {
            AggregationContainer aggr = null;

            switch (aggType)
            {
            case EnAggregationType.Sum:
                aggr = new SumAggregation(key, field);
                break;

            case EnAggregationType.Average:
                aggr = new AverageAggregation(key, field);
                break;

            case EnAggregationType.Min:
                aggr = new MinAggregation(key, field);
                break;

            case EnAggregationType.Max:
                aggr = new MaxAggregation(key, field);
                break;
            }
            if (aggr != null)
            {
                searchInfo.Aggregations.Add(key, aggr);
            }
            return(this);
        }
Exemplo n.º 2
0
        public static void HowToUseAggregationsInES(IElasticClient client,
                                                    SearchDescriptor <ProductListEsIndexModelExample> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", "") && new AverageAggregation("", "");

            //select x,count(1) from tb group by x
            sd = sd.Aggregations(a => a.Terms("terms", x => x.Field(m => m.IsGroup))).Size(1000);
            //select count(f) from tb where f is not null
            sd = sd.Aggregations(a => a.ValueCount("count", x => x.Field(m => m.IsGroup))).Size(1000);
            //最大值
            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup))).Size(1000);
            //最大值,最小值,平均值等统计数据
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));
            //直方图
            sd = sd.Aggregations(a => a.Histogram("price", x => x.Field("price").Interval(60)));
            //时间直方图
            sd = sd.Aggregations(a => a.DateHistogram("date", x => x.Field("date").Interval(new Time(TimeSpan.FromHours(1)))));
            //数值区域
            sd = sd.Aggregations(a => a.Range("range", x => x.Field("price").Ranges(r => r.From(10).To(20), r => r.From(30).To(40))));
            //日期区域
            sd = sd.Aggregations(a => a.DateRange("date_range", x => x.Field("date").Ranges(r => r.From(DateTime.Now.AddDays(-1)).To(DateTime.Now))));
            //ip区域
            sd = sd.Aggregations(a => a.IpRange("ip_range", x => x.Field("ip").Ranges(r => r.From("192.168.0.1").To("192.168.0.10"))));

            var response = client.Search <ProductListEsIndexModelExample>(x => sd);

            var stats = response.Aggregations.Stats("stats");
            //etc
        }
        public void It_should_throw_an_exception_when_the_type_is_not_a_numeric_type()
        {
            _DataTable.Rows.Add("Value-1", "Value-2");

            var exception = Assert.Throws <ArgumentException>(() => SumAggregation.Sum <DateTime>(_DataTable, ((0, 0), (0, 0))));

            Assert.AreEqual("The specified type is not a numeric type.", exception.Message);
        }
        public void It_should_throw_an_exception_when_the_range_tuple_does_not_specify_the_same_column()
        {
            _DataTable.Rows.Add("Value-1", "Value-2");

            var exception = Assert.Throws <ArgumentException>(() => SumAggregation.Sum <int>(_DataTable, ((0, 0), (1, 0))));

            Assert.AreEqual("The specified aggregation can only be performed on a single column.", exception.Message);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Aggregate resuts over a clause
        /// </summary>
        /// <param name="groupField">Field that will be used as term to group aggregation results</param>
        /// <param name="field">Id field uppon the aggregation will be used</param>
        /// <param name="aggType">Type of Aggregation</param>
        public FindRequest <T> TermAggregation(Expression <Func <T, object> > groupField, Expression <Func <T, object> > field, EnAggregationType aggType)
        {
            //Get term
            var termName          = Utils.ExpressionAttributeName(groupField);
            TermsAggregation term = null;

            if (!searchInfo.termDictionary.ContainsKey(termName))
            {
                term = new TermsAggregation(termName)
                {
                    Field = groupField
                };
                searchInfo.termDictionary.Add(termName, term);
            }
            else
            {
                term = searchInfo.termDictionary[termName];
            }

            if (term != null)
            {
                //if(agg)
                AggregationContainer aggr = null;
                string pKey = "";
                switch (aggType)
                {
                case EnAggregationType.Sum:
                    pKey = string.Format("{0}_sum", termName);
                    aggr = new SumAggregation(pKey, field);
                    break;

                case EnAggregationType.Average:
                    pKey = string.Format("{0}_avg", termName);
                    aggr = new AverageAggregation(pKey, field);
                    break;

                case EnAggregationType.Min:
                    pKey = string.Format("{0}_min", termName);
                    aggr = new MinAggregation(pKey, field);
                    break;

                case EnAggregationType.Max:
                    pKey = string.Format("{0}_max", termName);
                    aggr = new MaxAggregation(pKey, field);
                    break;
                }
                if (aggr != null)
                {
                    if (term.Aggregations == null)
                    {
                        term.Aggregations = new AggregationDictionary();
                    }
                    term.Aggregations.Add(pKey, aggr);
                }
            }
            return(this);
        }
Exemplo n.º 6
0
        public static void HowToUseAggregationsInES(this SearchDescriptor <EsExample.ProductListV2> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", Field.Create("")) && new AverageAggregation("", Field.Create(""));

            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup)));
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));

            var response = ElasticsearchClientManager.Instance.DefaultClient.CreateClient().Search <EsExample.ProductListV2>(x => sd);

            var stats = response.Aggs.Stats("stats");
            //etc
        }
        public void GetAggregatedValueTest()
        {
            List <Cell> cells = new List <Cell>();

            for (int i = 0; i < 10; i++)
            {
                var cell = new Mock <Cell>();
                cell.Setup(foo => foo.Content).Returns((double)i);
                cells.Add(cell.Object);
            }
            SumAggregation target   = new SumAggregation();
            double         expected = 45;
            double         actual;

            actual = (double)target.GetAggregatedValue(cells);
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 8
0
        public static void HowToUseAggregationsInES(this SearchDescriptor <ProductListEsIndexModel> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", "") && new AverageAggregation("", "");

            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup))).Size(1000);
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));
            //直方图
            sd = sd.Aggregations(a => a.Histogram("price", x => x.Field("price").Interval(60)));
            //时间直方图
            sd = sd.Aggregations(a => a.DateHistogram("date", x => x.Field("date").Interval(new Time(TimeSpan.FromHours(1)))));

            var response = ElasticsearchClientManager.Instance.DefaultClient.CreateClient().Search <ProductListEsIndexModel>(x => sd);

            var stats = response.Aggregations.Stats("stats");
            //etc
        }
Exemplo n.º 9
0
        static void HowToUseAggregationsInES(IElasticClient client,
                                             SearchDescriptor <EsIndexExample> sd)
        {
            var agg = new AggregationContainer();

            agg = new SumAggregation("", "") && new AverageAggregation("", "");

            //select x,count(1) from tb group by x
            sd = sd.Aggregations(a => a.Terms("terms", x => x.Field(m => m.IsGroup).Order(s => s.CountDescending()).Size(1000)));
            //select count(f) from tb where f is not null
            sd = sd.Aggregations(a => a.ValueCount("count", x => x.Field(m => m.IsGroup)));
            //最大值
            sd = sd.Aggregations(a => a.Max("max", x => x.Field(m => m.IsGroup)));
            //最大值,最小值,平均值等统计数据
            sd = sd.Aggregations(a => a.Stats("stats", x => x.Field(m => m.BrandId).Field(m => m.PIsRemove)));
            //直方图
            sd = sd.Aggregations(a => a.Histogram("price", x => x.Field("price").Interval(60)));
            //时间直方图
            sd = sd.Aggregations(a => a.DateHistogram("date", x => x.Field("date").Interval(new Time(TimeSpan.FromHours(1)))));
            //数值区域
            sd = sd.Aggregations(a => a.Range("range", x => x.Field("price").Ranges(r => r.From(10).To(20), r => r.From(30).To(40))));
            //日期区域
            sd = sd.Aggregations(a => a.DateRange("date_range", x => x.Field("date").Ranges(r => r.From(DateTime.Now.AddDays(-1)).To(DateTime.Now))));
            //ip区域
            sd = sd.Aggregations(a => a.IpRange("ip_range", x => x.Field("ip").Ranges(r => r.From("192.168.0.1").To("192.168.0.10"))));


            //sd = sd.SearchType(SearchType.QueryThenFetch);
            var response = client.Search <EsIndexExample>(x => sd);

            response.ThrowIfException();

            var terms         = response.Aggregations.Terms("group").Buckets;
            var count         = response.Aggregations.ValueCount("count").Value;
            var stats         = response.Aggregations.Stats("stats");
            var max           = response.Aggregations.Max("max").Value;
            var histogram     = response.Aggregations.Histogram("hist").Buckets.ToDictionary(x => x.Key, x => x.DocCount);
            var datehistogram = response.Aggregations.DateHistogram("date_hist").Buckets.ToDictionary(x => x.Date, x => x.DocCount);
            var range         = response.Aggregations.Range("range").Buckets.Select(x => new { x.From, x.To, x.DocCount });
            var date_range    = response.Aggregations.DateRange("date_range").Buckets.Select(x => new { x.From, x.To, x.DocCount });
            //etc
        }
        public void It_should_throw_an_exception_when_the_range_parameter_is_null()
        {
            var exception = Assert.Throws <ArgumentNullException>(() => SumAggregation.Sum <int>(_DataTable, string.Empty));

            Assert.AreEqual($"Value cannot be null. (Parameter 'range')", exception.Message);
        }
        public void It_should_throw_an_exception_when_the_table_parameter_is_null()
        {
            var exception = Assert.Throws <ArgumentNullException>(() => SumAggregation.Sum <int>(null, "A1:A1"));

            Assert.AreEqual($"Value cannot be null. (Parameter 'table')", exception.Message);
        }
        public void It_should_throw_an_exception_when_the_data_row_groupings_parameter_is_null()
        {
            var exception = Assert.Throws <ArgumentNullException>(() => SumAggregation.Sum <int>(null, 0));

            Assert.AreEqual($"Value cannot be null. (Parameter 'dataRowGroupings')", exception.Message);
        }
Exemplo n.º 13
0
        public override SearchRequest <PolicyDocument> BuildQuery()
        {
            var filters = new List <QueryContainer>();

            if (!string.IsNullOrWhiteSpace(query.FilterByAgentLogin))
            {
                filters.Add(new TermQuery
                {
                    Field = new Field("agentLogin.keyword"),
                    Value = query.FilterByAgentLogin
                });
            }

            if (!string.IsNullOrWhiteSpace(query.FilterByProductCode))
            {
                filters.Add(new TermQuery
                {
                    Field = new Field("productCode.keyword"),
                    Value = query.FilterByProductCode
                });
            }

            if (query.FilterBySalesDateStart != default || query.FilterBySalesDateEnd != default)
            {
                filters.Add(new DateRangeQuery
                {
                    Field = new Field("from"),
                    GreaterThanOrEqualTo = query.FilterBySalesDateStart,
                    LessThanOrEqualTo    = query.FilterBySalesDateEnd
                });
            }

            if (filters.Count == 0)
            {
                filters.Add(new MatchAllQuery());
            }

            var filter = new BoolQuery
            {
                Must = filters
            };

            var sumAmountAgg = new SumAggregation("total_premium", new Field("totalPremium"));

            var termAgg = new TermsAggregation("count_by_agent")
            {
                Field        = new Field("agentLogin.keyword"),
                Aggregations = sumAmountAgg
            };

            var filteredAgg = new FilterAggregation("agg_filter")
            {
                Filter       = filter,
                Aggregations = termAgg
            };


            return(new SearchRequest <PolicyDocument>
            {
                Aggregations = filteredAgg
            });
        }