# How to translate HighLevel client Bucket order to elasticsearch-java

**URL:** <https://discuss.elastic.co/t/how-to-translate-highlevel-client-bucket-order-to-elasticsearch-java/371840>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 11, 2024, 2:00pm UTC](https://discuss.elastic.co/t/how-to-translate-highlevel-client-bucket-order-to-elasticsearch-java/371840 "2024-12-11T14:00:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hans\_Kruse](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hans_kruse/32/54739_2.png) [@Hans\_Kruse](https://discuss.elastic.co/u/Hans_Kruse)\
**Post date:** [December 11, 2024, 2:00pm UTC](https://discuss.elastic.co/t/how-to-translate-highlevel-client-bucket-order-to-elasticsearch-java/371840/1 "2024-12-11T14:00:48Z")

</div>

In our Elasticsearch highlevel client based code we have some code that uses the `BucketOrder.key(...)` and `BucketOrder.count(...)` factory methods.

What are equivalent constructs for the the elasticsearch-java client SDK? The code now requires a `NamedValue<SortOrder>`. What should be used for the names in case of?:

- `Bucket.Order.key(true)`
- `BucketOrder.count(false)`

Original helper function using highlevel client code:

```Java
    TermsAggregation.Builder buildAggregation(final AggregatePair aggregatePair) {
        final var aggregation = new TermsAggregation.Builder()
                .terms(aggregatePair.aggregateName())
                .field(aggregatePair.aggregateField())
                .size(aggregatePair.size());

        // BucketOrder.key enables the sortering by name; true configures ascending sort by alphabet.
        // BucketOrder.count enables the sortering by count, false configures descending sort by count.
        if ("key".equals(aggregatePair.getOrder())) {
            aggregation.order(BucketOrder.key(true));
        } else {
            aggregation.order(BucketOrder.count(false));
        }
        return aggregation;
    } 

```

New helper function:

```Java
    private @NotNull NamedValue<SortOrder> toSortOrder(final @NotNull AggregatePair aggregatePair) {
        if ("key".equals(aggregatePair.order())){
            return NamedValue.of("???", SortOrder.Asc);
        }else{
            return NamedValue.of("???", SortOrder.Desc);
        }
    }

```

Context of building converting objects to aggregations:

```Java
 private @NotNull Map<String, Aggregation> toAggregations(final @Nullable List<AggregatePair> aggregations) {
        if (null == aggregations) {
            return Collections.emptyMap();
        }
        return aggregations
                .stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(AggregatePair::aggregateName,
                        aggregatePair ->
                                new Aggregation.Builder()
                                        .terms( termsAggreationBuilder -> termsAggreationBuilder
                                                .field(aggregatePair.aggregateField())
                                                .size(aggregatePair.size())

                                                .order(List.of(toSortOrder(aggregatePair))))
                                        .build()
                ));
    }

```

Context of Creating a `SearchRequest` with aggregations (Work In Progress):

```Java
 public @NotNull SearchRequest createRequest(final @NotNull SearchRequestParameters parameters,
                                       final @NotNull List<String> aliases) {
        if (aliases.isEmpty()) {
            throw new IllegalArgumentException("aliases must not be empty");
        }
        final var searchRequestBuilder = new SearchRequest.Builder()
                .index(aliases)
                .from(parameters.from())
                .size(parameters.size())
                .terminateAfter(parameters.terminateAfter())
                .sort(toSortOptions(parameters.sorting()))
                .indicesBoost(toIndicesBoost(parameters.boostedIndexList()))
                .highlight(createHighlightBuilder().build());
        //TODO: add more query configuration and the query itself.
        final var searchParameters = parameters.searchParameters();
        if(searchParameters != null) {
             searchRequestBuilder.aggregations(toAggregations(searchParameters.aggregatedFields()));
        }
        return searchRequestBuilder.build();
    }

```

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [December 16, 2024, 12:06pm UTC](https://discuss.elastic.co/t/how-to-translate-highlevel-client-bucket-order-to-elasticsearch-java/371840/2 "2024-12-16T12:06:01Z")

</div>

Hello!

We decided to change the implementation of Order so that it allows custom fields (like the server does, accepting any field), so you can use the same values described in the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html).

In your specific case they would be:

```java
NamedValue.of("_key", SortOrder.Asc);
NamedValue.of("_count", SortOrder.Desc);

```

---

<div class="post-metadata">

**Author:** ![Hans\_Kruse](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hans_kruse/32/54739_2.png) [@Hans\_Kruse](https://discuss.elastic.co/u/Hans_Kruse)\
**Post date:** [December 17, 2024, 10:14am UTC](https://discuss.elastic.co/t/how-to-translate-highlevel-client-bucket-order-to-elasticsearch-java/371840/3 "2024-12-17T10:14:22Z")

</div>

Having a bunch of constants in the SDK for these known options would be nice.

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [December 17, 2024, 10:36am UTC](https://discuss.elastic.co/t/how-to-translate-highlevel-client-bucket-order-to-elasticsearch-java/371840/4 "2024-12-17T10:36:20Z")

</div>

Agree! Will add a tracking issue on github
