引言:

  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(四)的更多相关文章

  1. springboot整合solr

    上一篇博客中简要写了solr在windows的安装与配置,这一篇接上文写一下springboot整合solr,代码已经上传到github,传送门. 1.新建core并配置schema 上篇博客中已经有 ...

  2. solr(四) : springboot 整合 solr

    前言: solr服务器搭起来, 数据导入之后, 就该应用到项目中去了. 那在项目中, 该怎么整合和应用solr呢? 接下来, 就来整合和应用solr 一. 整合 1. 引入jar包 <prope ...

  3. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  4. SpringBoot整合NoSql--(四)Session共享

    简介: 正常情况下,HttpSession是通过Servlet 容器创建并进行管理的,创建成功之后都是保存在内存中.如果开发者需要对项目进行横向扩展搭建集群,那么可以利用一些硬件或者软件工具来做负载均 ...

  5. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  6. SpringBoot整合elasticsearch(三)

    Docker安装elasticsearch 启动注意2点,1是内存,2是线程数(此处进行简单安装,后面会详细补充es文档) [root@topcheer ~]# docker images REPOS ...

  7. springboot整合多数据源解决分布式事务

    一.前言        springboot整合多数据源解决分布式事务.             1.多数据源采用分包策略              2.全局分布式事务管理:jta-atomikos. ...

  8. springboot(十四):springboot整合shiro-登录认证和权限管理(转)

    springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...

  9. SpringBoot整合MyBatis与MySql8.0

    一.前言 之前已经有一篇文章讨论过SpringBoot整合MyBatis,因而此篇不在重复累赘,本文主要是最新版的SpringBoot2.0与MyBatis.最新MySQL8.0整合过程中遇到的问题进 ...

随机推荐

  1. Redis 缓存失效和回收机制续

    二.Redis Key失效机制 Redis的Key失效机制,主要借助借助EXPIRE命令: EXPIRE key 30 上面的命令即为key设置30秒的过期时间,超过这个时间,我们应该就访问不到这个值 ...

  2. Spring Boot Web 自定义注解篇(注解很简单很好用)

    自从spring 4.0 开放以后,可以添加很多新特性的注解了.使用系统定义好的注解可以大大方便的提高开发的效率. 下面我贴一段代码来讲解注解: 通过小小的注解我们支持了以下功能: 使 spring. ...

  3. win10安装gitLab

    从控制面板选择hyper-V进行安装 1.打开控制面板选择程序=>选择启用或关闭windows功能=>选择Hyper-v 安装ubuntu 1.下载ubuntu系统(本次安装为18.04. ...

  4. 从On-Premise本地到On-Cloud云上运维的演进

    摘要: 从用户的声音中,我们听到用户对稳定.弹性.透明的诉求,我们也在不断升级ECS的运维能力和体验,助力用户建立主动运维体系,赋能业务永续运行.为了让大家更好的了解和用好ECS弹性计算服务,从本期开 ...

  5. 微信小程序echarts层级太高

    项目中因为需求,底部的tab导航栏是自己写的,在开发者工具中一切正常:但是在真机上页面滑动时,echarts的层级比tab高,调过两者的z-index后仍然如此. 经过查找后发现cover-view和 ...

  6. html 中 xmp标记

    HTML页面中显示HTML标签代码,可以使用<xmp>html标签内容</xmp>,这样,在网页中就会显示html标签 for(var i=0;i<columns.len ...

  7. 免费开源ERP-成功案例分析(1)

    Odoo用户案例 Odoo用户概要 关于Odoo全球的用户,我们来看一些数据: Odoo目前全球有300万使用者 Odoo系统上每天新创建的数据库超过1000个 Odoo和Word.Excel.Pow ...

  8. Android之PhotoView使用

    文章大纲 一.什么是PhotoView二.代码实战三.项目源码下载 一.什么是PhotoView   一款 ImageView 展示框架,支持缩放,响应手势,位于图片排行榜的第五位,PhotoView ...

  9. Git 中 .gitignore 的配置语法

    一.前言 在日常的开发中,当我们需要将一个项目提交到 Git 时,并不是所有的文件都需要提交,比如一些自动生成的文件,类似于 .idea 文件.class 文件等,这时候就可以使用.gitignore ...

  10. mysql 8 nodejs连不上

    https://www.jianshu.com/p/bf37e0bc7080 alter user 'root'@'localhost' identified with mysql_native_pa ...