Spring Data Solr的多个核心和存储库 [英] Spring Data Solr multiple cores and repository

查看:175
本文介绍了Spring Data Solr的多个核心和存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个核心的apache solr,例如货币,国家等...因此,使用Spring Data Solr,我可以从一个核心中检索信息.我现在已经针对'currency'核心查询了此XML配置.如果要查询国家/地区" 核心,该如何设置?

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>

,并将存储库定义为

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

我也尝试过使用 @SolrDocument(solrCoreName ="currency"),但这无效.

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;

...
...
...
}

我需要尽快获得帮助...否则,我将不得不回到RestTemplate解决方案:-(

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

希望有人可以提供帮助. 谢谢 总经理

Hope someone can help. Thanks GM

推荐答案

我想分享一下,我们最近花了很多时间配置多个内核.我们是用Java而不是xml完成​​的.

Thought I would share, We spend lot of time recently configuring multiple cores. We did in java, not xml.

作为spring @configuration的一部分,添加以下内容.

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;

可以使用以下代码将嵌入式服务器替换为HTTP.

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.

这篇关于Spring Data Solr的多个核心和存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆