# Problems with aggregations filtering

**URL:** <https://discuss.elastic.co/t/problems-with-aggregations-filtering/131910>\
**Category:** Elasticsearch\
**Created:** [May 15, 2018, 10:45am UTC](https://discuss.elastic.co/t/problems-with-aggregations-filtering/131910 "2018-05-15T10:45:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![davide\_ego](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@davide\_ego](https://discuss.elastic.co/u/davide_ego)\
**Post date:** [May 15, 2018, 10:45am UTC](https://discuss.elastic.co/t/problems-with-aggregations-filtering/131910/1 "2018-05-15T10:45:23Z")

</div>

Hi everyone,  
I'm trying to implement (using elastic php lib) a group by of 5 fields, filtering for some conditions and next ordering the result. Obviously I need to retrieve a set of all objects.  
I think i'm wrong because the order doesn't acts on the result set but on the single aggregation, and I'm not getting back from elastic a single set of object but a set for each aggregation.

Someone can help me please? This is the params I pass to php elastic search function:

```
{  
   "index":"test",
   "type":"test",
   "body":{  
      "aggs":{  
         "group_by_code":{  
            "terms":{  
               "field":"code.keyword"
            },
            "aggs":{  
               "group_by_article":{  
                  "terms":{  
                     "field":"article.keyword"
                  },
                  "aggs":{  
                     "group_by_color":{  
                        "terms":{  
                           "field":"color.keyword"
                        },
                        "aggs":{  
                           "group_by_attr":{  
                              "terms":{  
                                 "field":"attr.keyword"
                              },
                              "aggs":{  
                                 "top_hits_agg":{  
                                    "top_hits":{  
                                       "sort":[  
                                          {  
                                             "date.keyword":"desc",
                                             "number.keyword":"asc",
                                             "code.keyword":"desc",
                                             "code_2.keyword":"asc"
                                          }
                                       ],
                                       "size":1
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}
```

---

<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:** [June 12, 2018, 10:45am UTC](https://discuss.elastic.co/t/problems-with-aggregations-filtering/131910/2 "2018-06-12T10:45:41Z")

</div>

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