# New Java API Client GetResponse

**URL:** <https://discuss.elastic.co/t/new-java-api-client-getresponse/340287>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 7, 2023, 3:19pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287 "2023-08-07T15:19:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)\
**Post date:** [August 7, 2023, 3:19pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/1 "2023-08-07T15:19:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 7, 2023, 3:28pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/2 "2023-08-07T15:28:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)\
**Post date:** [August 7, 2023, 3:29pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/3 "2023-08-07T15:29:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 7, 2023, 3:30pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/4 "2023-08-07T15:30:46Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)\
**Post date:** [August 7, 2023, 3:32pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/5 "2023-08-07T15:32:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)\
**Post date:** [August 7, 2023, 3:35pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/6 "2023-08-07T15:35:52Z")

</div>

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

```auto
                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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 7, 2023, 3:55pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/7 "2023-08-07T15:55:56Z")

</div>

I guess you need to do something like that:

```auto
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 😉

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [August 8, 2023, 9:57am UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/8 "2023-08-08T09:57:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![toddcarv](https://avatars.discourse-cdn.com/v4/letter/t/779978/32.png) [@toddcarv](https://discuss.elastic.co/u/toddcarv)\
**Post date:** [August 8, 2023, 12:35pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/9 "2023-08-08T12:35:40Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 5, 2023, 12:36pm UTC](https://discuss.elastic.co/t/new-java-api-client-getresponse/340287/10 "2023-09-05T12:36:26Z")

</div>

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