Spring Boot集成Sorl搜索客戶端的實(shí)現(xiàn)代碼
Apache Solr是一個(gè)搜索引擎。Spring Boot為solr客戶端庫(kù)及Spring Data Solr提供的基于solr客戶端庫(kù)的抽象提供了基本的配置。Spring Boot提供了一個(gè)用于聚集依賴的spring-boot-starter-data-solr 'Starter POM'。
引入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一樣注入一個(gè)自動(dòng)配置的SolrServer實(shí)例。默認(rèn)情況下,該實(shí)例將嘗試使用localhost:8983/solr連接一個(gè)服務(wù)器。
@Component
public class MyBean {
private SolrServer solr;
@Autowired
public MyBean(SolrServer solr) {
this.solr = solr;
}
// ...
}
如果添加一個(gè)自己的SolrServer類(lèi)型的@Bean,它將會(huì)替換默認(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類(lèi)似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;
}
}
測(cè)試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());
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA創(chuàng)建maven項(xiàng)目時(shí)在tomcat運(yùn)行瀏覽器404的問(wèn)題
這篇文章主要介紹了IDEA創(chuàng)建maven項(xiàng)目時(shí)在tomcat運(yùn)行瀏覽器404的問(wèn)題及解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
maven實(shí)現(xiàn)docker自動(dòng)化部署插件的使用
本文主要介紹了maven實(shí)現(xiàn)docker自動(dòng)化部署插件的使用,分享給大家,感興趣的小伙伴們可以參考一下2021-06-06
在SpringBoot項(xiàng)目中使用Spring Cloud Sentinel實(shí)現(xiàn)流量控制
隨著微服務(wù)架構(gòu)的流行,服務(wù)之間的調(diào)用變得越來(lái)越頻繁和復(fù)雜,流量控制是保障系統(tǒng)穩(wěn)定性的重要手段之一,它可以幫助我們避免因過(guò)載而導(dǎo)致的服務(wù)不可用,本文將介紹如何在Spring Boot項(xiàng)目中使用Spring Cloud Sentinel來(lái)實(shí)現(xiàn)流量控制,需要的朋友可以參考下2024-08-08
Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式
這篇文章主要介紹了Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java中shiro框架和security框架的區(qū)別
這篇文章主要介紹了Java中shiro框架和security框架的區(qū)別,shiro和security作為兩款流行的功能強(qiáng)大的且易于使用的java安全認(rèn)證框架,在近些年中的項(xiàng)目開(kāi)發(fā)過(guò)程中使用廣泛,今天我們就來(lái)一起了解一下兩者的區(qū)別2023-08-08
Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Spring代理對(duì)象導(dǎo)致的獲取不到原生對(duì)象注解的解決
本文主要介紹了Spring代理對(duì)象導(dǎo)致的獲取不到原生對(duì)象注解的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java 內(nèi)省(Introspector)深入理解
這篇文章主要介紹了Java 內(nèi)省(Introspector)深入理解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Spring與Shiro整合及加載權(quán)限表達(dá)式問(wèn)題
這篇文章主要介紹了Spring與Shiro整合及加載權(quán)限表達(dá)式問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

