【问题标题】:elasticsearch findAll with pageableelasticsearch findAll 可分页
【发布时间】:2018-04-12 07:40:07
【问题描述】:

我需要获取所有可分页的文档,但出现此错误

com.fasterxml.jackson.databind.JsonMappingException: (是 java.lang.NullPointerException) (通过引用链: org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl["facets"])

存储库

public interface ConventionSearchRepository extends ElasticsearchRepository<Convention, Long> {
}

服务

@Service
@Transactional
public class ConventionServiceImpl implements ConventionService {

  private final ConventionSearchRepository conventionSearchRepository;

   @Override
   @Transactional(readOnly = true)
   public Page<Convention> findAll(final PageRequest pageable) {
     return conventionSearchRepository.findAll(pageable);
   }

}

控制器

  @GetMapping("/conventions")
  public Page<Convention> getAllConventions(@RequestParam(value = "_page", required = false, defaultValue = "0") int page,
      @RequestParam(value = "_perPage", required = false, defaultValue = "20") int perPage,
      @RequestParam(value = "_sortDir", required = false, defaultValue = "DESC") Sort.Direction sortDir,
      @RequestParam(value = "_sortBy", required = false, defaultValue = "shortname") String sortBy) {
    Sort sort = new Sort(sortDir, sortBy);
    PageRequest pageRequest = new PageRequest(page, perPage, sort);
    return conventionService.findAll(pageRequest);
  }

【问题讨论】:

    标签: elasticsearch spring-data-elasticsearch


    【解决方案1】:

    我可以简单地向我的@Controller 声明一个 org.springframework.data.domain.Pageable 参数,然后将它直接传递给我的服务,然后最终传递给@Repository(我使用 CustomRepository 扩展它)。 Spring 负责填充有关排序参数等的所有详细信息。我不必详细定义单独的 @RequestParams。

    这是一个例子

    public Page<LogEntry> findAllLogEntries(@PageableDefault(page = DEFAULT_PAGE, size = DEFAULT_PAGE_SIZE) @SortDefault.SortDefaults({@SortDefault(sort = "@timestamp", direction = Direction.DESC)}) Pageable pageable) {
    

    我正在使用 Spring 5 和 Spring Data Elasticsearch 3.0.5.RELEASE

    【讨论】:

      猜你喜欢
      • 2017-04-18
      • 2018-06-17
      • 1970-01-01
      • 2016-06-23
      • 2016-06-13
      • 1970-01-01
      • 2020-11-07
      • 2021-04-21
      • 1970-01-01
      相关资源
      最近更新 更多