spring-data-opensearch icon indicating copy to clipboard operation
spring-data-opensearch copied to clipboard

[BUG] Slicing search with searchAfter serialize date as string insted of number

Open d31m07y1988 opened this issue 10 months ago • 4 comments

What is the bug?

Slicing search with searchAfter serialize date as string insted of number.

        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>spring-data-opensearch-starter</artifactId>
            <version>1.5.0</version>
            <!--https://github.com/opensearch-project/spring-data-opensearch/issues/121-->
            <exclusions>
                <exclusion>
                    <groupId>org.opensearch.client</groupId>
                    <artifactId>opensearch-rest-client-sniffer</artifactId>
                </exclusion>

                <!-- disable to use opensearch java client-->
                <exclusion>
                    <groupId>org.opensearch.client</groupId>
                    <artifactId>opensearch-rest-high-level-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>opensearch-java</artifactId>
            <version>2.19.0</version>
        </dependency>

I have a query

var query = new CriteriaQueryBuilder(criteria)
                .withMaxResults(1)
                .withSort(Sort.by(List.of(
                        Sort.Order.desc("endedAt"),
                        Sort.Order.desc("id"))))
                .withSearchAfter(List.of(doc.getEndedAt().toInstant().toEpochMilli(), doc.getId()))
                .withTrackTotalHits(false)
                .build();
operations.search(query, LogDocument.class)

so in this configuration final request looks like


    "search_after": [
        "1684342681000",
        "01951433-a550-717a-9108-4cbd2f4cad3c"
    ],
    "size": 1,
    "sort": [
        {
            "endedAt": {
                "mode": "max",
                "order": "desc"
            }
        },
        {
            "id": {
                "mode": "max",
                "order": "desc"
            }
        }
    ],

insted of date like number with standard opensearch-rest-high-level-client


    "sort": [
        {
            "endedAt": {
                "order": "desc",
                "mode": "min"
            }
        },
        {
            "id": {
                "order": "desc",
                "mode": "min"
            }
        }
    ],
    "track_total_hits": -1,
    "search_after": [
        1684342681000,
        "01951433-a550-717a-9108-4cbd2f4cad3c"
    ]

with string value I get exception Request failed: [search_phase_execution_exception] all shards failed

In search result, before pagination returnd array for sear_after also write date timestamp as string.

How can one reproduce the bug?

Do the search

What is the expected behavior?

Number serialized like number, not String.

What is your host/environment?

Do you have any screenshots?

Do you have any additional context?

d31m07y1988 avatar Feb 20 '25 09:02 d31m07y1988

org.opensearch.data.client.osc.RequestConverter

     if (!isEmpty(query.getSearchAfter())) {
            builder.searchAfter(query.getSearchAfter()
                .stream()
                .map(TypeUtils::toFieldValue)
                .map(FieldValue::_toJsonString)
                .toList());
        }

in org.springframework.data.elasticsearch.client.elc.RequestConverter

							if (!isEmpty(query.getSearchAfter())) {
								bb.searchAfter(query.getSearchAfter().stream().map(TypeUtils::toFieldValue).toList());
							}

d31m07y1988 avatar Feb 20 '25 09:02 d31m07y1988

https://github.com/spring-projects/spring-data-elasticsearch/pull/2679

d31m07y1988 avatar Feb 20 '25 10:02 d31m07y1988

This is the same issue as https://github.com/opensearch-project/opensearch-java/issues/755, the argument type of builder.searchAfter( has been corrected to accept List<FieldValue> in the next major release. As that will be a breaking change a backwards compatible fix was also released in 2.13.0 adding a builder.searchAfterVals( which accepts the List<FieldValue>.

spring-data-opensearch should update org.opensearch.data.client.osc.RequestConverter to use builder.searchAfterVals until 3.0.0 is released.


@opensearch-project/admin Please transfer this issue to https://github.com/opensearch-project/spring-data-opensearch

Xtansia avatar Feb 20 '25 20:02 Xtansia

Catch All Triage - 1

andrross avatar Mar 10 '25 16:03 andrross

Fixed by https://github.com/opensearch-project/spring-data-opensearch/pull/474

reta avatar Jul 18 '25 17:07 reta