# High level rest client composite aggregation ordering

**URL:** <https://discuss.elastic.co/t/high-level-rest-client-composite-aggregation-ordering/231049>\
**Category:** Elasticsearch\
**Created:** [May 4, 2020, 10:13pm UTC](https://discuss.elastic.co/t/high-level-rest-client-composite-aggregation-ordering/231049 "2020-05-04T22:13:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![plm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/plm/32/77829_2.png) [@plm](https://discuss.elastic.co/u/plm)\
**Post date:** [May 4, 2020, 10:13pm UTC](https://discuss.elastic.co/t/high-level-rest-client-composite-aggregation-ordering/231049/1 "2020-05-04T22:13:07Z")

</div>

Hi,

I am trying to write the following composite aggregation query using the High Level Rest Client, but cannot figure out how to specify the bucket sort part on the last\_execution field:

```auto
curl -u elastic:pwd -v -H "Content-Type: application/json" -XPOST http://localhost:9200/myindex/_search?pretty=true -d '
{
   "query": {
         "bool": {
              "filter": [
                  { "range": { "updateDate" : { "gte": "now-30d"} } }
              ]
          } 
    },
    "aggs" : {
        "my_buckets": {
            "composite": {
                "size": 500,
                "sources" : [
                    { "clientId": { "terms": { "field": "clientId"} } },
                    { "orderId": { "terms": { "field": "oderId"} } },
                    { "orderState": { "terms": { "field": "orderState" } } }			   
                ]
            },
            "aggregations": {
                "last_execution": { "max": { "field": "updateDate" } },
                "last_execution_sort": { "bucket_sort": { "sort": [{ "last_execution": { "order": "desc" } }] } }
            }
        }
    },
    "size": 0
}'

```

There doesn't seem to be a way to order on the CompositeAggregationBuilder?

Here is my current code:

```auto
                SearchRequest searchRequest = new SearchRequest(searchAliases);
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.size(0);
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		searchSourceBuilder.query(boolQuery);
		searchRequest.source(searchSourceBuilder);
		
		// Add filters on boolQuery, removed because not relevant for this question
		
		List<CompositeValuesSourceBuilder<?>> sources = new ArrayList<>();  
		
        TermsValuesSourceBuilder clientIdBucket = 
        		new TermsValuesSourceBuilder("clientId")  
                    .field("clientId");
        sources.add(clientIdBucket); 
        
        TermsValuesSourceBuilder orderIdBucket = 
        		new TermsValuesSourceBuilder("orderId")  
                    .field("orderId");
        sources.add(orderIdBucket);
        
        TermsValuesSourceBuilder orderStateBucket = 
        		new TermsValuesSourceBuilder("orderState")  
                    .field("orderState");
        sources.add(orderStateBucket);

        MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("last_update_date").field("updateDate");
        
        CompositeAggregationBuilder compositeAggregationBuilder =
                new CompositeAggregationBuilder("my_buckets", sources).
                size(1000).
                subAggregation(maxAggregationBuilder);
        
        searchSourceBuilder.aggregation(compositeAggregationBuilder);

```

Thanks,  
Philippe

---

<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 1, 2020, 10:13pm UTC](https://discuss.elastic.co/t/high-level-rest-client-composite-aggregation-ordering/231049/2 "2020-06-01T22:13:12Z")

</div>

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