【发布时间】:2017-07-21 16:55:30
【问题描述】:
我在将我的 solr 配置从 1.5.4 升级到 3.0.0.M4(以及 Spring Boot 更新到 2.0.0.M2)时遇到问题。
之前的配置文件(spring-data-solr 1.5.4.RELEASE):
@Configuration
@EnableSolrRepositories(value = "com.bar.foo.repository.solr", multicoreSupport = true)
public class SolrConfiguration implements EnvironmentAware{
private RelaxedPropertyResolver propertyResolver;
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.data.solr.");
}
@Bean
public SolrServer solrServer() {
String solrHost = propertyResolver.getProperty("host");
return new HttpSolrServer(solrHost);
}
@Bean(name = "core1SolrTemplate")
public SolrOperations core1SolrTemplate() {
HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
return new SolrTemplate(httpSolrServer, "Core1");
}
@Bean(name = "core2SolrTemplate")
public SolrOperations core2SolrTemplate() {
HttpSolrServer httpSolrServer = new HttpSolrServer(propertyResolver.getProperty("host"));
return new SolrTemplate(httpSolrServer, "Core2");
}
...
}
然后我们在代码中使用这些solrtemplate作为
@Resource SolrTemplate core1SolrTemplate;
@Resource SolrTemplate core2SolrTemplate;
尝试更新时,我发现 HttpSolrServer 不再可用,我们应该使用 SolrClient,但是随着 multicoreSupport 的移除和声明模板核心的能力,我不知道如何为每个核心拥有一个模板(或一个能够检测到要查询的核心?)
这是我当前的非工作配置:
@Configuration
@EnableSolrRepositories(value = "com.bar.foo.repository.solr")
public class SolrConfiguration {
@Inject
private Environment environment;
@Bean
public SolrClient solrClient() {
return new HttpSolrClient.Builder(environment.getProperty("spring.data.solr.host")).build();
}
@Bean
public SolrTemplate solrTemplate() {
return new SolrTemplate(solrClient());
}
}
我们的 Pojo 配置为:
@SolrDocument(solrCoreName = "Core1")
public class Foo{
@Id
private String id;
...
}
以及引发错误的调用
core1SolrTemplate.queryForPage(simpleQuery, Foo.class)
“原因:org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException:来自http://192.168.99.100:8983/solr 的服务器错误:预期的 MIME 类型为 application/octet-stream 但得到了 text/html。”
看来 solrtemplate 只是调用了 baseurl 而没有选择任何核心。
我还尝试像以前一样创建多个 SolrTemplate 并将参数传递给 solrClient() 以构造每个核心的特定 url,但未能成功(我在第二个模板显然使用第一个 solrtemplate bean 时出现错误,指向错误的核心)。
我应该如何配置 Solr 以便能够查询多个核心?
当前 (2.1.4) solr 文档: http://docs.spring.io/spring-data/data-solr/docs/current/reference/html/#solr.multicore
3.0.0.M4 文档中没有描述多核: http://docs.spring.io/spring-data/data-solr/docs/3.0.0.M4/reference/html/#solr.multicore
【问题讨论】:
标签: spring spring-boot spring-data-solr