# Pagination + Sorting + Aggregaition? Has Anyone done this magic yeat ? PLS HELP

**URL:** <https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854>\
**Category:** Elasticsearch\
**Created:** [August 5, 2019, 5:24pm UTC](https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854 "2019-08-05T17:24:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 5, 2019, 5:24pm UTC](https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854/1 "2019-08-05T17:24:17Z")

</div>

Please it will be helpfull if you give me a script...

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 5, 2019, 5:31pm UTC](https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854/2 "2019-08-05T17:31:13Z")

</div>

{  
"onoff-products-tests-main-test" : {  
"mappings" : {  
"properties" : {  
"Categories" : {  
"type" : "nested",  
"properties" : {  
"Id" : {  
"type" : "integer"  
}  
}  
},  
"FullDescription" : {  
"type" : "text"  
},  
"InStorage" : {  
"type" : "boolean"  
},  
"ManufacturerPartNumber" : {  
"type" : "text"  
},  
"Manufacturers" : {  
"type" : "nested",  
"properties" : {  
"Id" : {  
"type" : "integer"  
}  
}  
},  
"ModelName" : {  
"type" : "keyword"  
},  
"Name" : {  
"type" : "text"  
},  
"Options" : {  
"type" : "nested",  
"properties" : {  
"AllowFiltering" : {  
"type" : "boolean"  
},  
"Id" : {  
"type" : "integer"  
}  
}  
},  
"ParentBaseProductId" : {  
"type" : "long"  
},  
"Price" : {  
"type" : "double"  
},  
"ShortDescription" : {  
"type" : "text"  
},  
"Sku" : {  
"type" : "text"  
},  
"VendorId" : {  
"type" : "integer"  
},  
"categories" : {  
"properties" : {  
"id" : {  
"type" : "long"  
}  
}  
},  
"customProperties" : {  
"type" : "object"  
},  
"fullDescription" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
},  
"id" : {  
"type" : "long"  
},  
"inStorage" : {  
"type" : "boolean"  
},  
"manufacturers" : {  
"properties" : {  
"id" : {  
"type" : "long"  
}  
}  
},  
"modelName" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
},  
"name" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
},  
"options" : {  
"properties" : {  
"allowFiltering" : {  
"type" : "boolean"  
},  
"id" : {  
"type" : "long"  
}  
}  
},  
"parentBaseProductId" : {  
"type" : "long"  
},  
"price" : {  
"type" : "float"  
},  
"shortDescription" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
},  
"sku" : {  
"type" : "text",  
"fields" : {  
"keyword" : {  
"type" : "keyword",  
"ignore\_above" : 256  
}  
}  
},  
"vendorId" : {  
"type" : "long"  
}  
}  
}  
}  
}

THIS IS MY MAPNG

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 5, 2019, 5:31pm UTC](https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854/3 "2019-08-05T17:31:34Z")

</div>

THIS IS WHAT I AM TRYING TO DO  
var Response = searchDescriptor  
.Index("onoff-products-tests-main")  
.RequestCache(true)  
.From(0)  
.Size(15)  
.Query(q =\>  
{  
QueryContainer container = +q.Terms(c =\> c  
.Field("categories.id")  
.Terms(categoryIds));

```
            if (manufacturerIds?.Any() == true)
            {
                container = container && +q.Terms(ma => ma
                    .Field("manufacturers.id")
                         .Terms(manufacturerIds));
            }
            if (filteredSpecs?.Any() == true)
            {
                container = container && +q.Terms(ma => ma
                    .Field("options.id")
                         .Terms(filteredSpecs));
            }
            if (vendorIds?.Any() == true)
            {
                container = container && +q.Terms(c => c
                     .Field("vendor.id")
                         .Terms(vendorIds));
            }
            if (inStorage == true)
            {
                container = container && +q.Term(c => c
                    .Field("inStorage")
                         .Value(true));
            }
            if (priceMax.HasValue || priceMin.HasValue)
            {
                container = container && +q.TermRange(x => x
                    .Field("price")
                        .GreaterThan("0")
                                .LessThan("20"));
            }

            return container;
        }).Aggregations(agg => agg
                   .Terms("products_aggregation",
                       x => x.Field("modelName.keyword")
                           .Include(partition,partition+15)
                            .Size(15)
          .Aggregations(agg2 => agg2
              .TopHits("sameModeledProducts_aggregation",
                  s => s.Sort((ss) =>
                  {
                      switch (orderBy)
                      {
                          case ProductSortingEnum.NameAsc:
                              return ss.Ascending(ff => ff.Name);
                          case ProductSortingEnum.NameDesc:
                              return ss.Descending(ff => ff.Name);
                          case ProductSortingEnum.PriceAsc:
                              return ss.Ascending(ff => ff.Price);
                          case ProductSortingEnum.PriceDesc:
                              return ss.Descending(ff => ff.Price);
                          case ProductSortingEnum.MostViewedDesc:
                              return ss.Descending(ff => ff.Price);
                          default:
                              return ss.Descending(ff => ff.Price);
                      }
                  }).Source(ss => ss.Includes(i => i.Field(f => f.Id)))
                          .Size(100)))));
```

---

<div class="post-metadata">

**Author:** ![Bidzina\_Goginashvili](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bidzina_goginashvili/32/51114_2.png) [@Bidzina\_Goginashvili](https://discuss.elastic.co/u/Bidzina_Goginashvili)\
**Post date:** [August 5, 2019, 5:31pm UTC](https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854/4 "2019-08-05T17:31:58Z")

</div>

BUT I CANNT PAGINATE AND SORT ALL OVER THE RECORDS PLEASEEE HELP...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 2, 2019, 5:32pm UTC](https://discuss.elastic.co/t/pagination-sorting-aggregaition-has-anyone-done-this-magic-yeat-pls-help/193854/5 "2019-09-02T17:32:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
