我使用Spring启动创建一个应用程序,我想查询并向Solr添加文档。所以我使用Spring数据solr,maven依赖是:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
然后我为Solr存储库配置创建配置类。感谢Spring,一切都很简单,工作正常。
@Configuration
@EnableSolrRepositories(basePackages = { "xxx.xx.xx.resource.repository.solr" },multicoreSupport = true)
public class SolrConfiguration {
@Bean
public SolrClient solrClient(@Value("${solr.host}") String solrHost) {
return new HttpSolrClient(solrHost);
}
}
然后,我想添加一个自定义函数来保存文档,因为默认转换器无法转换我的嵌套Java对象。我打算使用SolrClient
bean或SolrTemplate bean进行保存。
public class HouseSolrRepositoryImpl extends SimpleSolrRepository<House, String> implements HouseSolrRepository{
@Autowired
private SolrClient solrClient;
@Override
public House save(House house) throw Exception{
// Do converting
solrClient.save(doc);
solrClient.commit();
return house;/
}
}
但是,自动装配SolrClient
的路径在文档对象(solrCoreName
)的路径中没有获得@Document(solrCoreName = "gettingstarted")
。它只是向http://localhost:8983/solr
请求,而不是http://localhost:8983/solr/gettingstarted
请求核心名称。
我想在初始化存储库bean期间会设置solrCoreName
,因此我的配置不会包含它。
另一方面,我发现SolrOperation
的{{1}} bean也变为空,而SimpleSolrRepository
之类的所有其他查询都无法正常运行。
答案 0 :(得分:0)
我似乎误解了Spring Data的一些概念,我们不应该将它与原生SolrOperations一起使用。
我们可以简单地将SolrJ中的SolrClient与Spring Data一起使用。这是我的简单解决方案。
由于我在Solr中有多个内核,所以我为configuraiton中的每个内核创建SolrClient
(替换旧版本的SolrServer)。
@Bean
public SolrClient gettingStartedSolrClient(@Value("${solr.host}") String solrHost){
return new ConcurrentUpdateSolrClient(solrHost+"/gettingstarted");
}
@Bean
public SolrClient anotherSolrClient(@Value("${solr.host}") String solrHost){
return new ConcurrentUpdateSolrClient(solrHost+"/anotherCore");
}
然后我们可以通过自动装配SolrClient
bean在我们的Solr dao类中使用它。