Spring Data Solr multiple cores and repository

前端 未结 5 998
庸人自扰
庸人自扰 2020-12-06 08:11

I have apache solr with multiple cores e.g. currency, country etc... So using Spring Data Solr I can retrieve information from one core. I have got this XML configuration ri

5条回答
  •  情深已故
    2020-12-06 08:54

    Spring Data now supports multiple cores with their respective repositories.

    The multicoreSupport flag needs to be true in @EnableSolrRepositories annotation and the corresponding document needs to be told what core they belong to. Like:

    @SolrDocument(solrCoreName = "currency")
    public class Currency
    {
        // attributes
    }
    

    the other class should be

    @SolrDocument(solrCoreName = "country")
    public class Country
    {
        // attributes
    }
    

    The respective repositories should know what pojo they are working with.

    public interface CurrencyRepository extends SolrCrudRepository
    {
    }
    

    and

    public interface CountryRepository extends SolrCrudRepository
    {
    }
    

    and configuration should be

    @Configuration
    @EnableSolrRepositories(value = "com.package.name",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());
        }
    }
    

提交回复
热议问题