Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Solr multiple cores and repository

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

like image 211
user2279337 Avatar asked Dec 03 '22 23:12

user2279337


2 Answers

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.

like image 118
titogeo Avatar answered Dec 31 '22 13:12

titogeo


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);
like image 28
Christoph Strobl Avatar answered Dec 31 '22 13:12

Christoph Strobl