user2279337
user2279337

Reputation: 691

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

Upvotes: 4

Views: 11074

Answers (5)

Nikhil Sahu
Nikhil Sahu

Reputation: 2621

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());
    }
}

Upvotes: 2

user3989324
user3989324

Reputation: 11

<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" />

Upvotes: 1

titogeo
titogeo

Reputation: 2184

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.

Upvotes: 9

nydi
nydi

Reputation: 11

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>

Upvotes: 1

Christoph Strobl
Christoph Strobl

Reputation: 6736

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);

Upvotes: 4

Related Questions