/// <summary> /// Get the Aggregated token count /// </summary> /// <param name="interpretedFields">DON'T PUT THE DOCUMENTELASTIC->TEXT FIELD HERE! RATHER THE INTERPRETEDFIELDS</param> /// <param name="ngramCount"></param> /// <param name="tagId">if it's null then the Aggregate will be run on all the DocumentElastic</param> /// <returns></returns> public int GetAllWordsOccurences(List <string> interpretedFields, int ngramCount, string tagField = null) { var desc = new SearchDescriptor <DocumentElastic>(); var aggDesc = new AggregationContainerDescriptor <DocumentElastic>(); foreach (var interpretedField in interpretedFields) { aggDesc.Sum( interpretedField, ad => ad.Field($"{interpretedField}.1{_tokenCountSuffix}")); } desc.Size(0); desc.Aggregations(a => aggDesc); desc.Query(q => q.MatchAll()); var response = Client.Search <DocumentElastic>(desc); ResponseValidator(response); // calculate the count for the current n-gram from the unigrams count var allCount = response.Aggregations.Sum(a => Convert.ToInt32(((ValueAggregate)a.Value).Value)) - (ngramCount - 1); return(allCount); }
private AggregationContainerDescriptor <TEntity> GetAggregation(List <AggregationItem> aggregations) { AggregationContainerDescriptor <TEntity> aggregation = new AggregationContainerDescriptor <TEntity>(); if (aggregations != null) { foreach (var item in aggregations) { string name = item.Name; switch (item.Type) { case AggregationExpressionType.Count: aggregation.Terms(name, aggr => { aggr.Field(item.Name) .Size(item.Size) //.Exclude(item.ExcludedItems) .Aggregations(child => GetAggregation(item.Children)); if (!string.IsNullOrEmpty(item.SortField)) { aggr.Order(new TermsOrder() { Key = item.SortField, Order = item.SortType == "asc" ? SortOrder.Ascending : SortOrder.Descending }); } return(aggr); }); break; case AggregationExpressionType.Range: SetRangeAggregationContainer(item, aggregation); break; case AggregationExpressionType.Sum: aggregation.Sum(name, d => d.Field(item.Name)); break; case AggregationExpressionType.Avg: aggregation.Average(name, d => d.Field(item.Name)); break; case AggregationExpressionType.Min: aggregation.Min(name, d => d.Field(item.Name)); break; case AggregationExpressionType.Max: aggregation.Max(name, d => d.Field(item.Name)); break; default: break; } } } return(aggregation); }
public static AggregationContainerDescriptor <T> SumBy <T>(this AggregationContainerDescriptor <T> agg, Expression <Func <T, object> > fieldGetter, Expression <Func <T, bool> > filterRule = null) where T : class { var aggName = fieldGetter.GetAggName(AggType.Sum); if (filterRule == null) { return(agg.Sum(aggName, x => x.Field(fieldGetter))); } var filterName = filterRule.GenerateFilterName(); agg.Filter(filterName, f => f.Filter(fd => filterRule.Body.GenerateFilterDescription <T>()) .Aggregations(innerAgg => innerAgg.Sum(aggName, field => field.Field(fieldGetter)))); return(agg); }
/// <summary> /// Get the Aggregated token count per tag /// </summary> /// <param name="interpretedFields">DON'T PUT THE DOCUMENTELASTIC->TEXT FIELD HERE! RATHER THE INTERPRETEDFIELDS</param> /// <param name="ngramCount"></param> /// <param name="tagIds">the tagIds to run on</param> /// <param name="tagField"></param> /// <returns></returns> public Dictionary <string, int> CountForWord(List <string> interpretedFields, int ngramCount, List <string> tagIds, string tagField) { if (!tagIds.Any()) { return(new Dictionary <string, int>()); } var resultDic = new Dictionary <string, int>(); foreach (var tagIdsBatch in tagIds.Batch(1000)) { var mRequest = new MultiSearchRequest { Operations = new Dictionary <string, ISearchRequest>() }; foreach (var tagId in tagIdsBatch) { var desc = new SearchDescriptor <DocumentElastic>(); var aggDesc = new AggregationContainerDescriptor <DocumentElastic>(); foreach (var interpretedField in interpretedFields) { aggDesc.Sum( interpretedField, ad => ad.Field($"{interpretedField}.1{_tokenCountSuffix}")); } desc.Size(0); desc.Aggregations(a => aggDesc); if (!string.IsNullOrEmpty(tagId)) { desc.Query(q => q.Term($"{tagField}", tagId)); } else { desc.Query(q => q.MatchAll()); } mRequest.Operations.Add(tagId, desc); } var result = Client.MultiSearch(mRequest); ResponseValidator(result); tagIdsBatch.ToList().ForEach(tid => resultDic.Add(tid, Convert.ToInt32(result.GetResponse <DocumentElastic>(tid).Aggregations.Sum(a => Convert.ToInt32(((ValueAggregate)a.Value).Value))) - (ngramCount - 1))); } return(resultDic); }