欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot集成Sorl搜索客戶端的實(shí)現(xiàn)代碼

 更新時間:2017年11月02日 10:15:52   作者:素文宅  
本篇文章主要介紹了Spring Boot集成Sorl搜索客戶端的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Apache Solr是一個搜索引擎。Spring Boot為solr客戶端庫及Spring Data Solr提供的基于solr客戶端庫的抽象提供了基本的配置。Spring Boot提供了一個用于聚集依賴的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一樣注入一個自動配置的SolrServer實(shí)例。默認(rèn)情況下,該實(shí)例將嘗試使用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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解java接口(interface)在不同JDK版本中的變化

    詳解java接口(interface)在不同JDK版本中的變化

    這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Java字符轉(zhuǎn)碼之UTF-8互轉(zhuǎn)GBK具體實(shí)現(xiàn)

    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時中斷鎖定,文件的解鎖方法

    Eclipse操作SVN時中斷鎖定,文件的解鎖方法

    這篇文章主要介紹了Eclipse操作SVN時中斷鎖定,文件的解鎖方法,需要的朋友可以參考下
    2014-08-08
  • Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫顯示亂碼的解決方案

    Java向MySQL添加中文數(shù)據(jù)數(shù)據(jù)庫顯示亂碼的解決方案

    在用springboot做項(xiàng)目時,由于重新安裝了本地Mysql數(shù)據(jù)庫(5.7版本)在前臺向數(shù)據(jù)庫插入和更新數(shù)據(jù)可的時候,涉及中文的時候在數(shù)據(jù)庫一直顯示異常,所以本文給大家介紹了相關(guān)的解決方案,需要的朋友可以參考下
    2024-02-02
  • Map集合之HashMap的使用及說明

    Map集合之HashMap的使用及說明

    這篇文章主要介紹了Map集合之HashMap的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL

    Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL

    這篇文章主要為大家介紹了Java 下數(shù)據(jù)業(yè)務(wù)邏輯開發(fā)技術(shù) JOOQ 和 SPL詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • gRPC中interceptor攔截器的使用教程

    gRPC中interceptor攔截器的使用教程

    gRPC中的interceptor攔截器分為客戶端攔截器和服務(wù)端攔截器,分別是在客戶端和服務(wù)端的請求被發(fā)送出去之前進(jìn)行處理的邏輯,下面就跟隨小編一起學(xué)習(xí)一下interceptor攔截器的具體使用吧
    2023-08-08
  • 詳解高性能緩存Caffeine原理及實(shí)戰(zhàn)

    詳解高性能緩存Caffeine原理及實(shí)戰(zhàn)

    Caffeine是基于Java 8開發(fā)的,提供了近乎最佳命中率的高性能本地緩存組件,Spring5開始不再支持Guava Cache,改為使用Caffeine。Caffeine提供的內(nèi)存緩存使用參考Google guava的API
    2021-06-06
  • Java Springboot websocket使用案例詳解

    Java Springboot websocket使用案例詳解

    這篇文章主要介紹了Java Springboot websocket使用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析

    你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析

    這篇文章主要介紹了你所不知道的Spring的@Autowired實(shí)現(xiàn)細(xì)節(jié)分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08

最新評論