hello ,I use the search API,just want to get some fileds and set the addFields method.like this,
client.prepareSearch(index).setTypes(type).addFields(fields).setQuery(queryBuilder).setSize(sizeLimit).setExplain(true).get();
but when I get the result that is null.
for(SearchHit hit:response.getHits().getHits()){
Map<String, Object> hitsMap = hit.getSource();
log.info("[ "+hitsMap+" ]");
}
I try to set the setSearchType(SearchType.QUERY_AND_FETCH) method ,or set the QUERY_THEN_FETCH ....but it does not work.
version:2.3.0
How did you solve it?
Actually I ignore something. When we set the addFields method,we can't fetch the result use that kind of method. It should be like this.
[Code]:
for(SearchHit hit:response.getHits().getHits()){
if( null != fields ){
Set<Entry<String, SearchHitField>> fieldEntry = hit.getFields().entrySet();
JSONObject json = new JSONObject();
for(Entry<String, SearchHitField> entry:fieldEntry){
json.put(entry.getValue().getName(), entry.getValue().getValue());
}
log.info("[ "+json+" ]");
}else{
log.info(hit.getSourceAsString());
}
}
}