我的apache solr具有多个核心,例如货币,国家等。因此,使用Spring Data Solr可以从一个核心中检索信息。我现在已经获得了针对“ currency”核心的XML配置。如果我想查询“国家”核心,该如何设置?
<!-- 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> { }
从我的服务中我可以做到这一点
@Override public List<Currency> getCurrencies() { Page<Currency> currencies = (Page<Currency>) currencyRepository.findAll(); return currencies.getContent(); }
我也尝试过使用@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; ... ... ... }
我需要尽快的帮助…否则,我将不得不回到RestTemplate解决方案:-(
希望有人能帮忙。谢谢通用
以为我会分享:最近我们花大量时间配置多个内核。我们是用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(); }
并且要使用每个模板,请在服务类中使用以下内容。
@Resource private SolrTemplate solrCore1Template;
可以使用以下代码将嵌入式服务器替换为HTTP。
HttpSolrServer httpSolrServer = new HttpSolrServer(getSolrURL()); return new SolrTemplate(httpSolrServer, "core1");
希望这可以帮助。我知道这是一个很晚才回答的问题。