我在记录我的代码时,按照一些Spring数据的Solr apache教程写的,我发现我不知道SolrTemplate和SolrClient之间的区别。solrTemplate
和a SolrClient
?
我在记录下面的代码。
@Configuration
@EnableSolrRepositories(basePackages = {"com.anouar.solr.nomenclaturespringdatasolr.repository",
"com.anouar.solr.nomenclaturespringdatasolr.dataImportHandler"},
namedQueriesLocation = "classpath:solr-named-queries.properties")
public class SolrConfig {
@Value("${spring.data.solr.host}")
String solrURL;
/**
* returns the bean that establishes the connection with Solr through port 8983
*
* @return SolrClient
*
* **/
@Bean
public SolrClient solrClient() {
return new HttpSolrClient.Builder(solrURL).build();
}
/**
*
* @param client the bean that is connected to Solr through port 8983
*
* **/
@Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
return new SolrTemplate(client);
}
}
下面是apache文档中对solrTemplate和SolrClient的描述 SolrClient
Abstraction through which all communication with a Solr server may be routed
这意味着你所有的solr调用都将通过solrClient进行,所以我们需要将solr服务器地址、端口(也有一些其他的)配置为 solrClient
.
如 solrTemplate
是用于solr操作,如查询、计数等。solrTemplate
将使用 solrClient
这就是为什么在配置 solrTemplate
, solrClient
是通过。