Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding multipli core in spring with custom solr repository

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 664
    Comment on it

    Repository take solrtemplate to execute solr operations. Since we have multiple cores so we need to create multiple template and multiple repositories for each core. Following example explains it with 2 solr core

    Repository and custom implementation for core1:

    public interface ICore1CustomDao {
    
    	  List<Core1> findCore1Data();
    }
    
    public interface Core1Repository extends SolrCrudRepository<Core1, String> {
    
    }
    
    public class Core1DaoImpl implements ICore1CustomDao {
    
    	@Autowired
        private SolrTemplate core1Template;
    
        public EventRepositoryImpl(SolrTemplate core1Template) {
            this.core1Template = core1Template;
        }
    
        @Override
        public List<Core1> findCore1Data() {
            return core1Template.queryForPage(new SimpleQuery("*:*"), Core1.class);
        }
    
    }

    Do same for Core2 repositories.

    Configure solr as:

    @Configuration
    public class Application {
    	
    	@Bean
    	public SolrServerFactory solrServerFactory() {
    		return new MulticoreSolrServerFactory(new HttpSolrServer("http://localhost:8983/solr"));
    	}
    	
    	@Bean
    	public SolrTemplate core1Template() throws Exception {
    		SolrTemplate solrTemplate = new SolrTemplate(solrServerFactory());
    	    solrTemplate.setSolrCore("core1");
    	    return solrTemplate;
    	}
    	
    	@Bean
    	public Core1Repository core1Repository() throws Exception {
    	    return new SolrRepositoryFactory(core1Template())
    	      .getRepository(Core1Repository.class, new Core1DaoImpl(core1Template()));
    	}
    	
    	@Bean
    	public SolrTemplate core2Template() throws Exception {
    		SolrTemplate solrTemplate = new SolrTemplate(solrServerFactory());
    	    solrTemplate.setSolrCore("core2");
    	    return solrTemplate;
    	}
    	
    	@Bean
    	public Core2Repository core2Repository() throws Exception {
    	    return new SolrRepositoryFactory(core2Template())
    	      .getRepository(Core2Repository.class, new Core2DaoImpl(core2Template()));
    	}
    	
    }

    Its done. 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: