简体   繁体   English

如何在实现自定义Spring数据solr存储库时注入SolrOperations bean?

[英]How to inject SolrOperations bean when implement custom Spring data solr repository?

I create a application using Spring boot, and I want to query and add documents to Solr. 我使用Spring启动创建一个应用程序,我想查询并向Solr添加文档。 So I use Spring data solr for this, the maven dependency is: 所以我使用Spring数据solr,maven依赖是:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>

Then I create a configuration class for Solr repository configuration. 然后我为Solr存储库配置创建一个配置类。 Thanks to Spring, everything is simple and works fine. 感谢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);
    }

}

Then, I want to add a custom function for saving documents, since the default converter cannot convert my nested Java object. 然后,我想添加一个自定义函数来保存文档,因为默认转换器无法转换我的嵌套Java对象。 I intend to use the SolrClient bean or SolrTemplate bean for save. 我打算使用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;/
   }
}

But the path of autowired SolrClient does not get solrCoreName in the path from the document object( @Document(solrCoreName = "gettingstarted") ). 但是,自动SolrClient solrCoreName的路径不会从文档对象的路径中获取solrCoreName@Document(solrCoreName = "gettingstarted") )。 It just request to http://localhost:8983/solr , but not http://localhost:8983/solr/gettingstarted with the core name. 它只是请求http://localhost:8983/solr ,而不是http://localhost:8983/solr/gettingstarted的核心名称。

I guess the solrCoreName will be set during initialize repository beans, so my configuration will not contain it. 我想solrCoreName将在初始化存储库bean期间设置,因此我的配置将不包含它。

On the other hand, I found SolrOperation bean of SimpleSolrRepository also becomes null, and all other query like findOne() cannot work properly. 在另一方面,我发现SolrOperationSimpleSolrRepository也变成空的,像所有其他查询findOne()无法正常工作。

It seems I misunderstood some concept of Spring Data that we should not use it with native SolrOperations. 我似乎误解了Spring Data的一些概念,我们不应该将它与原生SolrOperations一起使用。

We can simply use SolrClient from SolrJ together with Spring Data. 我们可以简单地将SolrJ中的SolrClient与Spring Data一起使用。 Here is my simple solution. 这是我的简单解决方案。

Since I have multiple cores in Solr, so I create SolrClient (replacement of SolrServer in older version) for every core in configuraiton. 由于我在Solr中有多个内核,所以我为configuraiton中的每个内核创建了SolrClient (在旧版本中替换了SolrClient )。

@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");
}

Then we can use it in our Solr dao class by autowire the SolrClient bean. 然后我们可以通过自动装配SolrClient bean在我们的Solr dao类中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM