我使用spring-data-solr索引我的实体。据我了解,Solrj可以处理Date属性,但无法处理LocalDate。
我尝试在this之后创建并注册自定义转换器,以便在LocalDate和String之间进行转换,但它不起作用。此外,当我尝试使用org.joda.time.LocalDate或org.springframework.data.geo.Point时,我发现即使org.springframework.data.solr.core.convert.SolrCustomConversions中指定的转换器也无效。
从错误响应中,应用程序似乎尝试访问名为 localdate 的核心。
依赖关系:
org.springframework.boot:1.5.3.RELEASE
org.springframework.data:spring-data-solr:2.1.3.RELEASE
org.apache.solr:solr-solrj:6.0.1
代码:
@Getter
@Setter
@SolrDocument(solrCoreName = "testentity")
public class SoTestEntity {
@Field
@Indexed
private String id;
@Field
@Indexed(type = "daterange")
private LocalDate localDateAttribute;
}
public final class TemporalConverters {
@ReadingConverter
public enum StringToLocalDateConverter implements Converter<String, LocalDate> {
INSTANCE;
@Override
public LocalDate convert(String source) {
return StringToLocalDateTimeConverter.INSTANCE.convert(source).toLocalDate();
}
}
@WritingConverter
public enum LocalDateToStringConverter implements Converter<LocalDate, String> {
INSTANCE;
@Override
public String convert(LocalDate source) {
if (source == null) {
return null;
}
return source.format(DateTimeFormatter.ISO_DATE);
}
}
}
public class SoTest {
private SolrTemplate template;
@Before
public void setUp() throws Exception {
HttpSolrClient client = new HttpSolrClient("http://localhost:8983/solr");
template = new SolrTemplate(client);
template.setSchemaCreationFeatures(Collections.singletonList(SolrPersistentEntitySchemaCreator.Feature.CREATE_MISSING_FIELDS));
MappingContext mappingContext = new SimpleSolrMappingContext();
MappingSolrConverter converter = new MappingSolrConverter(mappingContext);
List<Converter> converters = new ArrayList<>();
converters.add(TemporalConverters.LocalDateTimeToStringConverter.INSTANCE);
converters.add(TemporalConverters.StringToLocalDateConverter.INSTANCE);
converter.setCustomConversions(new CustomConversions(converters));
template.setSolrConverter(converter);
template.afterPropertiesSet();
}
@Test
public void saveSingleTest() {
template.saveBean(new SolrTestEntity());
}
}
错误:
org.springframework.data.solr.UncategorizedSolrException: Error from server at http://localhost:8983/solr: Expected mime type application/octet-stream but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/localdate/schema/version. Reason:
<pre> Not Found</pre></p>
</body>
</html>
; nested exception is org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr: Expected mime type application/octet-stream but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/localdate/schema/version. Reason:
<pre> Not Found</pre></p>
</body>
</html>
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:224)
at org.springframework.data.solr.core.schema.DefaultSchemaOperations.getSchemaVersion(DefaultSchemaOperations.java:77)
at org.springframework.data.solr.core.schema.SolrSchemaWriter.retrieveSchemaVersion(SolrSchemaWriter.java:87)
at org.springframework.data.solr.core.schema.SolrSchemaWriter.isSchemaPresent(SolrSchemaWriter.java:79)
at org.springframework.data.solr.core.schema.SolrSchemaWriter.writeSchema(SolrSchemaWriter.java:41)
at org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator.process(SolrPersistentEntitySchemaCreator.java:63)
at org.springframework.data.solr.core.schema.SolrPersistentEntitySchemaCreator.onApplicationEvent(SolrPersistentEntitySchemaCreator.java:83)
at org.springframework.data.solr.core.mapping.SolrMappingEventPublisher.publishEvent(SolrMappingEventPublisher.java:54)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:345)
at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.createAndRegisterProperty(AbstractMappingContext.java:508)
at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.doWith(AbstractMappingContext.java:465)
at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:693)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:329)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:185)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:145)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:70)
at org.springframework.data.solr.core.SolrTemplate.getSolrCoreOrBeanCollection(SolrTemplate.java:1300)
at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:318)
at org.springframework.data.solr.core.SolrTemplate.saveBean(SolrTemplate.java:300)
at mypkg.SoTest.saveSingleTest(SoTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://localhost:8983/solr: Expected mime type application/octet-stream but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/localdate/schema/version. Reason:
<pre> Not Found</pre></p>
</body>
</html>
at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:545)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:241)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:230)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)
at org.springframework.data.solr.core.schema.DefaultSchemaOperations$2.doInSolr(DefaultSchemaOperations.java:81)
at org.springframework.data.solr.core.schema.DefaultSchemaOperations$2.doInSolr(DefaultSchemaOperations.java:77)
at org.springframework.data.solr.core.SolrTemplate.execute(SolrTemplate.java:220)
... 42 more