Spring-Boot+Solr搜索应用(索引数据创建+关键字高亮+thymeleaf静态html模板渲染跳转)
2018-02-07 15:57
996 查看
先来看一下效果,三张图
A. Solr服务中,按条件查询商品名称为Mac开头的所有办公电脑----> q,item_name:Mac*
fq === filer query 过滤查询,相当于 sql语句的 where(根据指定条件查询)
sort : 根据哪个字段进行排序
start,rows : 分页
fl : 显示哪些字段,比如,下面的查询结果规定显示的字段只有商品的id 和 item_name
wt : 结果集的数据格式
hl : highLight,是否高亮,选择既是高亮,随后设置高亮字段,多个需要高亮的字段逗号隔开
....hl的属性不说了,后面在demo演示中会再次提及
B.Spring-Boot中根据条件过滤检索内容,效果如下:
C.Spring-Boot根据上一步查询的结果,通过thymeleaf渲染html后的效果如下
一、项目结构图
二、项目相关pom依赖添加
本篇额外需要增加的两个依赖如下
三、application.properties添加solr服务url
这个地方的地址一定不要写错了
格式: ip:port/solr/core的名称
比如实际访问的地址应该是:http://localhost:8080/solr/index.html#/new_core
但是我们在Spring-Boot的配置中应该去掉#号和index.html,改写成---->http://localhost:8080/solr/new_core
四、创建SolrController(Solr的增删改查)
注意,不要使用@RestController注解,否则thymeleaf渲染html失败,无法实现页面跳转
(1)SolrController.java
(2)增加
可以参考京东的商品页面进行内容提取
对应demo中就是setField,完了add
启动Spring-Boot
使用工具测试API:http://localhost:8088/solr/add
添加成不成功我们还有待验证,最直接的方式就是在solr服务中直接查询全部
(3)查询
查询的关键就是条件的拼接,条件的拼接必须知道solr查询中各个符号所表示的含义,注释如下
所谓高亮,就是针对搜索结果中的关键字进行特殊处理,比如,字体颜色突出显示,如下
由于在demo中打印无法体现出高亮字段颜色的变化,所以我们需要借助thymeleaf模板渲染,对view.html进行数据包装+渲染
view.html
thymeleaf结果集遍历:th:each标签
搜索测试:查询办公电脑类,所有以Mac开头的(也就是苹果电脑),且价钱在1万以上的电脑
A.工具测试
B.浏览器中测试
(4)删除
执行删除
删除后,再调用query接口查询(这时候已经检索不到数据了)
A. Solr服务中,按条件查询商品名称为Mac开头的所有办公电脑----> q,item_name:Mac*
fq === filer query 过滤查询,相当于 sql语句的 where(根据指定条件查询)
sort : 根据哪个字段进行排序
start,rows : 分页
fl : 显示哪些字段,比如,下面的查询结果规定显示的字段只有商品的id 和 item_name
wt : 结果集的数据格式
hl : highLight,是否高亮,选择既是高亮,随后设置高亮字段,多个需要高亮的字段逗号隔开
....hl的属性不说了,后面在demo演示中会再次提及
B.Spring-Boot中根据条件过滤检索内容,效果如下:
-------------------高亮效果部分展示------------------------- 3 电脑办公类 <font color='red'>MacBook</font> Pro APPLE 苹果MacBook Pro 17年新款 笔记本电脑高端轻薄时尚游戏办公 15.4英寸 Bar i7 16G 256G闪存 2016款 银色 13838.0 ============分割线============== 4 电脑办公类 苹果(Apple) <font color='red'>MacBook</font> PRO 苹果(Apple) MacBook PRO苹果笔记本电脑 2016/2017新款原装日版 17款15英寸灰MPTT2Bar/16G/512G 17688.0 ============分割线============== 查询到的总条数:2, 内容:{numFound=2,start=0,docs=[SolrDocument{id=3, item_cat_name=[电脑办公类], item_name=[MacBook Pro], item_desc=[APPLE 苹果MacBook Pro 17年新款 笔记本电脑高端轻薄时尚游戏办公 15.4英寸 Bar i7 16G 256G闪存 2016款 银色], item_price=[13838.0], item_name_str=[MacBook Pro], _version_=1591732365446610944, item_desc_str=[APPLE 苹果MacBook Pro 17年新款 笔记本电脑高端轻薄时尚游戏办公 15.4英寸 Bar i7 16G 256G闪存 2016款 银色], item_cat_name_str=[电脑办公类]}, SolrDocument{id=4, item_cat_name=[电脑办公类], item_name=[苹果(Apple) MacBook PRO], item_desc=[苹果(Apple) MacBook PRO苹果笔记本电脑 2016/2017新款原装日版 17款15英寸灰MPTT2Bar/16G/512G], item_price=[17688.0], item_name_str=[苹果(Apple) MacBook PRO], _version_=1591732365446610946, item_desc_str=[苹果(Apple) MacBook PRO苹果笔记本电脑 2016/2017新款原装日版 17款15英寸灰MPTT2Bar/16G/512G], item_cat_name_str=[电脑办公类]}]} Query,被调用次数:2,综合耗时:6ms
C.Spring-Boot根据上一步查询的结果,通过thymeleaf渲染html后的效果如下
一、项目结构图
二、项目相关pom依赖添加
本篇额外需要增加的两个依赖如下
<!-- 添加thymeleaf 支持页面跳转 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
<!-- 添加solr依赖 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-solr</artifactId> </dependency>
<!-- 解决配置资源文件被漏掉问题 --> <resources> <!-- 如果出现thymeleaf无法渲染html模板,请加上这个 --> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
三、application.properties添加solr服务url
#solr服务Url spring.data.solr.host=http://localhost:8080/solr/new_core
这个地方的地址一定不要写错了
格式: ip:port/solr/core的名称
比如实际访问的地址应该是:http://localhost:8080/solr/index.html#/new_core
但是我们在Spring-Boot的配置中应该去掉#号和index.html,改写成---->http://localhost:8080/solr/new_core
四、创建SolrController(Solr的增删改查)
注意,不要使用@RestController注解,否则thymeleaf渲染html失败,无法实现页面跳转
(1)SolrController.java
package com.appleyk.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.appleyk.result.ResponseMessage; import com.appleyk.result.ResponseResult; @Controller public class SolrController { /** * 装载SolrClient实例,前提是solr的host地址配对后 */ @Autowired SolrClient solrClient; @RequestMapping("/solr/add") @ResponseBody public ResponseResult Test() throws Exception { List<SolrInputDocument> docs = new ArrayList<>(); SolrInputDocument doc = new SolrInputDocument(); /** * 添加字段索引,id一样,为修改,id不一样,为新增 */ doc.setField("id", "1"); /** * 商品类目 */ doc.setField("item_cat_name", "电脑办公类"); /** * 商品名称 */ doc.setField("item_name", "联想天逸510S"); /** * 商品简单描述 */ doc.setField("item_desc", "联想(Lenovo)天逸510S商用台式办公电脑整机(i3-7100 4G 1T 集显 WiFi 蓝牙 三年上门 win10)19.5英寸"); /** * 商品价格 */ doc.setField("item_price", "3099.00"); /** * 将创建好的单个SolrInputDocument对象添加进Solr文档集合列表中 */ docs.add(doc); doc = new SolrInputDocument(); doc.setField("id", "2"); doc.setField("item_cat_name", "电脑办公类"); doc.setField("item_name", "联想天逸510S"); doc.setField("item_desc", "联想(Lenovo)天逸510S商用台式办公电脑整机(i3-7100 4G1T 集显 WiFi 蓝牙 三年上门 win10)21.5英寸"); doc.setField("item_price", "3299.00"); docs.add(doc); doc = new SolrInputDocument(); doc.setField("id", "3"); doc.setField("item_cat_name", "电脑办公类"); doc.setField("item_name", "MacBook Pro"); doc.setField("item_desc", "APPLE 苹果MacBook Pro 17年新款 笔记本电脑高端轻薄时尚游戏办公 15.4英寸 Bar i7 16G 256G闪存 2016款 银色"); doc.setField("item_price", "13838.00"); docs.add(doc); doc = new SolrInputDocument(); doc.setField("id", "4"); doc.setField("item_cat_name", "电脑办公类"); doc.setField("item_name", "华为MateBook X"); doc.setField("item_desc", "华为(HUAWEI) MateBook X 13英寸超轻薄微边框笔记本(i5-7200U 8G 256G 拓展坞 2K屏 指纹 office)玫瑰金"); doc.setField("item_price", "7688.00"); docs.add(doc); doc = new SolrInputDocument(); doc.setField("id", "4"); doc.setField("item_cat_name", "电脑办公类"); doc.setField("item_name", "苹果(Apple) MacBook PRO"); doc.setField("item_desc", "苹果(Apple) MacBook PRO苹果笔记本电脑 2016/2017新款原装日版 17款15英寸灰MPTT2Bar/16G/512G"); doc.setField("item_price", "17688"); docs.add(doc); /** * 一次性全部添加 */ solrClient.add(docs); /** * 提交 */ solrClient.commit(); return new ResponseResult(ResponseMessage.OK); } @RequestMapping("/solr/query") public String Query(ModelMap map) throws Exception { SolrQuery solrQuery = new SolrQuery(); /** * *:*:表示查询全部 *Mac*代表左右模糊匹配,如果写Mac代表绝对匹配 */ solrQuery.set("q", "item_name:Mac*"); /** * 过滤条件:电脑办公类,10000元以上 [A TO B] 范围A到B之间 [A TO *] A到无穷 [* TO B] B以下 * 相当于sql语句中的where ----->fq = filter query */ solrQuery.set("fq", "item_cat_name:电脑办公类"); solrQuery.set("fq", "item_price:[10000 TO *]"); // 分页,0开始,每页5条,setStart设置的就是显示第几页 solrQuery.setStart(0); solrQuery.setRows(5); // 开启高亮 solrQuery.setHighlight(true); // 添加高亮字段,多个字段之间逗号隔开比如: A,B,C solrQuery.addHighlightField("item_name,item_price"); // 设置高亮字段的前缀 solrQuery.setHighlightSimplePre("<font color='red'>"); // 设置高亮字段的后缀 solrQuery.setHighlightSimplePost("</font>"); // 执行查询 QueryResponse response = solrClient.query(solrQuery); // 文档结果集 SolrDocumentList docs = response.getResults(); System.err.println("-------------------高亮效果部分展示-------------------------"); // 高亮显示的返回结果 Map<String, Map<String, List<String>>> maplist = response.getHighlighting(); /** * 静态html资源里面的对象 -- ${list} */ List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> m; // 返回高亮之后的结果.. for (SolrDocument solrDocument : docs) { String id = solrDocument.getFirstValue("id").toString(); String item_cat_name = solrDocument.getFirstValue("item_cat_name").toString(); String item_desc = solrDocument.getFirstValue("item_desc").toString(); String item_price = solrDocument.getFirstValue("item_price").toString(); System.err.println(id); System.err.println(item_cat_name); Map<String, List<String>> fieldMap = maplist.get(solrDocument.get("id")); List<String> stringlist = fieldMap.get("item_name"); String item_name = stringlist.get(0); System.err.println(item_name); System.err.println(item_desc); System.err.println(item_price); m = new HashMap<String, Object>(); m.put("id", id); m.put("item_cat_name", item_cat_name); m.put("item_name", item_name); m.put("item_desc", item_desc); m.put("item_price", item_price); list.add(m); System.err.println("============分割线=============="); } System.err.println("查询到的总条数:" + docs.getNumFound() + ", 内容:" + docs); map.addAttribute("list", list); return "view"; } @RequestMapping("/solr/deleteall") @ResponseBody public ResponseResult DeleteAll() throws Exception { // 清空所有数据 solrClient.deleteByQuery("*:*"); solrClient.commit(); return new ResponseResult(ResponseMessage.OK); } }
(2)增加
可以参考京东的商品页面进行内容提取
对应demo中就是setField,完了add
启动Spring-Boot
使用工具测试API:http://localhost:8088/solr/add
添加成不成功我们还有待验证,最直接的方式就是在solr服务中直接查询全部
(3)查询
查询的关键就是条件的拼接,条件的拼接必须知道solr查询中各个符号所表示的含义,注释如下
所谓高亮,就是针对搜索结果中的关键字进行特殊处理,比如,字体颜色突出显示,如下
由于在demo中打印无法体现出高亮字段颜色的变化,所以我们需要借助thymeleaf模板渲染,对view.html进行数据包装+渲染
view.html
thymeleaf结果集遍历:th:each标签
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <title>Hello World!</title> </head> <body> <table border="1"> <tr> <td>商品ID</td> <td>商品类目</td> <td>商品名称</td> <td>商品描述</td> <td>商品价格</td> </tr> <tr th:each="item : ${list}"><!-- 其中item是个临时变量,像for(User u : userList)那样中的u--> <td th:text="${item.id}"/> <td th:text="${item.item_cat_name}"/> <!-- 接收html格式的文本 --> <td th:utext="${item.item_name}"/> <td th:text="${item.item_desc}"/> <td th:text="${item.item_price}"/> </tr> </table> </body> </html>
搜索测试:查询办公电脑类,所有以Mac开头的(也就是苹果电脑),且价钱在1万以上的电脑
A.工具测试
B.浏览器中测试
(4)删除
执行删除
删除后,再调用query接口查询(这时候已经检索不到数据了)
相关文章推荐
- solr集成springboot并向solr推送索引数据更新和查询
- springboot使用thymeleaf模板渲染html,报错“whitelabel error page,type=not found,status=404”
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- ElasticSearch+Springboot实际应用:索引同步建设,搜索过程
- spring boot 使用velocity、freeMarker模板创建html页面返回给前端
- SpringBoot+MyBatis简单数据访问应用的实例代码
- solr安装部署、solr测试创建core、用solrj 访问solr(索引和搜索)
- B树索引+B树索引逻辑结构图+B树索引是怎么搜索数据+创建B树索引
- B-树和B+树的应用:数据搜索和数据库索引
- Spring Boot与FreeMarker集成后配置全局模板转义html/xml
- 解决 IntelliJ IDEA 无法热加载 Spring Boot 模板文件和静态资源文件
- springboot整合freemarker生成静态HTML页面
- B-树和B+树的应用:数据搜索和数据库索引
- B-树和B+树的应用:数据搜索和数据库索引
- spring cloud教程之使用spring boot创建一个应用
- springboot如何使用thymeleaf模板访问html页面
- 应用跳转到AppStore指定关键字搜索界面
- Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法
- B-树和B+树的应用:数据搜索和数据库索引
- 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索