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

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

问题描述

我有多个内核的 apache solr,例如货币、国家等...所以使用 Spring Data Solr 我可以从一个核心检索信息.我现在有这个 XML 配置查询 'currency' 核心.如果我想查询 'country' 核心,我该如何设置?

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天全站免登陆