private static AggregationBase GetDateHistogramAggregation(string originalField, string field, string proximity, string boost, IQueryVisitorContext context) { // NOTE: StartDate and EndDate are set in the Repositories QueryBuilderContext. var start = GetDate(context, "StartDate"); var end = GetDate(context, "EndDate"); bool isValidRange = start.HasValue && start.Value > DateTime.MinValue && end.HasValue && end.Value < DateTime.MaxValue && start.Value <= end.Value; var bounds = isValidRange ? new ExtendedBounds <DateMath> { Minimum = start.Value, Maximum = end.Value } : null; var interval = GetInterval(proximity, start, end); string timezone = TryConvertTimeUnitToUtcOffset(boost); var agg = new DateHistogramAggregation(originalField) { Field = field, MinimumDocumentCount = 0, Format = "date_optional_time", TimeZone = timezone, Meta = !String.IsNullOrEmpty(boost) ? new Dictionary <string, object> { { "@timezone", boost } } : null, ExtendedBounds = bounds }; interval.Match(d => agg.CalendarInterval = d, f => agg.FixedInterval = f); return(agg); }
/// <inheritdoc/> public void Visit(DateHistogramAggregation dateHistogramAggregation) { Ensure.IsNotNull(dateHistogramAggregation, nameof(dateHistogramAggregation)); EnsureClause.StringIsNotNullOrEmpty(dateHistogramAggregation.Metric, nameof(dateHistogramAggregation.Metric)); EnsureClause.StringIsNotNullOrEmpty(dateHistogramAggregation.FieldName, nameof(dateHistogramAggregation.FieldName)); dateHistogramAggregation.KustoQL = $"{dateHistogramAggregation.Metric} by {dateHistogramAggregation.FieldName} = "; if (!string.IsNullOrEmpty(dateHistogramAggregation.Interval)) { var period = dateHistogramAggregation.Interval[^ 1];
public override SearchRequest <PolicyDocument> BuildQuery() { var filters = new List <QueryContainer>(); 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 histogram = new DateHistogramAggregation("sales") { Field = new Field("from"), Interval = new Union <DateInterval, Time>(query.AggregationUnit.ToDateInterval()), Aggregations = new SumAggregation("total_premium", new Field("totalPremium")) }; var filteredAgg = new FilterAggregation("agg_filter") { Filter = filter, Aggregations = histogram }; return(new SearchRequest <PolicyDocument> { Aggregations = filteredAgg }); }
public string DateHistogramVisit_WithSimpleAggregation_ReturnsValidResponse() { var histogramAggregation = new DateHistogramAggregation() { Metric = "wibble", FieldName = "wobble", }; var visitor = new ElasticSearchDSLVisitor(SchemaRetrieverMock.CreateMockSchemaRetriever()); visitor.Visit(histogramAggregation); return(histogramAggregation.KustoQL); }
public AggregationDictionary ObterAgrupamentoNest(IEnumerable <GroupBy> agrupamentos, AggregationBase aggregations = null) { foreach (var aggr in agrupamentos) { //Tipos de agrupamento if (aggr.PropertyType == typeof(DateTime) || aggr.PropertyType == typeof(DateTime?)) { var dateHistAgg = new DateHistogramAggregation(aggr.Field) { Missing = (DateTime?)null,//(DateTime?)aggr.Missing, Field = aggr.Field, //Aggregations = ((aggregations != null) ? aggregations : null), Interval = DateInterval.Day, MinimumDocumentCount = 1, //Script = (!string.IsNullOrWhiteSpace(aggr.Script)) ? new InlineScript(aggr.Script) : null }; if (aggregations != null) { dateHistAgg.Aggregations = aggregations; } aggregations = dateHistAgg; } else { var termsAgg = new TermsAggregation(aggr.Field) { Field = aggr.Field, //Aggregations = ((aggregations != null) ? aggregations : null), Size = int.MaxValue, //Missing = null,//aggr.Missing, MinimumDocumentCount = 1, //Script = (!string.IsNullOrWhiteSpace(aggr.Script)) ? new InlineScript(aggr.Script) : null }; if (aggregations != null) { termsAgg.Aggregations = aggregations; } aggregations = termsAgg; } } return(aggregations); }
/// <inheritdoc/> public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jo = JObject.Load(reader); var obj = new DateHistogramAggregation { FieldName = (string)jo["field"], Interval = (string)jo["interval"], }; return(obj); }