Apache Solr是一個搜索引擎。Spring Boot為solr客戶端庫及Spring Data Solr提供的基于solr客戶端庫的抽象提供了基本的配置。Spring Boot提供了一個用于聚集依賴的spring-boot-starter-data-solr 'Starter POM'。

創(chuàng)新互聯(lián)建站主要從事成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)左權(quán),10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
引入spring-boot-starter-data-solr依賴,在pom.xml配置文件中增加如下內(nèi)容(基于之前章節(jié)“Spring Boot 構(gòu)建框架”中的pom.xml文件):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId> </dependency>
可以像其他Spring beans一樣注入一個自動配置的SolrServer實例。默認(rèn)情況下,該實例將嘗試使用localhost:8983/solr連接一個服務(wù)器。
@Component
public class MyBean {
private SolrServer solr;
@Autowired
public MyBean(SolrServer solr) {
this.solr = solr;
}
// ...
}
如果添加一個自己的SolrServer類型的@Bean,它將會替換默認(rèn)的。
應(yīng)用集成Solr搜索客戶端案例
Spring Boot的配置是集中性的(可以拆分成不同的配置文件),因此在application.properties文件中添加以下配置:
# SOLR (SolrProperties) spring.data.solr.host=http://localhost:8983/solr #spring.data.solr.zkHost= spring.data.solr.repositories.enabled=true
使用Spring-data-solr類似spring-data-jpa,配置@bean接受zk服務(wù)器相關(guān)屬性(自定義的配置方式,可以直接使用默認(rèn)方式)
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="spring.solr")
public class SolrConfig {
private String host;
private String zkHost;
private String defaultCollection;
public String getDefaultCollection() {
return defaultCollection;
}
public void setDefaultCollection(String defaultCollection) {
this.defaultCollection = defaultCollection;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getZkHost() {
return zkHost;
}
public void setZkHost(String zkHost) {
this.zkHost = zkHost;
}
配置SolrServer服務(wù),具體代碼如下:
@Configuration
@EnableConfigurationProperties(SolrConfig.class)
public class SolrClientConfig {
@Autowired
private SolrConfig solrConfig;
private CloudSolrServer solrServer;
@PreDestroy
public void close() {
if (this.solrServer != null) {
try {
this.solrServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Bean
public CloudSolrServer SolrServer(){
if (StringUtils.hasText(this.solrConfig.getZkHost())) {
solrServer = new CloudSolrServer(this.solrConfig.getZkHost());
solrServer.setDefaultCollection(this.solrConfig.getDefaultCollection());
}
return this.solrServer;
}
}
測試solr查詢,具體代碼如下:
@RestController
public class HelloController {
@Autowired
private CloudSolrServer solrserver;
public String hello(){
return"say hello";
}
@RequestMapping("test")
public void test(){
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("q","demo:素文宅博客");
params.add("ws","json");
params.add("start","0");
params.add("rows","10");
QueryResponse response = null;
try{
response=solrserver.query(params);
SolrDocumentList results = response.getResults();
for (SolrDocument document : results) {
System.out.println( document.getFieldValue("demo"));
System.out.println(document.getFieldValue("id"));
}
}catch(Exception e){
e.getStackTrace();
}
System.out.println(response.toString());
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享名稱:SpringBoot集成Sorl搜索客戶端的實現(xiàn)代碼
轉(zhuǎn)載來源:http://chinadenli.net/article48/iidihp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、電子商務(wù)、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、小程序開發(fā)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)