How to configure Spring Data Solr with Repositories for multiple cores

二次信任 提交于 2019-12-02 12:13:20

The only dependency that need to be used for spring-data-solr is

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>

It downloads solrj dependency and this must not be overridden with later versions of solrj. Also It is always preferable to use HttpSolrServer over EmbeddedSolrServer which is what we will be working with.

The Configuration class should look like this:

@Configuration
@EnableSolrRepositories(value = "com.package.",multicoreSupport = true)
public class SolrConfig
{
    @Bean
    public SolrServer solrServer() throws Exception
    {
        HttpSolrServerFactoryBean f = new HttpSolrServerFactoryBean();
        f.setUrl("http://localhost:8983/solr");
        f.afterPropertiesSet();
        return f.getSolrServer();
    }

    @Bean
    public SolrTemplate solrTemplate(SolrServer solrServer) throws Exception
    {
        return new SolrTemplate(solrServer());
    }
}

The document entity should contain information about which core they belong to

@SolrDocument(solrCoreName = "core1")
public class Document1
{
    @Id
    @Field
    private String id;

    /**other attributes**/
}

The other document should be

@SolrDocument(solrCoreName = "core2")
public class Document2
{
    @Id
    @Field
    private String id;

    /**other attributes**/
}

Now the best part is you are already done. Simply setting up repository in the plain old way does the trick

public interface SolrCore1Repository extends SolrCrudRepository<Document1,String>
{
    // optional code
}

the other repo is like

public interface SolrCore2Repository extends SolrCrudRepository<Document2,String>
{
    // optional code
}

Once solr is running on the url specified and it has fields according to the pojo, you are done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!