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.