Skip to content

Add configurable sort options to opensearch source search_options#6761

Merged
dlvenable merged 6 commits into
opensearch-project:mainfrom
srikanthpadakanti:opensearch-source-sort-options
Apr 21, 2026
Merged

Add configurable sort options to opensearch source search_options#6761
dlvenable merged 6 commits into
opensearch-project:mainfrom
srikanthpadakanti:opensearch-source-sort-options

Conversation

@srikanthpadakanti

Copy link
Copy Markdown
Collaborator

Description

Adds configurable sort option to the opensearch source's search_options, allowing pipeline authors to specify custom sort fields for pagination instead of the hardcoded [_doc asc, _id asc] default.

Example configuration:

source:                                                   
   opensearch:                                                                                                                                     
     search_options:                                                                                                                               
       sort:                                                                                                                                       
         - name: "@timestamp"                                                                                                                      
           order: descending                                                                                                                       
         - name: "_id"                                                                                                                             
           order: ascending                                                                                                                        

When no sort is configured, behavior is unchanged, defaults to _doc ascending + _id ascending for PIT/no-context searches, and _doc ascending for scroll.

Changes include:

  • New SortConfig configuration model with name/order fields and validation
  • Wired sort options through SearchConfiguration --> workers --> request models --> both OpenSearchAccessor and ElasticsearchAccessor
  • Resolved existing TODO comments in PitWorker and NoSearchContextWorker for passing sort options
  • Connected the pre-existing but unused SortingOptions model and SearchPointInTimeRequest.sortingOptions field

Issues Resolved

Resolves #6332
#6332

Check List

  • [ X ] New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • [ X ] New functionality has javadoc added
  • [ X ] Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com>

@dlvenable dlvenable left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @srikanthpadakanti ! This looks good, and I only have a few small comments.

private String name;

@JsonProperty("order")
private String order = "ascending";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using enums helps us with our JSON schema generation and provides a list of valid values. You should be able to search the repo for enums that serve similar purposes and follow the same patterns. WhitespaceOption in the key-value processor is a nice simple example.

You can also add a method to get the OpenSearch name for each.

e.g.

ASCENDING("ascending", "asc"),
DESCENDING("decending", desc");

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, updated to use a SortOrder enum following the WhitespaceOption pattern. Also added getSortOrderValue() for the asc/desc mapping.

private static SortingOptions fromSortConfig(final SortConfig sortConfig) {
final SortingOptions sortingOptions = new SortingOptions();
sortingOptions.fieldName = sortConfig.getName();
sortingOptions.order = "descending".equalsIgnoreCase(sortConfig.getOrder()) ? "desc" : "asc";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the enum comment above for a way to handle this in the enum itself.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

.collect(Collectors.toList());
}

private static SortingOptions fromSortConfig(final SortConfig sortConfig) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be beneficial to detect if _id is present. If it isn't, there is the possibility that we won't be able to break ties. Probably just log a warning for this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a warning log in SortingOptions.fromSortConfigs() when _id is not present in the custom sort configuration.

Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com>
Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com>
@srikanthpadakanti

Copy link
Copy Markdown
Collaborator Author

Thanks @srikanthpadakanti ! This looks good, and I only have a few small comments.

Thank you for the review, @dlvenable. I’ve addressed your comments. Please take a look when you have a moment.

@dlvenable dlvenable left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @srikanthpadakanti !

@dlvenable dlvenable merged commit 6144e46 into opensearch-project:main Apr 21, 2026
94 of 97 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenSearch source with alternative sort/search_after

3 participants