首页 > 解决方案 > SolrJ 和 LocalDate 和 LocalDateTime

问题描述

SolrJ 将 LocalDate 和 LocalDateTime 序列化,但它们不能作为日期(或日期范围)进行搜索,而是作为字符串进行搜索。如果想要让它可搜索,则必须在使用 SolrJ 之前将其转换为 java.util.Date。

有没有办法为 SolrJ 定义自定义序列化程序或将 LocalDate 和 LocalDateTime 也标记为日期字段(因此可搜索)?

谢谢!

编辑:代码示例

String zkHosts = "localhost:2181";
CloudSolrClient client = new CloudSolrClient.Builder().withZkHost(zkHosts).build();
client.setDefaultCollection("playground");

Person test = Person.builder().id("UID-333").createdTimestamp(LocalDateTime.now()).dateOfBirth(LocalDate.now()).gender("male").build();
client.addBean("person", test, 1000);

client.close();

这是保存在 Solr 中的内容:

{
  "id":"UID-333",
  "dateOfBirth":["java.time.LocalDate:2018-05-15"],
  "dateOfBirth_str":["java.time.LocalDate:2018-05-15"],
  "gender":["male"],
  "gender_str":["male"],
  "timestamp":["java.time.LocalDateTime:2018-05-15T15:00:12.433"],
  "timestamp_str":["java.time.LocalDateTime:2018-05-15T15:00:12.433"]
}

当然,实体的相应属性用 注释@Field

标签: solrsolrj

解决方案


推荐阅读