【问题标题】:Spring data elastic search findAll with OrderBySpring数据弹性搜索findAll与OrderBy
【发布时间】:2015-11-22 15:13:36
【问题描述】:

我正在使用 spring data 的弹性搜索模块,但在构建查询时遇到了麻烦。这是一个非常简单的查询。 我的文档如下所示:

@Document(indexName = "triber-sensor", type = "event")
public class EventDocument implements Event {

    @Id
    private String id;
    @Field(type = FieldType.String)
    private EventMode eventMode;
    @Field(type = FieldType.String)
    private EventSubject eventSubject;
    @Field(type = FieldType.String)
    private String eventId;
    @Field(type = FieldType.Date)
    private Date creationDate;
}

Spring 数据仓库长这样:

public interface EventJpaRepository extends ElasticsearchRepository<EventDocument, String> {

    List<EventDocument> findAllOrderByCreationDateDesc(Pageable pageable);

}

所以我试图让所有事件按creationDate 排序,并首先获取最新事件。但是,当我运行代码时,我得到一个异常(也在 STS 中):

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property desc found for type Date! Traversed path: EventDocument.creationDate.

所以它似乎没有选择“OrderBy”部分?然而,带有 findBy 子句的查询(例如 findByCreationDateOrderByCreationDateDesc)似乎没问题。也没有订购的 findAll 作品。 这是否意味着spring data的弹性搜索模块不允许findAll with ordering?

【问题讨论】:

  • 以下确实有效Page&lt;EventDocument&gt; findAll = eventJpaRepository.findAll(new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), new Sort(Direction.DESC, "creationDate"))); return findAll.getContent()。但我觉得它很丑

标签: spring-data spring-data-elasticsearch


【解决方案1】:

尝试将 By 添加到方法名称:
findAllByOrderByCreationDateDesc

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 2018-06-08
    • 2023-04-01
    • 2021-11-23
    • 1970-01-01
    • 2018-02-01
    • 2018-05-07
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多