SearchHit[] searchHits1 = searchHits.get();
//方式一
for (int i = 0; i < searchHits1.length; i++) {
Map<String, Object> source = searchHits1[i].getSource();
list.add(source);
}
//方式二
if (searchHits.isPresent()) {
Arrays.asList(searchHits.get()).parallelStream().forEach(InternalSearchHit -> list.add(InternalSearchHit.getSource()));
}
Logic of the code library is isolated from es specified data, and then stored in the set list, returns a page or otherwise processed; The problem here is that, when the second operating mode, the list will be null data, as follows Figure:
The presence of empty data, it will cause all kinds of unexpected bug, alas, headache ah;
A display into the normal mode;
Why?
The reason: I look carefully. . .
Found, the cause of the stream, and replaced with the following code on it:
if (searchHits.isPresent()) { Arrays.asList(searchHits.get()).stream().forEach(InternalSearchHit -> list.add(InternalSearchHit.getSource())); }
Here we must talk about the difference of the flow: