springelasticsearch

How to query elasticsearch from spring with LocalDate


Having this SearchQuery:

final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.rangeQuery("updateTime").gte(LocalDate.now())).build();
final List<ActivityValue> listOf = elasticsearchTemplate.queryForList(searchQuery, ActivityValue.class);

With Entity ActivityValue:

@Document(indexName = "tracking1", type = "activity")
public class ActivityValue {

@Id
private String id;

@Field(type = FieldType.Date, index = false, store = true, format = DateFormat.custom, pattern = "yyyy-MM-dd")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate updateTime;

@Field(type = FieldType.Object, includeInParent = true)
private Vendor vendor;

@Field(type = FieldType.Object, includeInParent = true)
private QCriteria quality;

public ActivityValue() {
}

//setter and getter
}

If i run the query and try to receive the list i get following exception:

caused by: java.io.IOException: can not write type [class java.time.LocalDate]

The entity is stored before with the actual date as LocalDate. I'm uncertain what is the best/easiest way to query elasticsearch and to resolve this error. Can anybody help?


Solution

  • final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.rangeQuery("updateTime").gte(LocalDate.now().toString())).build();
    final List<ActivityValue> listOf = elasticsearchTemplate.queryForList(searchQuery, ActivityValue.class);
    

    I solved this problem by using LocalDate.now().toString() instead of LocalDate.now()