Find top order by date desc issue

1.7k views Asked by At

My elasticsearch version is: "2.4.2", spring boot version is: "1.4.2.RELEASE".

My repository:

public interface StockDetailsEsRepository extends ElasticsearchRepository<StockDetails, String> {

    StockDetails findTopByOrderByDateDesc();
}

StockDetails Class:

@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;

The problem is when I use the query from my repository I get following exception:

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)

I debug the ElasticsearchTemplate.queryForPage method and the argument CriteriaQuery criteriaQuery is null. The Same problem occurs when I change the method name in my repository to findTopByOrderByStockTickerDesc. It's wired because in official spring data elasticsearch documentation is almost the same example http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories.limit-query-result

findTopByOrderByAgeDesc(); 

Ofcourse I can achieve my goal in other way for example:

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

But I would like use method which is describe in official documentation. Has someone similar problem?

0

There are 0 answers