Spring Data Solr multiple cores and repository

user2279337 picture user2279337 · May 31, 2013 · Viewed 10.4k times · Source

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 right now queries against 'currency' core. If I wanted to query against 'country' core how can I set this up?

<!-- Enable Solr repositories and configure repository base package -->
<solr:repositories base-package="com.acme.repository" solr-template-ref="solrCurrencyTemplate"/>

<solr:solr-server id="solrCurrencyServer" url="http://localhost:8983/solr/currency"/>

<bean id="solrCurrencyTemplate" class="org.springframework.data.solr.core.SolrTemplate">
    <constructor-arg ref="solrCurrencyServer" />
</bean>

and have the repository defined as

@Repository
public interface CurrencyRepository extends SolrCrudRepository<Currency, String> {

}

and from my service I can do this

@Override
public List<Currency> getCurrencies() {
    Page<Currency> currencies = (Page<Currency>) currencyRepository.findAll();
    return currencies.getContent();
}

I have also tried using @SolrDocument(solrCoreName = "currency") but this din't work.

@SolrDocument(solrCoreName = "currency")
public class Currency {
    public static final String FIELD_CURRENCY_NAME = "currency_name";
    public static final String FIELD_CURRENCY_CODE = "currency_code";
    public static final String FIELD_DECIMALS = "decimals";

    @Id
    @Field(value = FIELD_CURRENCY_CODE)
    private String currencyCode;

    //currency_name,decimals
    @Field(value = FIELD_CURRENCY_NAME)
    private String currencyName;

    @Field(value = FIELD_DECIMALS)
    private String decimals;

...
...
...
}

I need help on this asap... otherwise I will have to go back to the RestTemplate Solution :-(

Hope someone can help. Thanks GM

Answer

titogeo picture titogeo · Feb 18, 2014

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.