![](/img/trans.png)
[英]How to configure Spring Data Solr with Repositories for multiple cores
[英]Configure Spring Data Solr with multiple cores
我在將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());
}
}
我們的Pojos配置為:
@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 doc: 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
我認為您需要在pojos中添加@SolrDocument批注以使用多核支持。 參見下面的討論。 不過要取決於您的設置方式。
看起來我很累,以下配置似乎正常運行-隨時進行改進
@Configuration
@EnableSolrRepositories(basePackages={"com.example.demo.repository"})
public class SolrContext {
static final String SOLR_HOST = "solr.host";
@Autowired
private Environment environment;
@Bean
public SolrClient solrClient() {
return new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)).build();
}
@Bean
public SolrTemplate solrTemplate() {
return new SolrTemplate(solrClient());
}
@Bean
public SolrTemplate core1Template() {
return new SolrTemplate(new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)+"/core1").build());
}
@Bean
public SolrTemplate core2Template() {
return new SolrTemplate(new HttpSolrClient.Builder(environment.getProperty(SOLR_HOST)+"/core2").build());
}
}
帶有示例: https : //github.com/Farael49/spring-solr-config/blob/master/demo/src/main/java/com/example/demo/web/CoreController.java
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.