最简单的可能是使用SimpleStringCriteria
String start = JavaDateConverter.INSTANCE.convert(sc.getStartDate())+"/DAY";
String end = JavaDateConverter.INSTANCE.convert(sc.getEndDate())+"/DAY";
Criteria criteria = new SimpleStringCriteria("[" + start + " TO " + end + "]");
更复杂的是注册一个自定义转换器
@Configuration
@EnableSolrRepositories
public class SolrContext {
//..skipped solr server initialization to shorten
@Bean
public SolrTemplate solrTemplate() throws Exception {
SolrTemplate solrTemplate = new SolrTemplate(solrServerFactory());
solrTemplate.setSolrCore("collection1");
solrTemplate.registerQueryParser(SimpleQuery.class, queryParser());
return solrTemplate;
}
@Bean
public QueryParser queryParser() {
DefaultQueryParser queryParser = new DefaultQueryParser();
//register custom converter for Date
queryParser.registerConverter(RoundDayConverter.INSTANCE);
return queryParser;
}
private enum RoundDayConverter implements Converter<Date, String> {
INSTANCE;
private static final DateTimeFormatter FORMATTER = ISODateTimeFormat.dateTime().withZoneUTC();
@Override
public String convert(Date source) {
if (source == null) {
return "";
}
return ClientUtils.escapeQueryChars(FORMATTER.print(source.getTime())) + "/DAY";
}
}
}
由于上述内容适用于每个人Date
,因此创建自定义类型并为此注册转换器可能是个好主意。