Spring Data Solr多核和存储库

use*_*337 4 spring spring-data spring-data-solr

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

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

并将存储库定义为

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

}
Run Code Online (Sandbox Code Playgroud)

从我的服务我可以做到这一点

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

我也试过使用@SolrDocument(solrCoreName ="currency"),但这不行.

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

...
...
...
}
Run Code Online (Sandbox Code Playgroud)

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

希望有人能提供帮助.谢谢GM

tit*_*geo 9

以为我会分享,我们最近花了很多时间配置多个核心.我们在java中做过,而不是xml.

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

@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();
}
Run Code Online (Sandbox Code Playgroud)

并在服务类中使用如下所示的每个模板.

@Resource
private SolrTemplate solrCore1Template;
Run Code Online (Sandbox Code Playgroud)

可以使用以下代码使用HTTP关联嵌入式服务器.

HttpSolrServer httpSolrServer = new HttpSolrServer(getSolrURL());
return new SolrTemplate(httpSolrServer, "core1");
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.我知道对于提出的问题,这是一个非常晚的回复.