opensearch-project / opensearch-project/opensearch-java

Sort field missing in response when using search_after for pagination with Java Client

Open
#1,550 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
165
Forks
250
Avg merge
1d 18h
Merged PRs (30d)
26

Description

**What is the bug?
We are trying to implement pagination using search_after with the Java client (v2.15) on OpenSearch 2.15. Despite setting a sort field (both integer and keyword types), the search response always returns sort: [], i.e., an empty or missing sort value for each hit. This makes it impossible to use search_after for pagination.

How can one reproduce the bug?

Here is the code snippet we are using to build the request:

SearchRequest.Builder searchRequestBuilder = new SearchRequest.Builder();
searchRequestBuilder.index("nlp-index-details-v4")
    .query(query)
    .pipeline(searchPipelineName)
    .requestCache(true)
    .trackTotalHits(t -> t.enabled(true))
    .source(sourceBuilder -> sourceBuilder.filter(fb -> fb.excludes("name_embedding")))
    .from(from)
    .size(size)
    .sort(s -> s.field(f -> f.field("name").order(SortOrder.Desc))); // Also tried with "id" (integer)

SearchRequest searchRequest = searchRequestBuilder.build();
SearchResponse<Map> searchResponse = client.search(searchRequest, Map.class);

Index mapping:
name: keyword
id: integer

Expected: The response should include non-null sort values to enable search_after to work.
Actual: The sort field is always missing or empty in the hits, making pagination fail.

What is the expected behavior?

Each hit in the response should contain a sort field with the corresponding sort value, allowing us to use that value in subsequent paginated requests.

What is your host/environment?

OS: Windows
Java: OpenJDK 21
OpenSearch: 2.15.0
OpenSearch Java Client: 2.15.0

Do you have any screenshots?
Do you have any additional context?

The same search query (with sort) works correctly when executed via Postman – the sort values appear in the response as expected. This issue seems specific to the Java client.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue through the Java client's SearchRequest and client.search path using the supplied index, mappings, and sort settings. Compare the resulting SearchResponse with the Postman response to locate where sort values are lost. Done means each returned hit includes its corresponding sort value so it can be passed to search_after.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.