Apache Solr是一個(gè)搜索引擎。Spring Boot為solr客戶端庫及Spring Data Solr提供的基于solr客戶端庫的抽象提供了基本的配置。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文件):
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-solr</ artifactId > </ dependency > |
可以像其他Spring beans一樣注入一個(gè)自動配置的SolrServer實(shí)例。默認(rèn)情況下,該實(shí)例將嘗試使用localhost:8983/solr連接一個(gè)服務(wù)器。
1
2
3
4
5
6
7
8
9
|
@Component public class MyBean { private SolrServer solr; @Autowired public MyBean(SolrServer solr) { this .solr = solr; } // ... } |
如果添加一個(gè)自己的SolrServer類型的@Bean,它將會替換默認(rèn)的。
應(yīng)用集成Solr搜索客戶端案例
Spring Boot的配置是集中性的(可以拆分成不同的配置文件),因此在application.properties文件中添加以下配置:
1
2
3
4
|
# 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)方式)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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ù),具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@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查詢,具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@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í)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.yoodb.com/yoodb/article/detail/1423