Skip to content
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

[BUG] Generic Client with Custom Deserializer fails to deserialize aggregations #2932

Open
dsinghal-nice opened this issue Aug 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@dsinghal-nice
Copy link

dsinghal-nice commented Aug 13, 2024

What is the bug?
I am using opensearch-java client 2.12.0

Describe the issue:
I have a specific requirement where I will be using aggregations on a index, but I want to use SQL plugin for that. Since the response for the aggregations from OpenSearchClient is very complicated.

I am trying to deserialize the response but getting below error:

Exception in thread "main" jakarta.json.stream.JsonParsingException: Property name 'year' is not in the 'type#name' format. Make sure the request has 'typed_keys' set.

final String body = response.getBody().map(org.opensearch.client.opensearch.generic.Body::bodyAsString).orElse("");

I get the below response as part of Response body

{"took":17,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":null,"hits":[]},"aggregations":{"year":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":1960,"doc_count":1,"COUNT(1)":{"value":0}}]}}}

Below is the code I am trying to deserialize the response

JacksonJsonpMapper jacksonJsonpMapper = new JacksonJsonpMapper();
            final SearchResponse<Book> result = response.getBody().
                    map(b -> Bodies.json(b, SearchResponse.createSearchResponseDeserializer(Book.createBookDeserializer()), jacksonJsonpMapper))
                    .orElse(null);

Below is my code for the same

private static void searchUsingGenericClient(OpenSearchGenericClient openSearchGenericClient) throws IOException {
	JsonObject jsonObject = Json.createObjectBuilder().add("query", "SELECT title, count(1) from books_test where year > 1951 group by year").build();

	try (org.opensearch.client.opensearch.generic.Response response = openSearchGenericClient
			.execute(
					Requests.builder()
							.endpoint("/_plugins/_sql?format=json")
							.method("POST")
							.json(jsonObject)
							.build())) {
		// Retrieve the response body as a simple string
		final String body = response.getBody().map(org.opensearch.client.opensearch.generic.Body::bodyAsString).orElse("");

		JacksonJsonpMapper jacksonJsonpMapper = new JacksonJsonpMapper();
		final SearchResponse<Book> result = response.getBody().
				map(b -> Bodies.json(b, SearchResponse.createSearchResponseDeserializer(Book.createBookDeserializer()), jacksonJsonpMapper))
				.orElse(null);

		System.out.println("object conversion from generic client :: " + result.hits().hits().get(0).source());
	}
	System.out.println("End searchUsingGenericClient :: ");
}

Do you have any additional context?
(https://forum.opensearch.org/t/generic-client-with-custom-deserializer-fails-to-deserialize-aggregations/20739)

@dsinghal-nice dsinghal-nice added bug Something isn't working untriaged labels Aug 13, 2024
@dblock dblock removed the untriaged label Sep 2, 2024
@dblock
Copy link
Member

dblock commented Sep 2, 2024

[Weekly Catch All Triage - 1]

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

No branches or pull requests

2 participants