opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

[BUG] The .order() method of aggregation does not work with composite aggregations

Open SavvasUmangKamdar opened this issue 1 year ago • 6 comments

What is the bug?

The .order() method of aggregation expects input parameters of Map<> or List<Map> type only which throws error while doing the composite aggregation.

How can one reproduce the bug?

Aggregation aggregation = new Aggregation.Builder().terms(aggField -> aggField.field("my_name.keyword").order(Map.of("_key", SortOrder.Desc))).build();

This will throw an error when used with composite aggregation. Error: [x_content_parse_exception] [1:125] [composite] failed to parse field [sources] with this root cause [1:137] [terms] order doesn't support values of type: START_ARRAY

What is the expected behavior?

I think the .order() method should just accept string values too

What is your host/environment?

Using opensearch-java library in my spring boot application

Do you have any additional context?

Pls refer this stackoverflow link for detailed question: Link

SavvasUmangKamdar avatar Apr 24 '24 07:04 SavvasUmangKamdar

The issue might stem from the fact that the CompositeAggregationSource class expects a TermsAggregation when terms is specified in a composite source, i.e. the same TermsAggregation structure that is used when specifying standalone terms aggregations. TermsAggregation has a different way of supplying the sort order, i.e using List<Map<String, SortOrder>>

On the server-side, the TermsValuesSourceBuilder class expects a simple SortOrder value

consulthys avatar Apr 24 '24 08:04 consulthys

Hey @consulthys, thanks for replying. It would be helpful if you have any solution on how to handle this case?

SavvasUmangKamdar avatar Apr 24 '24 08:04 SavvasUmangKamdar

@SavvasUmangKamdar It's a bug in the OS Java client that needs to be fixed. TermsAggregation needs to be specialized to be used in a CompositeAggregationSource

consulthys avatar Apr 24 '24 08:04 consulthys

@SavvasUmangKamdar want to give it a try? Maybe start by writing a (failing) test for it?

dblock avatar Apr 24 '24 12:04 dblock

@dblock sorry but am completely new to opensearch, so not much aware of this!

SavvasUmangKamdar avatar Apr 24 '24 19:04 SavvasUmangKamdar

@dblock sorry but am completely new to opensearch, so not much aware of this!

Totally. Give it a shot though, start with https://github.com/opensearch-project/opensearch-java/blob/main/DEVELOPER_GUIDE.md.

dblock avatar Apr 24 '24 22:04 dblock