1

I create a application using Spring boot, and I want to query and add documents to Solr. So I use Spring data solr for this, the maven dependency is:

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

Then I create a configuration class for Solr repository configuration. Thanks to Spring, everything is simple and works fine.

@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. I intend to use the SolrClient bean or SolrTemplate bean for save.

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") ). It just request to http://localhost:8983/solr, but not http://localhost:8983/solr/gettingstarted with the core name.

I guess the solrCoreName will be set during initialize repository beans, so my configuration will not contain it.

On the other hand, I found SolrOperation bean of SimpleSolrRepository also becomes null, and all other query like findOne() cannot work properly.

BurnetZhong
  • 438
  • 1
  • 5
  • 14

1 Answers1

0

It seems I misunderstood some concept of Spring Data that we should not use it with native SolrOperations.

We can simply use SolrClient from SolrJ together with 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.

@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.

BurnetZhong
  • 438
  • 1
  • 5
  • 14
  • Does it work for you? In my case whenever I call the method it keep creating new instances. – Maralc Feb 14 '18 at 03:01