3.8、Lucene文件检索实战 —— 结果展示


1、引入 Thymeleaf 模板引擎

2、定义搜索结果 —— result.html

3、调试搜索结果页面

4、点击文件名下载文件

-———————————-

代码仓库:https://gitee.com/carloz/lucene-learn.git

https://gitee.com/carloz/lucene-learn/tree/master/lucene-filesearch

-———————————-

工程结构

1、引入 Thymeleaf 模板引擎

1、pom.xml 中引入依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.properties 添加 Thymeleaf 配置

1
2
3
4
5
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/

2、定义搜索结果 —— result.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>搜索结果</title>
<link type="text/css" rel="stylesheet" href="css/base.css"/>
<link type="text/css" rel="stylesheet" href="css/page_search_result.css"/>
<link type="text/css" rel="stylesheet" href="css/moudle_g_search_bar.css"/>
</head>
<body>
<div class="wrap">
<div class="main clearfix">
<div class="header"><br/><br/></div>
<div id="search_result_panel">
<div class="content_top">
<div class="g_search_bar">
<div style="display: inline-block">
&nbsp;&nbsp;&nbsp;<img alt="文件检索" src="images/logo.png" width="40px" height="35px"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</div>
<div style="display: inline-block" >
<form class="search_form" action="/search">
<input class="search_text" name="keywords" type="text" th:value="${keywords}" />
<input class="btn_submit" type="submit" value="搜索" />
</form>
</div>
</div>
<div class="clear"></div>
<div><br /><br /></div>
</div>
<div class="content_left">
<div class="result_item_article" style="display: none;">
<h3><a href="#">coding的最新相关信息</a></h3>
<ul>
<li>最新更新:2015-4-3 2:25</li>
<li>122人赞同</li>
<li>44人反对</li>
</ul>
<div class="article_body">
<a href="#">
<img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/sealyao/594039/o_clip_image001_thumb.jpg" />
</a>
<div>哈哈哈哈哈哈嘎嘎嘎嘎嘎嘎哈哈哈哈哈哈嘎嘎嘎嘎嘎哈哈哈哈哈哈哈嘎嘎嘎嘎嘎哈哈哈哈嘎嘎嘎嘎嘎哈哈哈哈嘎嘎嘎嘎嘎哈哈哈哈嘎嘎嘎嘎嘎哈哈哈哈嘎嘎嘎嘎嘎哈哈哈哈</div>
</div>
<div class="article_label">
<span>标签:</span>
<a href="#">ide</a>
<a href="#">coding</a>
<a href="#">ide</a>
<a href="#">coding</a>
<a href="#">ide</a>
</div>
</div>
<div class="result_item_article" th:each="resultItem:${resultList}">
<h3>
<a th:href="@{'/download?fileName='+${resultItem.filename}}"
th:utext="${resultItem.title}"></a>
</h3>
<div class="article_body" th:utext="${resultItem.content}"></div>
<div class="article_label">
</div>
</div>
<div th:if="${null == resultList || 0 == resultList.size()}">
<h1>没有搜索到相关数据 (;-_-)ᴇᴍᴍᴍ</h1>
</div>
</div>
<div class="content_right"></div>
<div class="clear"></div>
<div class="refer_link"></div>
<div class="page_index"></div>
</div>
</div>
</div>
<div th:include="footer"></div>
</body>
</html>

3、调试搜索结果页面

定义url:search

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param keywords
* @return 搜索结果页面
*/
@RequestMapping("/search")
public String searchFile(String keywords, Model model) {
String indexPathStr = "indexdir";
ArrayList<FileModel> hitsList = getTopDoc(keywords, indexPathStr, 100);
log.info(keywords + ":共搜到:" + hitsList.size() + " 条数据!");
model.addAttribute("keywords", keywords);
model.addAttribute("resultList", hitsList);
return "result";
}

访问它:

http://localhost:18080/search?keywords=%E6%97%A0%E4%BA%BA%E9%A9%BE%E9%A9%B6

http://localhost:18080/search?keywords=%E8%B4%9F%E8%B4%A3

4、点击文件名下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.learn.lucenefilesearch.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.nio.charset.Charset;

/**
* 文件下载
* 访问:http://localhost:18080/download?fileName=Mycat-config.xml
*/
@Slf4j
@Controller
public class FileDownloadServletController {

@RequestMapping("/download")
public ResponseEntity<FileSystemResource> downloadFile(String fileName) throws Exception {
String filePath = "files/" + fileName;
File file = new File(filePath);
log.info("download path:" + file.getPath());
FileSystemResource fileSystemResource = new FileSystemResource(file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentLength(file.length());
headers.setCacheControl(CacheControl.noStore());
headers.setContentDisposition(
ContentDisposition.builder("attachment")
.filename(fileName, Charset.forName("UTF-8"))
.size(file.length())
.build()
);
return ResponseEntity.ok()
.headers(headers)
.body(fileSystemResource);
}
}

访问:http://localhost:18080/download?fileName=Mycat-config.xml

http://localhost:18080/download?fileName=session共享版上线(第二版).pptx

控制台输出: