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類型的@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類似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)文章
詳解java接口(interface)在不同JDK版本中的變化
這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Java字符轉(zhuǎn)碼之UTF-8互轉(zhuǎn)GBK具體實(shí)現(xiàn)
在Java程序中字符串默認(rèn)的編碼方式是UTF-16編碼,因此需要將GBK編碼轉(zhuǎn)換為UTF-8編碼,主要是為了避免出現(xiàn)亂碼的情況,這篇文章主要給大家介紹了關(guān)于Java字符轉(zhuǎn)碼之UTF-8互轉(zhuǎn)GBK具體實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-11-11
Eclipse操作SVN時(shí)中斷鎖定,文件的解鎖方法
這篇文章主要介紹了Eclipse操作SVN時(shí)中斷鎖定,文件的解鎖方法,需要的朋友可以參考下2014-08-08
Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫(kù)顯示亂碼的解決方案
在用springboot做項(xiàng)目時(shí),由于重新安裝了本地Mysql數(shù)據(jù)庫(kù)(5.7版本)在前臺(tái)向數(shù)據(jù)庫(kù)插入和更新數(shù)據(jù)可的時(shí)候,涉及中文的時(shí)候在數(shù)據(jù)庫(kù)一直顯示異常,所以本文給大家介紹了相關(guān)的解決方案,需要的朋友可以參考下2024-02-02
Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL
這篇文章主要為大家介紹了Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
詳解高性能緩存Caffeine原理及實(shí)戰(zhàn)
Caffeine是基于Java 8開發(fā)的,提供了近乎最佳命中率的高性能本地緩存組件,Spring5開始不再支持Guava Cache,改為使用Caffeine。Caffeine提供的內(nèi)存緩存使用參考Google guava的API2021-06-06
Java Springboot websocket使用案例詳解
這篇文章主要介紹了Java Springboot websocket使用案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析
這篇文章主要介紹了你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

