New Java API Client GetResponse

The HLRC provided getSourceAsMap in GetResponse. I'm trying to get the source as a map. How do I do this with the new client? Thanks.

Welcome!

I think you can do something like this:

SearchResponse<Map> response =  client.search(sr -> sr.index("search-data"), Map.class);

But it won't exactly generate the map for the whole response, just for the documents...

Why do you want to do this actually? I'm asking because we might be able to propose a better to solve your usecase.

Thanks. We are doing a custom mapping to an object with the query result. It's not straightforward so we can't just provide a target class.

So it's not exactly the "response" object that you want to get back as a Map but more the _source, right?
I guess my answer works in that case...

Yes that is correct. Sorry if I wasn't clear.

Can I just use Map? I'm seeing a complaint about using a raw use of paramaterized type.

                GetResponse<Map> response = getJavaApiClient().get(g -> g
                        .index(indexName.toString())
                        .id(fileUri),
                    Map.class
                );

I want to use <Map<String, Object>>. But what should the target class be in that case?

I guess you need to do something like that:

ObjectMapper mapper = new ObjectMapper();

GetResponse<ObjectNode> getResponse = client.get(gr -> gr.index("get-with-filter").id("1"), ObjectNode.class);
Map<String, Object> result = mapper.convertValue(getResponse.source(), new TypeReference<>(){});

Unless @swallez has a better idea :wink:

I would use ObjectNode as in your example. Using Map<String, Object> is brittle as it lets the object mapper decide what type to use to represent nested objects, and is therefore less robust/predictable.

Thanks!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.