elasticsearch: return TotalPages not correct

好久不见. 提交于 2020-01-06 02:35:09

问题


I have 107 documents in my index base, i created a method to return all these documents with pagination, in my case the first page contains 20 documents and i logically get 6 pages, the 5 first pages contain 20 documents each and the 6th page contains only 7. The problem is that the methods reeturn always 1 page not 6

  @Override
  @Transactional(readOnly = true)
  public Page<Convention> findAll(Pageable pageable) throws UnknownHostException {
    String[] parts = pageable.getSort().toString().split(":");
    SortOrder sortOrder;
    if ("DESC".equalsIgnoreCase(parts[1].trim())) {
      sortOrder = SortOrder.DESC;
    } else {
      sortOrder = SortOrder.ASC;
    }
    SearchResponse searchResponse = elasticsearchConfiguration.getTransportClient()
     .prepareSearch("convention")
     .setTypes("convention")
     .setQuery(QueryBuilders.matchAllQuery())
     .addSort(SortBuilders.fieldSort(parts[0])
     .order(sortOrder))
     .setSize(pageable.getPageSize())
     .setFrom(pageable.getPageNumber() * pageable.getPageSize())
     .setSearchType(SearchType.QUERY_THEN_FETCH)
     .get();

    return searchResults(searchResponse);
  }

  private Page<Convention> searchResults(SearchResponse searchResponse) {
    List<Convention> conventions = new ArrayList<>();
    for (SearchHit hit : searchResponse.getHits()) {
      if (searchResponse.getHits().getHits().length <= 0) {
        return null;
      }
      String sourceAsString = hit.getSourceAsString();
      if (sourceAsString != null) {
        ObjectMapper mapper = new ObjectMapper();
        Convention convention = null;
        try {
          convention = mapper.readValue(sourceAsString, Convention.class);
        } catch (IOException e) {
          LOGGER.error("Error", e);
        }
        conventions.add(convention);
      }
    }
    return new PageImpl<>(conventions);
  }

http://localhost:8081/api/conventions?page=0&size=20&sort=shortname,DESC

When i execute this api, i have TotalElements=20, Number=0, TotalPages=1, and Size=0

@GetMapping("/conventions")
  public ResponseEntity<List<Convention>> getAllConventions(final Pageable pageable) throws UnknownHostException {
    final Page<Convention> page = conventionService.findAll(pageable);
    System.out.println("-------------- 1:" + page.getTotalElements()); // 20
    System.out.println("-------------- 2:" + page.getNumber()); // 0
    System.out.println("-------------- 3:" + page.getTotalPages()); // 1
    System.out.println("-------------- 4:" + page.getSize()); // 0
    HttpHeaders headers = new HttpHeaders();
    headers.add("X-Total-Count", Long.toString(page.getTotalElements()));
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
  }

回答1:


This issue is addressed and fixed in current stable version of spring-data-elasticsearch 3.0.7

See https://jira.spring.io/browse/DATAES-402




回答2:


i think it comes from this line: return new PageImpl<>(conventions);

Maybe you should transfer the total size of the responshits, because you override the query.



来源:https://stackoverflow.com/questions/49896753/elasticsearch-return-totalpages-not-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!