通过spring boot构建solr查询实例
Posted Java架构研究室
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过spring boot构建solr查询实例相关的知识,希望对你有一定的参考价值。
曾经使用spring我们需要各种各样的配置web.xml,配置springapplication.xml。真的很繁琐,要建一个项目框架出来,确实不太容易。而现在spring社区推出spring boot。完全不需要去管那些xml的配置了,springboot采用约定优于配置的思想,通过你项目所需的dependencies来自动配置框架。
springboot的特性:
1自动配置文件
2.直接内置tomcat或jetty web服务器不需要打war包直接通过mvn spring-boot:run就可以运行。
3.只需要提供几个简单依赖就可以提供像springmvc,mybatis,jpa等框架支持。
springboot实例
该实例使用maven构建项目,以下是maven依赖。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<guava.version>18.0</guava.version>
<commons.lang3.version>3.3.2</commons.lang3.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
application.properties
spring.solr.host=http://192.168.219.134:8080/solr
spring.solr.zkHost=192.168.219.134:2181,192.168.219.140:2181
spring.solr.defaultCollection=demo
通过一个bean接收zk服务器相关属性
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
@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())) {
//solrClient = new CloudSolrClient(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());
}
}
喜欢文章的朋友们不要忘了扫一扫关注一下哦,每日更新干货好文~
---------------------
以上是关于通过spring boot构建solr查询实例的主要内容,如果未能解决你的问题,请参考以下文章
JPA 左连接查询不会在 Spring Boot 中实例化子集合?
有没有办法从 Spring Boot 中的参数列表中查询方法名称
架构实战篇:Spring Boot 集成企业级搜索引擎 SolrCloud