Spring Data Solr multiple cores and repository

前端 未结 5 1008
庸人自扰
庸人自扰 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: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 = ;
        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.

提交回复
热议问题