# Stored fields and SearchResponse

**URL:** <https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 31, 2023, 2:40pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062 "2023-08-31T14:40:35Z")\
**Posts on this page:** 9\
**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 31, 2023, 2:40pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/1 "2023-08-31T14:40:35Z")

</div>

Example:

```auto
SearchResponse<ObjectNode> searchResponse = elasticsearchClient.search(searchRequest, ObjectNode.class);
Hit<ObjectNode> hit = searchResponse.hits().hits().get(0);
Map<String, JsonData> fields = hit.fields();
JsonData data = fields.get("somefield");

```

What is the correct way to turn 'data' into a json string for further processing?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [September 1, 2023, 9:53am UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/2 "2023-09-01T09:53:52Z")

</div>

Hi @toddcarv,

Per chance are you using the [Elasticsearch Java client](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonData.html)?

Have you tried looking at the [`JsonpMapper`](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonpMapper.html) to convert `data` into an object for further processing [as suggested in the documentation](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonData.html)?

> A raw JSON value. It can be converted to a JSON node tree or to an arbitrary object using a [`JsonpMapper`](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonpMapper.html).

---

<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:** [September 5, 2023, 2:39pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/3 "2023-09-05T14:39:39Z")

</div>

Thanks. Yes I'm using the Java client. And yes I read the doc. It also says " Instances of this class returned by API clients keep a reference to the client's [`JsonpMapper`](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonpMapper.html) and can be converted to arbitrary types using [`to(Class)`](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonData.html#to(java.lang.Class)) without requiring an explicit mapper." I ran a test and I'm able to use the following to get the json string from a JsonData.

```auto
String json = fields.get("thefield").toJson().toString();

```

Using this doesn't seem to work:

```auto
String json = fields.get("thefield").to(String.class);

```

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [September 5, 2023, 2:50pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/4 "2023-09-05T14:50:49Z")

</div>

Looking at the JavaDoc for `to(Class)` it states that you need to specify a mapper:

> Converts this object to a target class. A mapper must have been provided at creation time.  
> Throws:  
> `java.lang.IllegalStateException` - if no mapper was provided at creation time.

Can you see if specifying an explicit mapper works?

---

<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:** [September 5, 2023, 3:00pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/5 "2023-09-05T15:00:34Z")

</div>

Am I missing something? It says I shouldn't have to if the instance was returned by the API client - whiich it was.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [September 5, 2023, 3:22pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/6 "2023-09-05T15:22:26Z")

</div>

I'm slightly confused myself. Can you share the link to the location in the docs where it says an explicit mapper isn't needed?

---

<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:** [September 5, 2023, 3:28pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/7 "2023-09-05T15:28:30Z")

</div>

From two posts ago:

> [@toddcarv](#):
>
> without requiring an explicit mapper

> **[JsonData (java-client 8.1.0 API)](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonData.html)**
>
> declaration: package: co.elastic.clients.json, interface: JsonData

---

<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:** [September 5, 2023, 7:00pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/8 "2023-09-05T19:00:34Z")

</div>

Specifically I'm trying to figure out how do access the value of a stored field. It used to be like this with the HLRC:

```auto
DocumentField field = fields.get("thefield");
String value = field.getValue();

```

Now?

```auto
JsonData jsonData = fields.get("thefield");
// How to get the first string value?

```

---

<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:** [October 3, 2023, 7:01pm UTC](https://discuss.elastic.co/t/stored-fields-and-searchresponse/342062/9 "2023-10-03T19:01:21Z")

</div>

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