如何在java中使用elasticsearchrepository实现elasticsearch分页逻辑

ippsafx7  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(0)|浏览(581)

是否有任何方法可以使用elasticsearchrepository,它从pagingandsortingrepository扩展而来。这允许内置分页和排序支持。但我无法将我的实现更改为使用elasticsearchrepository。我只想知道如何申请:
如何使用后期搜索
如何使用esquery,它基本上提供了search query string esquery=string.format(searchtextquery,startdate,enddate,formattedquerys);
如何使用我得到的post uri,如下所示:request request=new request(“get”,“/”+user.getuserid()+“/\u search”);因此,有了以上这些,如何使用elasticsearchrepository的分页,下面是我的服务代码:

public List getResponses(ZonedDateTime startDate, ZonedDateTime endDate, 
String cat, FieldFilterVM filter, String query) throws IOException {
User user = (User) 
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Request request = new Request("GET", "/" + user.getUserId() + "/_search");
List<String> matchQueries = new ArrayList<>();
matchQueries.addAll(formatCategoryQuery(cat));
matchQueries.addAll(formatFilterQuery(filter, false));
if (query != null && query.length() > 0) {
    matchQueries.add(String.format(textFilterQuery, query));
}
StringBuilder formattedQueries = new StringBuilder();
for (int i = 0; i < matchQueries.size(); i++) {
    formattedQueries.append(',');
    formattedQueries.append(matchQueries.get(i));
}
String esQuery = String.format(searchTextQuery, startDate, endDate, 
formattedQueries);
request.setJsonEntity(esQuery);
Response response = 
elasticSearchService.getClient().getLowLevelClient().performRequest(request);
String responseBody = IOUtils.toString(response.getEntity().getContent(), 
"UTF-8");
ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(responseBody, new TypeReference<Map>() {
});
List matchedTextResponses = new ArrayList();
if (map != null) {
    List<Map> textResponses = (List<Map>) ((Map) 
map.get("hits")).get("hits");
    for (Map textResponse : textResponses) {

    }
}
return matchedTextResponses;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题