Using spring-data-solr with configurable Solr core

Using spring-data-solr with configurable Solr core

Developing Java applications accessing data from Solr is easier with spring-data-solr. You can just use Repository interfaces, auto generated findBy... methods or @Query annotations to define your data sources. Unfortunately using this method it is not possible to configure the name of the Solr core (or collection). The name of the collection is automatically defined based on the name of your Document class or by the @SolrDocument annotation.

@SolrDocument(collection = "collection1")
public class YourSolrDataClass {
    public String id;
    public String text;
    // ...

While your Repository can be just an interface. See Spring-Data-Solr's showcase implementation on Github, which is downloadable from the "Getting Started" page as well.

Configuring the collection name is not possible. However, there is a workaround with a bit of code. Unfortunately this means that you have to implement the methods which are actually querying Solr, you can't use the @Query annotations and auto generation magic (which is, by the way, explained very well in this article by g00glen00b) in your Repository interface any more.

First step, reimplement An implementation of this interface is handed around internally in SimpleSolrRepository, which is used commonly as implementation of your repository.

Then, implement your Repository, extending from SimpleSolrRepository.

That's all. After that, you can setup your Spring context.

If there is a simpler solution, I am looking forward to it, drop me a line. I'd wish to see this feature (or more simple) included in Spring Data Solr.