Elasticsearch 6.1.0 Java API search issues

I am using ES - 6.1 and corresponding java version of the api. I was able to index the data using the java API. I validated this via kibana. All the mappings were correctly saved but an error comes up when I try to search. Even the most basic search returns me an empty collection.

// client is a reference to the TransportClient instance
SearchResponse searchResponse = this.client.prepareSearch(index).get().

When I try get the fields, by getFields() and further do a getField(key) I get a null pointer exception. I tried troubleshooting and saw that in the response there are no objects being returned. Following is the basic search I am doing.

// index is the index I am interested to search under.
SearchResponse searchResponse = this.client.prepareSearch(index).get();

This is an issue I see when I tried migrating my code to latest version from 1.7.3 Java API. Earlier, there are no issues found with our implementation.

Please help me out on this.

-Ajit

1 Like

I believe getFields() in 6.x will only return fields that have been
explicitly set as stored. This behavior might have started in 2.x. Only the
_source field is stored and values can be retrieved from it. If you are
retrieve multiple fields, you are better off just getting the source, which
would require only one disk seek.

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html

Hi Ivan,

Thanks for that suggestion. I have re-indexed my data with each of the property set with stored mapping. I still do not get response with the fields I am interested in. The fields Keyset is still a null value

Following is the mapping sample.

"street": {
    "type": "text",
    "store": true
},
"country": {
    "type": "text",
    "store": true
},
"city": {
    "type": "text",
    "store": true
}

Here is response for each hit I get when I troubleshoot. I am getting the right number of hits in that index . Attaching the screenshot of my variables from debugger. hits is the variable containing response from following piece of code.

hits = searchResponse.getHits()

Thank You for the help.

-Ajit

Hi All,

The issue is identified.

Unlike the previous versions, the new version will get the hits in a map in a different method.

//in previous version (1.7.3)
hit.getFields() 
// in new version (6.1.0)
hit.getSourceAsMap()

Further on, we can deserialize the response just the way we had been doing it earlier.

Thanks everyone for stopping and (hopefully) trying to solve the problem.

-Ajit

1 Like

Sorry if my suggestion did not work. Assumed that was the issue.

Getting the data from the source is not the same as getting it from the
stored fields. In the majority of cases, it is the preferred way, so your
new method should be ideal (it depends on the number/type of fields
returned). You do not need to store the individual fields in your mapping
if retrieving the fields from the source.

Thank you for this solution. We spent the better part of a day debugging this after an upgrade from 5.5.0 to 6.1.1. Has anyone been able to find specific documentation around this change?

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