elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Can't use _source array with SearchRequest withJson

Open cgendreau opened this issue 3 years ago • 3 comments

Java API client version

7.17.6

Java version

17

Elasticsearch Version

7.17.6

Problem description

I'm trying to create a SearchRequest from a search request expressed as json using the withJson method.

SearchRequest sr = SearchRequest.of(b -> b .withJson(strReader).index("myindex"));

Content of json string in strReader:

{
  "_source": [ "obj1.*", "obj2.*" ],
  "query": {
    "match": {
      "user.id": "kimchy"
    }
  }
}

Used with withJson on SearchRequest throws:

co.elastic.clients.json.JsonpMappingException: Error deserializing co.elastic.clients.elasticsearch.core.SearchRequest: co.elastic.clients.json.UnexpectedJsonEventException: Unexpected JSON event 'START_ARRAY' instead of '[START_OBJECT, KEY_NAME, VALUE_STRING, VALUE_TRUE, VALUE_FALSE]' (JSON path: _source) (line no=2, column no=15, offset=16)

It is working if I limit the _source to 1 value (no array) like "_source": "obj1.*"

cgendreau avatar Sep 16 '22 12:09 cgendreau

faces same issue with v8.5.3

mhanna avatar Jan 08 '23 12:01 mhanna

same issue with v8.6.2

Upd: A workaround is to use object notation:

"_source": {
    "includes": ["obj1.*", "obj2.*"]
}

marc- avatar Apr 12 '23 18:04 marc-

I have the same problem, using a slightly different method to create the search request:

SearchRequest sr = new SearchRequest.Builder()
	.withJson(new StringReader(json.toString()))
	.index(index)
	.build();

Thank you, @cgendreau, for pointing out how it works with a single value. This is a non-obvious behaviour. The builder transforms "_source": "fieldName" from the json into "_source": ["fieldName"] in the request.

That helped me in a special case. But of cause it doesn't help if more than one field is needed.

Edit: @marc- you're right, this helps as well for more than one field. Didn't read your update carefully. Anyway, "_source": [ "field1", "field2" ] should be made working as well.

truj avatar May 04 '23 08:05 truj