I have this OpenSearch code:
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.GetResponse;
private OpenSearchClient client;
private Optional<GetResponse> getDocumentResponse(BaseIndexDto request, String... indices) {
try {
final String indexName = this.getIndexName(indices);
GetRequest getRequest = new GetRequest.Builder()
.index(indexName)
.id(request.getId())
.build();
GetResponse<GetResponse> response = client.get(getRequest, GetResponse.class);
if (response.found()) {
return Optional.of(response);
} else {
return Optional.empty();
}
} catch (Exception e) {
throw Exception("Error");
}
}
I want to edit it this way:
private Optional<GetResponse<Map<String, Object>>> getDocumentResponse(BaseIndexDto request, String... indices) {
try {
final String indexName = this.getIndexName(indices);
GetRequest getRequest = new GetRequest.Builder()
.index(indexName)
.id(request.getId())
.build();
GetResponse<Map<String, Object>> response = client.get(getRequest, GetResponse.class);
if (response.found()) {
return Optional.of(response);
} else {
return Optional.empty();
}
} catch (Exception e) {
throw Exception("Error");
}
}
But I get error:
Required type: GetResponse<Map<String, Object>>
Provided: GetResponse <GetResponse>
Incompatible equality constraint: Map<String, Object> and GetResponse
I need something like this:
GetResponse<Map<String, Object>> response = client.get(getRequest, GetResponse<Map<String, Object>>.class);
What is the correct way to implement this?
EDIT:
How I can implement a solution without this casting:
private Optional<GetResponse<Map<String, Object>>> getDocumentResponse(BaseIndexDto request, String... indices) {
try {
final String indexName = this.getIndexName(indices);
GetRequest getRequest = new GetRequest.Builder()
.index(indexName)
.id(request.getId())
.build();
GetResponse<Object> response = client.get(getRequest, Object.class);
if (response.found()) {
GetResponse<Map<String, Object>> typedResponse = castGetResponse(response, Map.class);
return Optional.of(typedResponse);
} else {
return Optional.empty();
}
} catch (Exception e) {
throw Exception();
}
}
private <T> GetResponse<T> castGetResponse(GetResponse<?> response, Class<Map> clazz) {
return (GetResponse<T>) response;
}
client.get(getRequest, GetResponse.class)? The same as before you changed toMapRequired type: GetResponse <Map<String, Object>> Provided: GetResponse <GetResponse> Incompatible equality constraint: Map<String, Object> and GetResponseGetResponsethen assign that variable to another variable of the right type, adding a cast. This will cause a warning. Suppress that. It's ugly and means the compiler will not verify types for this method.GetResponse<?> response = (GetResponse<?>) client.get(getRequest, GetResponse.class); Map<?, ?> map = (Map<?, ?>) response.source(); Map<String, Object> typedMap = map.entrySet().stream().collect(Collectors.toMap(e -> (String) e.getKey(), e -> e.getValue()));If this seems unwieldy, it’s because programs are not supposed to use raw types in the first place.