Spring Data Solr multiple cores and repository

前端 未结 5 999
庸人自扰
庸人自扰 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:41

    multicore support via namespace config is unfortunately an open issue. You'll need to have a separate SolrTemplate for each core and create repositories manually.

    @Autowired 
    @Qualifier("solrCurrencyTemplate")
    private SolrTemplate solrCurrencyTemplate;
    
    @Autowired
    @Qualifier("solrCountryTemplate")
    private SolrTemplate solrCountryTemplate;
    
    //...
    
    CurrencyRepository currencyRepo = new SolrRepositoryFactory(this.solrCurrencyTemplate)
      .getRepository(CurrencyRepository.class);
    
    CountryRepository countryRepo = new SolrRepositoryFactory(this.solrCountryTemplate)
      .getRepository(CountryRepository.class);
    
    0 讨论(0)
  • 2020-12-06 08:45

    With Spring Data Solr 1.1.0.RC1 multiple cores works as described by Christoph Strobl with @EnableSolrRepositories. It works also with an XML configuration by set multicore-support="true".

    <solr:repositories base-package="your.solr.repo.package" repository-impl-postfix="Impl" multicore-support="true"/>
    
    <solr:solr-server id="solrServer" url="${solr.server.base.connection.url}" />
    
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg index="0" ref="solrServer" />
    </bean>
    
    0 讨论(0)
  • 2020-12-06 08:46

    Thought I would share, We spend lot of time recently configuring multiple cores. We did in java, not xml.

    As part of spring @configuration add following.

    @Bean(name="solrCore1Template")
    public SolrTemplate solrCore1Template() throws Exception {
        EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(getCoreContainer(), "core1");
        return new SolrTemplate(embeddedSolrServer);
    }
    
    @Bean(name="solrCore2Template")
    public SolrTemplate solrCore2Template() throws Exception {   
        EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(getCoreContainer(), "core2");
        return new SolrTemplate(embeddedSolrServer);
    }
    
    @Bean
    @Scope
    public CoreContainer getCoreContainer() throws FileNotFoundException{
        String dir = <path_to_solr_home>;
        System.setProperty("solr.solr.home", dir);
        CoreContainer.Initializer initializer = new CoreContainer.Initializer();
        return initializer.initialize();
    }
    

    And to use each template use like below in service classes.

    @Resource
    private SolrTemplate solrCore1Template;
    

    Embedded server can be relaced with HTTP using below code.

    HttpSolrServer httpSolrServer = new HttpSolrServer(getSolrURL());
    return new SolrTemplate(httpSolrServer, "core1");
    

    Hope this helps. I know it's a very late reply for the question asked.

    0 讨论(0)
  • 2020-12-06 08:46
    <solr:solr-server id="solrServer" timeout="1000" maxConnections="1000" url="${solr.server.1},${solr.server.2}"/>
    
    <bean id="solrServerFactory" class="org.springframework.data.solr.server.support.MulticoreSolrServerFactory">
        <constructor-arg ref="solrServer" />
        <constructor-arg name="cores">
            <list>
                <value>${solr.index.customer}</value>
                <value>${solr.index.task}</value>
            </list>
        </constructor-arg>
    </bean>
    
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServerFactory" />
    </bean>
    
    <solr:repositories base-package="com.deve.pig.solr" multicore-support="true" solr-template-ref="solrTemplate" />
    
    0 讨论(0)
  • 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<Currency,String>
    {
    }
    

    and

    public interface CountryRepository extends SolrCrudRepository<Country,String>
    {
    }
    

    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());
        }
    }
    
    0 讨论(0)
提交回复
热议问题