public static AggregationBase GetDefaultAggregation(this GroupNode node, IQueryVisitorContext context) { var elasticContext = context as IElasticQueryVisitorContext; if (elasticContext == null) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } if (!node.HasParens || String.IsNullOrEmpty(node.Field) || node.Left != null) { return(null); } string field = elasticContext.MappingResolver.GetAggregationsFieldName(node.Field); var property = elasticContext.MappingResolver.GetMappingProperty(field, true); switch (node.GetOperationType()) { case AggregationType.DateHistogram: return(GetDateHistogramAggregation("date_" + node.GetOriginalField(), field, node.UnescapedProximity, node.UnescapedBoost ?? node.GetTimeZone(elasticContext.DefaultTimeZone), context)); case AggregationType.Histogram: return(GetHistogramAggregation("histogram_" + node.GetOriginalField(), field, 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 = 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: var agg = new TermsAggregation("terms_" + node.GetOriginalField()) { Field = field, 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); case AggregationType.TopHits: return(new TopHitsAggregation("tophits") { Size = node.GetProximityAsInt32() }); } return(null); }
public static AggregationBase GetDefaultAggregation(this GroupNode node, IQueryVisitorContext context) { var elasticContext = context as IElasticQueryVisitorContext; if (elasticContext == null) { throw new ArgumentException("Context must be of type IElasticQueryVisitorContext", nameof(context)); } if (!node.HasParens || String.IsNullOrEmpty(node.Field) || node.Left != null) { return(null); } string field = elasticContext.GetNonAnalyzedFieldName(node.Field); var mapping = elasticContext.GetPropertyMapping(field); switch (node.GetOperationType()) { 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.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); }