public static AggregationContainerDescriptor <T> SortedTopHits <T>(this AggregationContainerDescriptor <T> agg, int size, Expression <Func <T, object> > fieldSort, SortType sorttype, params Expression <Func <T, object> >[] fieldGetter) where T : class { var aggName = sorttype + fieldSort.GetAggName(AggType.TopHits); var sortFieldDescriptor = new SortFieldDescriptor <T>(); var fieldSortName = Names.GetNameFromGetFieldNamed(fieldSort.Body); sortFieldDescriptor = fieldSortName != null?sortFieldDescriptor.Field(fieldSortName) : sortFieldDescriptor.Field(fieldSort); sortFieldDescriptor = sorttype == SortType.Ascending ? sortFieldDescriptor.Ascending() : sortFieldDescriptor.Descending(); var fieldNames = fieldGetter.Select(x => Names.GetNameFromGetFieldNamed(x.Body)).Where(x => x != null); var fieldGetters = fieldGetter.Where(x => Names.GetNameFromGetFieldNamed(x.Body) == null); var allFields = fieldNames.Select(x => new Field(x)).Concat(fieldGetters.Select(x => new Field(x))); return(agg.TopHits( aggName, x => x .Size(size) .Source(i => i.Includes(f => f.Fields(allFields))) .Sort(s => new PromiseValue <IList <ISort> >(new List <ISort> { sortFieldDescriptor })))); }
public static AggregationContainerDescriptor <T> SortedTopHits <T>(this AggregationContainerDescriptor <T> agg, int size, Expression <Func <T, object> > fieldSort, SortType sorttype, params Expression <Func <T, object> >[] fieldGetter) where T : class { var aggName = sorttype + fieldSort.GetAggName(AggType.TopHits); var sortFieldDescriptor = new SortFieldDescriptor <T>(); sortFieldDescriptor = sortFieldDescriptor.Field(fieldSort); if (sorttype == SortType.Ascending) { sortFieldDescriptor = sortFieldDescriptor.Ascending(); } else { sortFieldDescriptor = sortFieldDescriptor.Descending(); } return(agg.TopHits(aggName, x => x.Size(size).Source(i => i.Include(f => f.Fields(fieldGetter))).Sort(s => sortFieldDescriptor))); }