solr8.0 springboot整合solr(四)
引言:
solr搭建起后,就该应用到java后台开发里了,接下来就用springboot整合应用solr
一:引入jar包
<!--solr-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>4.0.6.RELEASE</version>
</dependency> <!--操作solr的工具-->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.0.0</version>
</dependency>
二:对application配置文件进行配置(mycore1是我创建的核心,具体名字改为你所创建的核心)
spring:
data:
solr:
host: http://127.0.0.1:8983/solr/mycore1
三:接下来就是代码操作了(详细解释看注解),这个只是我的服务层
@Service
public class SearchService { @Autowired
private SolrClient solrClient;
//search就是搜索的内容,currentpage是因为我做了分页,如果没做分页可忽略此参数
public PageResult searchNews(String search,int currentPage) throws IOException, SolrServerException {
// 创建solr查询对象
SolrQuery query = new SolrQuery();
if(null != search && !"".equals(search)){
// 设置查询关键词
query.setQuery(search);
// 设置默认查询域
query.set("df", "news_keywords");
}
// 高亮显示
query.setHighlight(true);
// 设置高亮显示字段
query.addHighlightField("newsTitle,newsAbstract");
query.setHighlightSimplePre("<span style='color:red'>");
query.setHighlightSimplePost("</span>");
// 设置排序规则
query.setSort("newsTime",SolrQuery.ORDER.desc);
// 设置返回格式
query.set("wt","json");
// 设置分页
query.set("start", (currentPage - 1) * 10);
query.set("rows", 10);
// 进行查询得到返回结果
QueryResponse queryResponse = solrClient.query(query);
// 取出高亮部分
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
// 得到主体数据部分
SolrDocumentList results = queryResponse.getResults(); ArrayList<NewsWithBLOBs> newsList = new ArrayList<>();
// 对主体数据进行遍历,将数据依次保存到news对象中,然后将news对象加入list集合就是查询到的所有新闻
for (SolrDocument result : results){
NewsWithBLOBs news = new NewsWithBLOBs();
news.setNewsId(result.get("id").toString());
news.setNewsCover(result.get("newsCover").toString());
news.setNewsTime((Date) result.get("newsTime"));
news.setNewsBrowse((Integer) result.get("newsBrowse"));
news.setNewsSchoolid(result.get("newsSchoolid").toString());
news.setNewsCategoryid(result.get("newsCategoryid").toString());
news.setNewsAbstract(result.get("newsAbstract").toString());
news.setNewsContent(result.get("newsContent").toString());
// 设置高亮部分,下边是得到指定新闻id的高亮部分,并且将高亮部分设置进入对象中
Map<String, List<String>> map = highlighting.get(result.get("id"));
List<String> list = map.get("newsAbstract");
if(null != list && list.size() > 0){
String newsAbstract = list.get(0);
news.setNewsAbstract(newsAbstract);
}
List<String> list1 = map.get("newsTitle");
if(null != list1 && list1.size() > 0){
String newsTitle = list1.get(0);
news.setNewsTitle(newsTitle);
}
newsList.add(news);
} // 得到所获得的新闻条数
long numFound = results.getNumFound();
// 下边是我自己的分页封装,可忽略,上边的到的newslist就是获得的所有新闻
PageResult result = new PageResult();
result.setRecordCount(numFound);
System.out.println(numFound);
result.setTotalPages((int) (numFound%10 == 0 ? numFound/10 : numFound/10+1));
result.setList(newsList);
return result;
}
}
通过上边就能获取到指定的查询对象了,并且高亮显示也正常
solr8.0 springboot整合solr(四)的更多相关文章
- springboot整合solr
上一篇博客中简要写了solr在windows的安装与配置,这一篇接上文写一下springboot整合solr,代码已经上传到github,传送门. 1.新建core并配置schema 上篇博客中已经有 ...
- solr(四) : springboot 整合 solr
前言: solr服务器搭起来, 数据导入之后, 就该应用到项目中去了. 那在项目中, 该怎么整合和应用solr呢? 接下来, 就来整合和应用solr 一. 整合 1. 引入jar包 <prope ...
- SpringBoot整合Shiro 四:认证+授权
搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...
- SpringBoot整合NoSql--(四)Session共享
简介: 正常情况下,HttpSession是通过Servlet 容器创建并进行管理的,创建成功之后都是保存在内存中.如果开发者需要对项目进行横向扩展搭建集群,那么可以利用一些硬件或者软件工具来做负载均 ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合elasticsearch(三)
Docker安装elasticsearch 启动注意2点,1是内存,2是线程数(此处进行简单安装,后面会详细补充es文档) [root@topcheer ~]# docker images REPOS ...
- springboot整合多数据源解决分布式事务
一.前言 springboot整合多数据源解决分布式事务. 1.多数据源采用分包策略 2.全局分布式事务管理:jta-atomikos. ...
- springboot(十四):springboot整合shiro-登录认证和权限管理(转)
springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...
- SpringBoot整合MyBatis与MySql8.0
一.前言 之前已经有一篇文章讨论过SpringBoot整合MyBatis,因而此篇不在重复累赘,本文主要是最新版的SpringBoot2.0与MyBatis.最新MySQL8.0整合过程中遇到的问题进 ...
随机推荐
- 关于vue使用form上传文件
在vue中使用form表单上传文件文件的时候出现了一些问题,获取文件的时候一直返回null, 解决之后又出现发送到后台的file文件后台显示为空,解决源码 <template> <d ...
- 设计模式 | 抽象工厂模式(abstract factory)
定义: 提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. 结构:(书中图,侵删) 这个图相对来说有一点点复杂,其实就是在工厂方法模式的基础上做了一些扩展,工厂方法模式只用于生成一种 ...
- ES6数组扩展运算符
1 扩展运算符的运用 (1)复制数组 数组是复合的数据类型,直接复制的话,只是复制了指向底层数据机构的指针,而不是克隆一个全新的数组; const a1=[1,2]; const a2= a1; a2 ...
- Android组件化开发的简单应用
组件化开发的主要步骤: 一.新建Modules 1.新建Project,作为应用的主Module. 2.新建Module:"Common",类型选择"Android Li ...
- Centos7.3离线(rpm方式)安装mysql服务
1.mysql官网下载安装包,官网地址:www.mysql.com [root@seiang software]# ll total 580020 -rw-r--r--. 1 root root 59 ...
- IoC和AOP的理解
spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实 ...
- pandas 对数据帧DataFrame中数据的索引及切片操作
1.创建数据帧 index是行索引,即每一行的名字:columns是列索引,即每一列的名字.建立数据帧时行索引和列索引都需要以列表的形式传入. import pandas as pd df = pd. ...
- SignalR使用笔记
最近项目要求添加一个给用户发送消息的功能,就决定使用SignalR.翻到了以前学习SignalR的学习笔记,基本是官方文档的简版整理,便于快速阅览和实现. 1. nuget添加signalr引用: a ...
- 常用Latex公式
注意: 1 在博客中书写Latex公式时,需在公式两侧用$包括 2 大括号{ }在Latex有本身的含义,如果要输出为普通字符需要使用\{...\} 符号 公式 说明 $ \in $ \in 包含 $ ...
- Python基础(max,min方法)
max函数(min函数使用方法一致): #-----------------max函数----------------- #max之简单层次的使用 li=[1,0,2,45,12,-2,7,9] re ...