# Retrieving stored fields using java client

**URL:** <https://discuss.elastic.co/t/retrieving-stored-fields-using-java-client/339812>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 1, 2023, 2:26pm UTC](https://discuss.elastic.co/t/retrieving-stored-fields-using-java-client/339812 "2023-08-01T14:26:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jagadeesh12](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jagadeesh12/32/124186_2.png) [@Jagadeesh12](https://discuss.elastic.co/u/Jagadeesh12)\
**Post date:** [August 1, 2023, 2:26pm UTC](https://discuss.elastic.co/t/retrieving-stored-fields-using-java-client/339812/1 "2023-08-01T14:26:27Z")

</div>

Hi,  
I am facing issues while retrieving stored fields from Elasticsearch using java client.

1. creating template:

```auto
PUT _index_template/test_tf_template
{
  "index_patterns": ["test-tf-*"],
  "template": {
    "mappings": {
      "_source": {
        "excludes": ["inference"]
      },
      "properties": {
        "inference": {
          "type": "text",
          "store": true
        }
      }
    }
  }
}

```

1. indexing a document via kibana

```auto
POST test-tf-index/_create/ds1
{
  "name": "ds-1",
  "inference": "sample text"
}

```

1. retrieving stored fields via kibana (works with no issues)

```auto
GET test-tf-index/_search
{
  "_source": ["*"],
  "stored_fields": ["inference"]
}

```

1. Tried doing the same with java, but getting parser exception.

```auto
String indexName = "test-tf-index";
SearchRequest request = SearchRequest.of(s -> s.index(indexName)
                .source(sc -> sc.filter(v -> v.includes(List.of("name"))))
                .storedFields(List.of("inference"))
);

SearchResponse<Map> response= client.search(request, Map.class);
System.out.println("response : " + response);

```

```auto
Exception Trace:
jakarta.json.stream.JsonGenerationException: Illegal method during JSON generation, not valid in current context IN_OBJECT
	at org.eclipse.parsson.JsonGeneratorImpl.checkContextForValue(JsonGeneratorImpl.java:405)
	at org.eclipse.parsson.JsonGeneratorImpl.write(JsonGeneratorImpl.java:357)
	at co.elastic.clients.json.JsonpUtils.copy(JsonpUtils.java:186)
	at co.elastic.clients.json.JsonpUtils.copy(JsonpUtils.java:180)
	at co.elastic.clients.json.JsonpUtils.copy(JsonpUtils.java:157)
	at co.elastic.clients.json.jackson.JacksonJsonBuffer.serialize(JacksonJsonBuffer.java:98)
	at co.elastic.clients.elasticsearch.core.search.Hit.serializeInternal(Hit.java:319)
	at co.elastic.clients.elasticsearch.core.search.Hit.serialize(Hit.java:293)
	at co.elastic.clients.elasticsearch.core.search.HitsMetadata.serializeInternal(HitsMetadata.java:130)
	at co.elastic.clients.elasticsearch.core.search.HitsMetadata.serialize(HitsMetadata.java:115)
	at co.elastic.clients.elasticsearch.core.search.ResponseBody.serializeInternal(ResponseBody.java:248)
	at co.elastic.clients.elasticsearch.core.search.ResponseBody.serialize(ResponseBody.java:232)
	at co.elastic.clients.json.JsonpUtils.toString(JsonpUtils.java:409)
	at co.elastic.clients.json.JsonpUtils.toString(JsonpUtils.java:346)
	at co.elastic.clients.elasticsearch.core.search.ResponseBody.toString(ResponseBody.java:311)
	at java.base/java.lang.StringConcatHelper.stringOf(StringConcatHelper.java:453)
	at java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:408)
	.....
	Suppressed: jakarta.json.stream.JsonGenerationException: Generating incomplete JSON
		at org.eclipse.parsson.JsonGeneratorImpl.close(JsonGeneratorImpl.java:519)
		at co.elastic.clients.json.JsonpUtils.toString(JsonpUtils.java:408)
		... 6 more

```

Elastic Search v8.9.0  
Java client 8.8.1

Please let me know if there is a way to fix this or if I missed something.  
Thank you.

---

<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 1, 2023, 5:05pm UTC](https://discuss.elastic.co/t/retrieving-stored-fields-using-java-client/339812/2 "2023-08-01T17:05:59Z")

</div>

Welcome!

It's because you can not toString this I think.  
Remove the last line of your code and I believe that will be ok.

---

<div class="post-metadata">

**Author:** ![Jagadeesh12](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jagadeesh12/32/124186_2.png) [@Jagadeesh12](https://discuss.elastic.co/u/Jagadeesh12)\
**Post date:** [August 1, 2023, 5:37pm UTC](https://discuss.elastic.co/t/retrieving-stored-fields-using-java-client/339812/3 "2023-08-01T17:37:21Z")

</div>

Thanks for the reply.  
I removed that line which is now not throwing the exception. But the root cause is still not solved. I believe the response object holds the exception caused while parsing and I am merely printing above. This is happening because of the existence of "fields" in Elasticsearch response. If I do not query for stored fields, I am getting a properly parsed response.

Query:

```auto
GET test-tf-index/_search
{
  "size": 1, 
  "_source": ["*"],
  "stored_fields": ["inference"]
}

```

Response:

```auto
"hits": [
      {
        "_index": "test-tf-index",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "ds-1",
        },
        "fields": { // causing parser exception.
          "inference": [
            "sample text"
          ]
        }
      }
    ]

```

---

<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:** [August 29, 2023, 5:37pm UTC](https://discuss.elastic.co/t/retrieving-stored-fields-using-java-client/339812/4 "2023-08-29T17:37:52Z")

</div>

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