如何使用LocalDate从Spring查询Elasticsearch

时间:2018-08-27 07:05:12

标签: spring elasticsearch

具有此SearchQuery:

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

具有实体ActivityValue:

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

如果我运行查询并尝试接收列表,则会出现以下异常:

do {
  try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
  try AVAudioSession.sharedInstance().setActive(true)
  UIApplication.shared.beginReceivingRemoteControlEvents()
} catch {
  //handle error
  print(error)
}

该实体存储之前,实际日期为LocalDate。 我不确定什么是查询elasticsearch并解决此错误的最佳/最简便方法。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

我通过使用LocalDate.now().toString()而不是LocalDate.now()来解决了这个问题