按日期desc问题查找最高订单

时间:2016-12-27 19:54:35

标签: java elasticsearch spring-boot spring-data spring-data-elasticsearch

我的弹性搜索版本是:“2.4.2”, spring boot版本是:“1.4.2.RELEASE”。

我的存储库:

public interface StockDetailsEsRepository extends ElasticsearchRepository<StockDetails, String> {

    StockDetails findTopByOrderByDateDesc();
}

StockDetails类:

@Document(indexName = "stock_details", type = "daily", replicas = 0)
public class StockDetails {

    @Id
    private String id;

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd")
    @JsonDeserialize(using = CustomLocalDateDeserializer.class)
    @JsonSerialize(using = CustomLocalDateSerializer.class)
    private LocalDate date;

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    @JsonDeserialize(using = BigDecimalDeserializer.class)
    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal openPrice;

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    @JsonDeserialize(using = BigDecimalDeserializer.class)
    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal maxPrice;

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    @JsonDeserialize(using = BigDecimalDeserializer.class)
    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal minPrice;

    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    @JsonDeserialize(using = BigDecimalDeserializer.class)
    @JsonSerialize(using = BigDecimalSerializer.class)
    private BigDecimal closePrice;

    @Field(type = FieldType.Long)
    private Long transactionsNumber = 0L;

    @Field(type = FieldType.Long)
    private Long volume;

    private Stock stock;

问题是当我使用来自我的存储库的查询时,我得到以下异常:

java.lang.NullPointerException: null
    at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForPage(ElasticsearchTemplate.java:308)
    at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.queryForObject(ElasticsearchTemplate.java:252)

我调试ElasticsearchTemplate.queryForPage方法,参数CriteriaQuery criteriaQuery为null。当我将我的存储库中的方法名称更改为findTopByOrderByStockTickerDesc时,会出现相同的问题。它是有线的,因为在官方的春季数据中,elasticsearch文档几乎是同一个例子http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.limit-query-result

findTopByOrderByAgeDesc(); 

当然,我可以用其他方式实现我的目标,例如:

Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "date"));
        StockDetails topByOrderByDateDesc = stockDetailsEsRepository.findAll(new PageRequest(0, 1, sort))
            .getContent()
            .stream()
            .findFirst()
            .get();

但我想使用官方文档中描述的方法。有人有类似的问题吗?

0 个答案:

没有答案