引言:

  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. 如何使用FluentMigrator进行数据库迁移

    标题:如何使用FluentMigrator进行数据库迁移 地址:https://www.cnblogs.com/lwqlun/p/10649949.html 作者: Lamond Lu FluentM ...

  2. Windows核心编程第一章.错误处理

    Windows核心编程第一章,错误处理. 一丶错误处理 1.核心编程学习总结 不管是做逆向,开始做开发.在Windows下.你都需要看一下核心编程这本书.这本书确实写得很好.所以自己在学习这本书的同时 ...

  3. Oracle 经典面试题

    第一题 create table test( id ) primary key, type ) , t_id ), value ) ); ,,,'张三'); ,,,'男'); ,,,'); ,,,'刘 ...

  4. 自定义超链接动画---transition

    效果图: <a href="#"> <span>HTML</span> </a> a { position: relative; t ...

  5. gitbook 入门教程之环境要求

    gitbook 是基于 node.js 的命令行工具,首先需要安装并配置好 node.js 环境,然后才能安装gitbook 相关工具. 由于安装工具全部都是国外网站,因此速度可能会很慢,也可能需要F ...

  6. Centos7.3离线(rpm方式)安装mysql服务

    1.mysql官网下载安装包,官网地址:www.mysql.com [root@seiang software]# ll total 580020 -rw-r--r--. 1 root root 59 ...

  7. windows下nginx的安装及使用

    安装过程比较简单 1.下载nginx http://nginx.org/en/download.html 下载稳定版本,以nginx/Windows-1.14.2为例,直接下载 nginx-1.14. ...

  8. Cocos Creator 资源加载流程剖析【一】——cc.loader与加载管线

    这系列文章会对Cocos Creator的资源加载和管理进行深入的剖析.主要包含以下内容: cc.loader与加载管线 Download部分 Load部分 额外流程(MD5 Pipe) 从编辑器到运 ...

  9. 图的BFS----迷宫问题

    题目描述: ...11111111111111111111111111111 11.111111........1111111111.1111 11.111111..111.11111111..... ...

  10. Java 枚举类详解

    1. 枚举类定义 在某些情况下,一个类的对象是有限而且固定的,比如季节类,它只有4个对象,这种实例有限而且固定的类,在Java里被称为枚举类. 2. 早期实现枚举的方式 public static f ...