Bidzina Goginashvili

Bidzina Goginashvili

  • NA
  • 206
  • 26.2k

ElasticSearch paging sorting aggregation

Aug 5 2019 4:43 PM
  1. .Index("onoff-products-tests-main")  
  2. .RequestCache(true)  
  3. .From(0)  
  4. .Size(15)  
  5. .Query(q =>  
  6. {  
  7. QueryContainer container = +q.Terms(c => c  
  8. .Field("categories.id")  
  9. .Terms(categoryIds));  
  10. if (manufacturerIds?.Any() == true)  
  11. { container = container && +q.Terms(ma => ma .Field("manufacturers.id") .Terms(manufacturerIds));  
  12. }  
  13. if (filteredSpecs?.Any() == true)  
  14. { container = container && +q.Terms(ma => ma .Field("options.id") .Terms(filteredSpecs));  
  15. }  
  16. if (vendorIds?.Any() == true)  
  17. { container = container && +q.Terms(c => c .Field("vendor.id") .Terms(vendorIds));  
  18. }  
  19. if (inStorage == true)  
  20. { container = container && +q.Term(c => c .Field("inStorage") .Value(true));  
  21. }  
  22. if (priceMax.HasValue || priceMin.HasValue)  
  23. { container = container && +q.TermRange(x => x .Field("price") .GreaterThan("0") .LessThan("20"));  
  24. return container;  
  25. }).Aggregations(agg => agg .Terms("products_aggregation", x => x.Field("modelName.keyword")  
  26. .Include(partition,partition+15)  
  27. .Size(15)  
  28. .Aggregations(agg2 => agg2  
  29. .TopHits("sameModeledProducts_aggregation", s => s.Sort((ss) =>  
  30. switch (orderBy)  
  31. case ProductSortingEnum.NameAsc:  
  32. return ss.Ascending(ff => ff.Name);  
  33. case ProductSortingEnum.NameDesc:  
  34. return ss.Descending(ff => ff.Name);  
  35. case ProductSortingEnum.PriceAsc:  
  36. return ss.Ascending(ff => ff.Price);  
  37. case ProductSortingEnum.PriceDesc:  
  38. return ss.Descending(ff => ff.Price);  
  39. case ProductSortingEnum.MostViewedDesc:  
  40. return ss.Descending(ff => ff.Price);  
  41. defaultreturn ss.Descending(ff => ff.Price); } })  
  42. .Source(ss => ss.Includes(i => i.Field(f => f.Id))) .Size(100)))));  
I need to have pagination here plus sorting on parent aggregation and sub aggregation but i dont know what to do pls help me to change this original...

Answers (1)