public static AggregationBase GetDefaultAggregation(this TermNode node, IQueryVisitorContext context) { var elasticContext = context as IElasticQueryVisitorContext; if (elasticContext == null) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } string aggField = elasticContext.MappingResolver.GetAggregationsFieldName(node.Field); var property = elasticContext.MappingResolver.GetMappingProperty(node.Field, true); string timezone = !String.IsNullOrWhiteSpace(node.UnescapedBoost) ? node.UnescapedBoost: node.GetTimeZone(elasticContext.DefaultTimeZone); switch (node.GetOperationType()) { case AggregationType.Min: return(new MinAggregation("min_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type }, { "@timezone", timezone } } }); case AggregationType.Max: return(new MaxAggregation("max_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type }, { "@timezone", timezone } } }); case AggregationType.Avg: return(new AverageAggregation("avg_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type } } }); case AggregationType.Sum: return(new SumAggregation("sum_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type } } }); case AggregationType.Stats: return(new StatsAggregation("stats_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type } } }); case AggregationType.ExtendedStats: return(new ExtendedStatsAggregation("exstats_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type } } }); case AggregationType.Cardinality: return(new CardinalityAggregation("cardinality_" + node.GetOriginalField(), aggField) { Missing = node.GetProximityAsDouble(), PrecisionThreshold = node.GetBoostAsInt32() }); case AggregationType.TopHits: return(new TopHitsAggregation("tophits") { Size = node.GetProximityAsInt32() }); case AggregationType.Missing: return(new MissingAggregation("missing_" + node.GetOriginalField()) { Field = aggField }); case AggregationType.DateHistogram: return(GetDateHistogramAggregation("date_" + node.GetOriginalField(), aggField, node.UnescapedProximity, node.UnescapedBoost, context)); case AggregationType.Histogram: return(GetHistogramAggregation("histogram_" + node.GetOriginalField(), aggField, node.UnescapedProximity, node.UnescapedBoost, context)); case AggregationType.Percentiles: return(GetPercentilesAggregation("percentiles_" + node.GetOriginalField(), aggField, node.UnescapedProximity, node.UnescapedBoost, context)); case AggregationType.GeoHashGrid: var precision = GeoHashPrecision.Precision1; if (!String.IsNullOrEmpty(node.UnescapedProximity)) { Enum.TryParse(node.UnescapedProximity, out precision); } return(new GeoHashGridAggregation("geogrid_" + node.GetOriginalField()) { Field = aggField, Precision = precision, Aggregations = new AverageAggregation("avg_lat", null) { Script = new InlineScript($"doc['{node.Field}'].lat") } && new AverageAggregation("avg_lon", null) { Script = new InlineScript($"doc['{node.Field}'].lon") } }); case AggregationType.Terms: var agg = new TermsAggregation("terms_" + node.GetOriginalField()) { Field = aggField, Size = node.GetProximityAsInt32(), MinimumDocumentCount = node.GetBoostAsInt32(), Meta = new Dictionary <string, object> { { "@field_type", property?.Type } } }; if (agg.Size.HasValue && (agg.Size * 1.5 + 10) > MAX_BUCKET_SIZE) { agg.ShardSize = Math.Max((int)agg.Size, MAX_BUCKET_SIZE); } return(agg); } return(null); }
public static AggregationBase GetDefaultAggregation(this TermNode node, IQueryVisitorContext context) { var elasticContext = context as IElasticQueryVisitorContext; if (elasticContext == null) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } string field = elasticContext.GetNonAnalyzedFieldName(node.Field); var mapping = elasticContext.GetPropertyMapping(field); string timezone = !String.IsNullOrWhiteSpace(node.UnescapedBoost) ? node.UnescapedBoost: GetString(context, "TimeZone"); switch (node.GetOperationType()) { case AggregationType.Min: return(new MinAggregation("min_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() }, { "@timezone", timezone } } }); case AggregationType.Max: return(new MaxAggregation("max_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() }, { "@timezone", timezone } } }); case AggregationType.Avg: return(new AverageAggregation("avg_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() } } }); case AggregationType.Sum: return(new SumAggregation("sum_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() } } }); case AggregationType.Stats: return(new StatsAggregation("stats_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() } } }); case AggregationType.ExtendedStats: return(new ExtendedStatsAggregation("exstats_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() } } }); case AggregationType.Cardinality: return(new CardinalityAggregation("cardinality_" + node.GetOriginalField(), field) { Missing = node.GetProximityAsDouble() }); case AggregationType.Missing: return(new MissingAggregation("missing_" + node.GetOriginalField()) { Field = field }); case AggregationType.DateHistogram: return(GetDateHistogramAggregation("date_" + node.GetOriginalField(), field, node.Proximity, node.UnescapedBoost, context)); case AggregationType.Histogram: return(GetHistogramAggregation("histogram_" + node.GetOriginalField(), field, node.Proximity, node.UnescapedBoost, context)); case AggregationType.Percentiles: return(GetPercentilesAggregation("percentiles_" + node.GetOriginalField(), field, node.Proximity, node.UnescapedBoost, context)); case AggregationType.GeoHashGrid: var precision = GeoHashPrecision.Precision1; if (!String.IsNullOrEmpty(node.Proximity)) { Enum.TryParse(node.Proximity, out precision); } return(new GeoHashGridAggregation("geogrid_" + node.GetOriginalField()) { Field = field, Precision = precision, Aggregations = new AverageAggregation("avg_lat", null) { Script = new InlineScript($"doc['{node.Field}'].lat") } && new AverageAggregation("avg_lon", null) { Script = new InlineScript($"doc['{node.Field}'].lon") } }); case AggregationType.Terms: return(new TermsAggregation("terms_" + node.GetOriginalField()) { Field = field, Size = node.GetProximityAsInt32(), MinimumDocumentCount = node.GetBoostAsInt32(), Meta = new Dictionary <string, object> { { "@field_type", mapping?.Type?.ToString() } } }); } return(null); }