Skip to content

Possible bug building SortOptions object from json #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aianta opened this issue May 11, 2023 · 3 comments
Open

Possible bug building SortOptions object from json #573

aianta opened this issue May 11, 2023 · 3 comments
Labels
Category: Bug Something isn't working

Comments

@aianta
Copy link

aianta commented May 11, 2023

Java API client version

8.7.1

Java version

19.0.1

Elasticsearch Version

8.4.3

Problem description

I'm trying to create a SortOptions object for an elastic search query using the Create API objects from JSON data method. But I keep getting the following exception:

class co.elastic.clients.json.JsonpDeserializer$3 cannot be cast to class co.elastic.clients.json.ObjectDeserializer (co.elastic.clients.json.JsonpDeserializer$3 and co.elastic.clients.json.ObjectDeserializer are in unnamed module of loader 'app')
java.lang.ClassCastException: class co.elastic.clients.json.JsonpDeserializer$3 cannot be cast to class co.elastic.clients.json.ObjectDeserializer (co.elastic.clients.json.JsonpDeserializer$3 and co.elastic.clients.json.ObjectDeserializer are in unnamed module of loader 'app')
    at co.elastic.clients.util.WithJsonObjectBuilderBase.withJson(WithJsonObjectBuilderBase.java:53)
    at co.elastic.clients.json.WithJson.withJson(WithJson.java:57)
    at ElasticsearchFromJsonTest.lambda$parseSortOptions$0(ElasticsearchFromJsonTest.java:27)
    at co.elastic.clients.elasticsearch._types.SortOptions.of(SortOptions.java:119)
    at ElasticsearchFromJsonTest.parseSortOptions(ElasticsearchFromJsonTest.java:27)
...

Code sample:

import co.elastic.clients.elasticsearch._types.SortOptions;

import org.junit.jupiter.api.Test;
import java.io.StringReader;

public class ElasticsearchFromJsonTest {

    @Test
    void parseSortOptions(){

        String test = """
                {
                  "sort" : [
                    { "term" : "asc"}
                  ]
                }
                """;

        SortOptions so = SortOptions.of(b->b.withJson(new StringReader(test)));

    }

}

I've also tried setting test to:

[{"term" : "asc"}]

And got the same results. I'll admit the 'bug' may just be in my simple minded interpretation of what subsection/format of json input is required to get this to work 😓, on the plus side that would be an easy fix.

@l-trotta l-trotta added the Category: Bug Something isn't working label Apr 18, 2024
@l-trotta
Copy link
Contributor

Hello, thank you for the report! Definitely a bug, we'll investigate this.

@prchen
Copy link

prchen commented Jan 15, 2025

For new guests to this issue, this comment may help you resolve this ancient issue temporarily 😂

I've figured out a possible solution by copying and override some source code:

  1. Write a child class for SortOptions.Builder
  2. Override withJson methods to make it works
  3. Build SortOptions instance one-by-one for each item in the sort array

Code example:

import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.json.JsonpMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.stream.JsonParser;

import java.io.StringReader;
import java.util.Map;

public class DummySortOptionBuilder extends SortOptions.Builder {

    @Override
    public SortOptions.Builder withJson(JsonParser parser, JsonpMapper mapper) {
        var option = SortOptions._DESERIALIZER.deserialize(parser, mapper);
        if (option.isScore()) {
            score(option.score());
        } else if (option.isDoc()) {
            doc(option.doc());
        } else if (option.isGeoDistance()) {
            geoDistance(option.geoDistance());
        } else if (option.isScript()) {
            script(option.script());
        } else if (option.isField()) {
            field(option.field());
        }
        return this;
    }

    public static SortOptions fromJson(String json) {
        return new DummySortOptionBuilder()
                .withJson(new StringReader(json))
                .build();
    }

    public static void main(String[] args) throws Exception {
        var mapper = new ObjectMapper();

        // sort by score
        System.out.println(fromJson(mapper.writeValueAsString("_score")));
        System.out.println(fromJson("\"_score\""));

        // sort by doc
        System.out.println(fromJson(mapper.writeValueAsString("_doc")));
        System.out.println(fromJson("\"_doc\""));

        // sort by field
        System.out.println(fromJson(mapper.writeValueAsString("term")));
        System.out.println(fromJson("\"term\""));

        // sort by field with options
        System.out.println(fromJson(mapper.writeValueAsString(Map.of("term", "asc"))));
        System.out.println(fromJson("{\"term\" : \"asc\"}"));
    }
}

@li-a
Copy link

li-a commented Feb 3, 2025

More directly, the workaround would be to avoid these two forms:

"sort" : [
    "_score"
]

"sort" : [
    { "myField": "asc" }
]

and instead, construct the JSON with the most verbose form:

"sort" : [
    { "_score": { "order": "desc" } }
]

"sort" : [
    { "myField": { "order": "asc" } }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants