通过spring boot构建solr查询实例
来源:互联网 发布:手机淘宝领天猫积分 编辑:程序博客网 时间:2024/05/24 07:26
通过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/solrspring.solr.zkHost=192.168.219.134:2181,192.168.219.140:2181spring.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 {@Autowiredprivate SolrConfig solrConfig;private CloudSolrServer solrServer;@PreDestroypublic 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查询
@RestControllerpublic class HelloController {@Autowiredprivate 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());}
}
阅读全文
0 0
- 通过spring boot构建solr查询实例
- springboot构建solr查询实例
- 通过Spring Boot构建项目工程
- spring boot demo 通过maven构建
- Spring Boot 实例通过接口安全退出
- Spring Boot 实例通过接口安全退出
- spring boot 集成solr
- Spring Boot Jpa之构建动态SQL查询语句
- Spring Boot构建应用
- maven构建Spring Boot
- docker构建spring boot
- Spring Boot 构建框架
- Spring boot 入门 实例
- Spring Boot AOP实例
- spring-boot 实例
- Spring Boot devtools实例
- Spring Boot入门实例
- Spring boot 入门实例
- MySQL日期的加减
- swift学习日记
- MySQL命令-- mysqlbinlog
- 【Leetcode】【python】Permutations
- 模拟-Codeforces 777C
- 通过spring boot构建solr查询实例
- 仿真三件套
- 克拉美罗下界 CRLB的计算
- Leetcode 122 Best Time to Buy and Sell Stock II
- linux下动态库(共享库)
- 使用foreach操作数组
- HDU 5977 树的分治+子集枚举
- 1560 扔人游戏
- Leetcode 349 Intersection of Two Arrays