Elasticsearch MultiGet working through curl, but no results are returned through Java API

I am running elasticsearch 2.3.4, but the syntax does not seem to have changed in 5.x.

Multiget over curl is working just fine. Here is what my curl looks like:

curl 'localhost:9200/_mget' -d '{
    "docs" : [
        {
            "_index" : "logs-2017-04-30",
            "_id" : "e72927c2-751c-4b33-86de-44a494abf78f"
        }
    ]
}'

And when I want to pull the "message" field off that response, I use this request:

curl 'localhost:9200/_mget' -d '{
    "docs" : [
        {
            "_index" : "logs-2017-04-30",
            "_id" : "e72927c2-751c-4b33-86de-44a494abf78f",
            "fields" : ["message"]
        }
    ]
}'

Both of the above queries return the log and information that I am looking for.

But when I try to translate it to Java like this:

MultiGetRequestBuilder request = client.prepareMultiGet();
request.add("logs-2017-04-30", null, "e72927c2-751c-4b33-86de-44a494abf78f");
MultiGetResponse mGetResponse = request.get();
for (MultiGetItemResponse itemResponse : mGetResponse.getResponses()) {
  GetResponse response = itemResponse.getResponse();
  logger.debug("Outputing object: " + ToStringBuilder.reflectionToString(response));
}

I appear to be getting null objects back. When I try to grab the message field off the null-looking GetResponse object, nothing is there:

GetField field = response.getField("message"); <--- returns null

What am I doing wrong? doing a rest call to elasticsearch proves the log exists, but my Java call is wrong somehow.

The documentation page for the Java multi get completely skips over the extra syntax required to retrieve data beyond the _source field. Just like the REST API, doing a multi get with the minimum information required to locate a log gets very limited information about it. In order to get specific fields from a log in a multi get call through the Java API, you must pass in a MultiGetRequest.Item to the builder. This item needs to have the fields you want specified in it before you execute the request.

Here is the code change (broken into multiple lines for clarity) that results in the fields I want being present when I make the query:

MultiGetRequestBuilder request = client.prepareMultiGet();
MultiGetRequest.Item item = new MultiGetRequest.Item("logs-2017-04-30", "null", "e72927c2-751c-4b33-86de-44a494abf78f");
item.fields("message");
request.add(item);
MultiGetResponse mGetResponse = request.get();

Now I can ask for the field I specified earlier:

GetField field = response.getField("message");

I would argue that the document page should be marked as incomplete so future readers do not get lost when they try to access the fields of their logs.

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