Retrieving stored fields using java client

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

  1. creating template:
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
POST test-tf-index/_create/ds1
{
  "name": "ds-1",
  "inference": "sample text"
}
  1. retrieving stored fields via kibana (works with no issues)
GET test-tf-index/_search
{
  "_source": ["*"],
  "stored_fields": ["inference"]
}
  1. Tried doing the same with java, but getting parser exception.
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);
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.

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.

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:

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

Response:

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

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