MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("app",app);
SearchQuery query = new NativeSearchQueryBuilder()
.withIndices(index)
.withTypes(type)
.withQuery(matchQueryBuilder)
.withPageable(page)
.build();
long scollTimeInMillis = 1000;
String scrollId = elasticsearchTemplate.scan(query, scollTimeInMillis, false);
boolean hasRecords = true;
long totalCount = 0;
while (hasRecords) {
Page<LogDetail> page = elasticsearchTemplate.scroll(scrollId, scollTimeInMillis, LogDetail.class);
if (page.hasContent()) {
LOGGER.info("page number:{}",page.getNumberOfElements());
totalCount += page.getNumberOfElements();
} else {
hasRecords = false;
}
}
//clear scroll
elasticsearchTemplate.clearScroll(scrollId);
public FacetedPageWithScore searchResumeByConditionForPage(final SearchQuery searchQuery) {
return elasticsearchTemplate.query(searchQuery, new ResultsExtractor<FacetedPageWithScore>() {
@Override
public FacetedPageWithScore extract(SearchResponse response) {
final List<SearchResumeWithScore> scoreResumeList = new ArrayList<>();
ResultsMapper mapper = new DefaultResultMapper(elasticsearchTemplate.getElasticsearchConverter().getMappingContext());
FacetedPage<SearchResume> searchResult = mapper.mapResults(response, SearchResume.class, searchQuery.getPageable());
List<SearchResume> content = searchResult.getContent();
Map<Long, SearchResume> mapSearchResume = new HashMap<>();
for (SearchResume searchResume: content){
mapSearchResume.put(searchResume.getId(), searchResume);
}
SearchHits searchHits = response.getHits();
for(SearchHit searchhit:searchHits){
Long id = (Long) searchhit.getSource().get("id");
SearchResumeWithScore scoreResume = new SearchResumeWithScore();
scoreResume.setSearchResume(mapSearchResume.get(id));
// scoreResume.setScore(searchhit.getScore()) 会出现NaN异常
try{
float score = searchhit.getScore();
if(score > 0){
scoreResume.setScore(score);
}else{
scoreResume.setScore(0.0f);
}
}catch(Exception e){
scoreResume.setScore(0.0f);
}
scoreResumeList.add(scoreResume);
}
FacetedPageWithScore facetedPageWithScore = new FacetedPageWithScore(scoreResumeList, searchResult.getTotalPages());
return facetedPageWithScore;
}
});
}
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("app",app);
SearchQuery query = new NativeSearchQueryBuilder()
.withIndices(index)
.withTypes(type)
.withQuery(matchQueryBuilder)
.withPageable(page)
.build();
long scollTimeInMillis = 1000;
String scrollId = elasticsearchTemplate.scan(query, scollTimeInMillis, false);
boolean hasRecords = true;
long totalCount = 0;
while (hasRecords) {
Page<LogDetail> page = elasticsearchTemplate.scroll(scrollId, scollTimeInMillis, LogDetail.class);
if (page.hasContent()) {
LOGGER.info("page number:{}",page.getNumberOfElements());
totalCount += page.getNumberOfElements();
} else {
hasRecords = false;
}
}
//clear scroll
elasticsearchTemplate.clearScroll(scrollId);