I get NPE when I want to get nbHits, and I cannot get the value of any other field. But, when I disable this line, I get responses without error:
nbHits += response.getHits().getTotalHits();`
there is my whole code :
public static void requestBuilder(ArrayList<String> formulae) {
ArrayList<SearchRequestBuilder> srb = new ArrayList<SearchRequestBuilder>(formulae.size());
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("localhost",9300)));
int i = 0;
MultiSearchRequestBuilder sr = client.prepareMultiSearch();
for (String formula : formulae) {
srb.add(client.prepareSearch(index).setSource(formula));
sr.add(srb.get(i));
i += 1;
}
MultiSearchResponse resp = sr.execute().actionGet();
long nbHits = 0;
for (MultiSearchResponse.Item item : resp.getResponses()){
SearchResponse response = item.getResponse();
// nbHits += response.getHits().getTotalHits();
System.out.println(response);
}
client.close();
}
But when I use the following code, I can get get results from any field:
SearchResponse response = client.prepareSearch()
.setIndices(index)
.setSource(formulae.get(1))
.execute()
.actionGet();
for (SearchHit hit : response.getHits()) {
String get = hit.getSource().get("text").toString();
System.out.println(get);
}
How can I do the same with MultiSearch Api?