public SearchResult <Customer> GetCustomer(CustomerSearchRequest customerSearchRequest) { var filters = new List <Func <QueryContainerDescriptor <Customer>, QueryContainer> >(); if (customerSearchRequest.filters?.Count > 0) { foreach (Filter filter in customerSearchRequest.filters) { filters.Add(fq => fq.Term(t => t.Field(filter.fieldName).Value(filter.fieldValue))); } } AggregationBase agg = new TermsAggregation("school.suburb") { Field = Nest.Infer.Field <Customer>(p => p.school.suburb) } && new TermsAggregation("school.suburb") { Field = Nest.Infer.Field <Customer>(p => p.school.state) }; var response = EsCoreOperation.SearchClient.Search <Customer>(NewMethod2(customerSearchRequest, filters)); var fileter = new List <BrandLink.Extensions.ElasticSearch.Operations.Model.Request.Filter>(); foreach (Filter filter in customerSearchRequest.filters) { fileter.Add(new BrandLink.Extensions.ElasticSearch.Operations.Model.Request.Filter() { FieldName = filter.fieldName, FieldValue = filter.fieldValue }); } SearchParams searchRequest = new SearchParams() { SearchTerm = customerSearchRequest.searchTerm, Filters = fileter }; var responseNew = SearchConfiguration.CustomerSet.Query(searchRequest); var responseNew1 = SearchConfiguration.CustomerSet.Query(() => NewMethod3(customerSearchRequest, filters)); var searchResult = new SearchResult() { Total = response.Total, Page = customerSearchRequest.pageIndex, PageSize = customerSearchRequest.pageSize, Results = (List <Customer>)response.Documents }; searchResult.aggregations = BuildAggregations(response.Aggregations); return(responseNew); }
public static TermsAggregation TermsAggregation <T>(Expression <Func <T, object> > fieldExpression, AggsOrderBy orderBy, AggsOrderDirection direction, int?size = null, params IAggregation[] statAggregation) where T : new() { var fieldName = ExpressionHelper.GetPropertyName(fieldExpression); var termA = new TermsAggregation(fieldName, size, new AggsOrder(orderBy, direction)); foreach (var sa in statAggregation) { if (termA.Aggregations == null) { termA.Aggregations = new Dictionary <string, IAggregation>(); } if (sa.IsSubclassOfRawGeneric(typeof(RangeAggregation <>)) || sa is TermsAggregation) { termA.Aggregations["nested"] = sa; } else { termA.Aggregations[sa.GetType().Name.Replace("Aggregation", "")] = sa; } } return(termA); }
public Tuple <string, List <BoardGame> > QueryData_Buckets(string query) { _elasticClient.Flush("boardgames"); var queryContainer = new QueryContainer(); queryContainer &= new MatchAllQuery(); var aggregation = new TermsAggregation("category_aggregation") { Field = Infer.Field <BoardGame>(p => p.Category), }; var searchRequest = new SearchRequest("boardgames", typeof(BoardGame)) { Query = queryContainer, Aggregations = aggregation }; var searchResponse = _elasticClient.Search <BoardGame>(searchRequest); var categories = searchResponse.Aggregations.Terms("category_aggregation").Buckets; return(new Tuple <string, List <BoardGame> >( searchResponse.DebugInformation, searchResponse.Documents.ToList() )); }
/// <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); }
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 termAgg = new TermsAggregation("count_by_product") { Field = new Field("productCode.keyword"), Aggregations = new SumAggregation("total_premium", new Field("totalPremium")) }; var filteredAgg = new FilterAggregation("agg_filter") { Filter = filter, Aggregations = termAgg }; return(new SearchRequest <PolicyDocument> { Aggregations = filteredAgg }); }
public static TermsAggregation MakeTermsAggQuery(string field, string name = "", bool keyword = false) { if (name == "") { name = field; } TermsAggregation termsAggregation = new TermsAggregation(name) { Field = field + (keyword ? ".keyword" : "") }; return(termsAggregation); }
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); }
private static void AddTermAggregationRequest(IDictionary <string, AggregationContainer> container, string aggregationId, string field, QueryContainer filter, TermAggregationRequest termAggregationRequest) { var facetSize = termAggregationRequest.Size; TermsAggregation termsAggregation = null; if (!string.IsNullOrEmpty(field)) { termsAggregation = new TermsAggregation(aggregationId) { Field = field, Size = facetSize == null ? null : facetSize > 0 ? facetSize : int.MaxValue, }; if (termAggregationRequest.Values?.Any() == true) { termsAggregation.Include = new TermsIncludeExclude { Values = termAggregationRequest.Values }; } } if (filter == null) { if (termsAggregation != null) { container.Add(aggregationId, termsAggregation); } } else { var filterAggregation = new FilterAggregation(aggregationId) { Filter = filter, }; if (termsAggregation != null) { filterAggregation.Aggregations = termsAggregation; } container.Add(aggregationId, filterAggregation); } }
/// <summary> /// Gets the simple aggregation "query" /// </summary> /// <returns>An AggregationDictionary containing the "query"</returns> /// <param name="facetConfig">Configuration for the field to aggregate</param> /// <param name="query"></param> private AggregationDictionary GetAggQuery(R4RAPIOptions.FacetConfig facetConfig, ResourceQuery query) { // If we *really* need the parentKey for this facet, then we must add it to the aggregation. // however, we may not really need it. var keyLabelAggregate = new TermsAggregation($"{facetConfig.FilterName}_key") { Field = new Field($"{facetConfig.FilterName}.key"), //Set the field to rollup Size = 999, //Use a large number to indicate unlimted (def is 10) // Now, we need to get the labels for the keys and thus // we need to add a sub aggregate for this term. // Normally you would do this for something like city/state rollups Aggregations = new TermsAggregation($"{facetConfig.FilterName}_label") { Field = new Field($"{facetConfig.FilterName}.label") } }; AggregationDictionary aggBody = keyLabelAggregate; // This facet requires a parent and thus needs a filter aggregate // to wrap the keyLabelAggregate if (!String.IsNullOrWhiteSpace(facetConfig.RequiresFilter)) { aggBody = new FilterAggregation($"{facetConfig.FilterName}_filter") { Filter = this.GetQueryForFilterField($"{facetConfig.FilterName}.parentKey", query.Filters[facetConfig.RequiresFilter]), Aggregations = keyLabelAggregate }; } //Start with a nested aggregation var agg = new NestedAggregation($"{facetConfig.FilterName}_agg") { Path = new Field(facetConfig.FilterName), //Set the path of the nested agg // Add the sub aggregates (bucket keys) Aggregations = aggBody }; return(agg); }
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 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 ISearchResponse <T> GetResponseOfAggs <T>(TermsAggregation termsAggregation) where T : class { return(Client.Search <T>(s => s.Index(IndexName).Aggregations( termsAggregation))); }