本文参考自:https://blog.csdn.net/Q54665642ljf/article/details/127701719
本文适用于elasticsearch
入门小白,还请大佬能指出我的不足(本人其实也是刚学elasticsearch
没多久)
文章目录
- 一、准备工作
- 1.1 安装ES文本抽取插件
- 1.2 定义文本抽取管道(pipeline)
- 1.3 创建索引
- 二、在 Kibana 中测试添加文档
- 2.1 先把文件转为Base64形式
- 2.2 向ES中添加一条记录
- 2.3 测试关键词高亮搜索
- 三、SpringBoot 实现
- 3.1 elasticsearch配置
- (1)pom.xml
- (2)application.yml
- (3)ElasticSearchConfig 类
- (4)elasticsearch 工具类
- 3.2 DocumentObj 实体类
- 3.3 Service 接口
- 3.4 ServiceImpl 实现类
- 3.5 Controller 层
- 3.6 测试
- (1)加载文件
- (2)关键字查询
一、准备工作
1.1 安装ES文本抽取插件
(1)为什么要有文本抽取插件?
对于word
、pdf
等文档类型的文件而言,它们文件底层的内容除了纯文本之外,还会有很多杂乱的信息(比如在一个word
文件中,除了文本内容,还包含了页面设置、字体大小、颜色等无关信息)
为了剔除文档中与文本无关的信息,所以才需要使用文本抽取插件。
(2)如何安装文本抽取插件?
在 elasticsearch
的bin
目录下,使用elasticsearch-plugin
来安装文本抽取插件ingest-attachment
。
# windows下命令(进到bin目录):
elasticsearch-plugin install ingest-attachment
# Linux下命令(进到bin目录):
./elasticsearch-plugin install ingest-attachment
为了方便后续检索文本,需要安装一个IK分词器插件(官方下载地址:https://github.com/medcl/elasticsearch-analysis-ik
官方里面也有说明如何进行下载。选择一个和你elasticsearch
版本相同的版本进行下载即可,比如执行以下命令:
# windows下(进到bin目录):
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-7.6.2.zip
# Linux下(进到bin目录)
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-7.6.2.zip
命令执行完毕,在plugins
目录下可以看到相关插件已安装。
1.2 定义文本抽取管道(pipeline)
(1)什么是管道(pipeline)?
pipeline
也叫做“预处理管道”,它主要的作用是可以在存储内容时,对字段进行加工。
比如,有一串奇怪的字符串&*he@¥#ll%&o……¥%
,在不进行加工的情况下,我直接丢给用户看,那么用户看到的就是&*he@¥#ll%&o……¥%
,无法看见其中的关键信息。
但是,假如我有这么一个字符串加工机器,我把&*he@¥#ll%&o……¥%
丢进去,结果出来的是hello
,这时候的数据才是用户真正想要的。
pipeline
就相当于这里的“加工机器”,它起到的是一个加工数据的作用。
(2)定义文本抽取管道
我们需要在kibana
控制台中,创建一个名为"attachment"
的预定义管道。
Kibana 是一款免费且开放的前端应用程序, 可以为 Elasticsearch 中索引的数据提供搜索和数据可视化功能。
(此处不提供 Kibana 安装教程)
在"attachment"
中指定要过滤的字段为content
,所以写⼊elasticsearch
时需要将⽂档内容放在content
字段。
PUT /_ingest/pipeline/attachment
{
"description": "提取附件信息",
"processors": [{
"attachment": {
"field": "content",
"ignore_missing": true
}
},
{
"remove": {
"field": "content"
}
}
]
}
注意!!!
定义好管道之后,我们只需把文档文件转化为Base64
格式,并把它丢到content
字段上,文本抽取管道会自动帮我们把文件内容进行加工,把经过IK分词器分词后的纯文本结果存储到content
字段上!
接下来,我们可以开始创建索引,并在索引中定义这个content
字段了。
1.3 创建索引
(1)创建的索引结构
id
:标识唯一记录userId
:文件所属用户id,根据需求添加。docId
:文件id,根据需求添加。docName
:文件名称,使用了ik_max_word
中文分词器(把中⽂尽可能的拆分)docType
:文件类型,根据需求添加。content
:关键!! 用于存储文件的base64
内容,使用了ik_smart
中文分词器(按常⽤习惯划分)
PUT /docwrite
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"userId": {
"type": "keyword"
},
"docId":{
"type": "keyword"
},
"docName": {
"type": "text",
"analyzer": "ik_max_word"
},
"docType": {
"type": "keyword"
},
"attachment": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
}
二、在 Kibana 中测试添加文档
2.1 先把文件转为Base64形式
找一个Base64在线转换网站,把某个文档文件转换成base64
字符串。
或者可以直接用我下面给出的Base64
内容。
这是我自己创建的一个word
文档,里面的内容是从elasticsearch
官网里抄来的。把这个文档转化为Base64
后的结果是:
UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC0lMtuwjAQRfeV+g+Rt1Vi6KKqKgKLPpYtUukHGHsCVv2Sx7z+vhMCUVUBkQpsIiUz994zVsaD0dqabAkRtXcl6xc9loGTXmk3K9nX5C1/ZBkm4ZQw3kHJNoBsNLy9GUw2ATAjtcOSzVMKT5yjnIMVWPgAjiqVj1Ykeo0zHoT8FjPg973e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AAAAAAAAAAAAAABaGQAAd29yZC9zZXR0aW5ncy54bWxQSwECLQAUAAYACAAAACEA8IJeJnkLAAAEcgAADwAAAAAAAAAAAAAAAABXHgAAd29yZC9zdHlsZXMueG1sUEsBAi0AFAAGAAgAAAAhAAIPzDrvAQAARwgAABQAAAAAAAAAAAAAAAAA/SkAAHdvcmQvd2ViU2V0dGluZ3MueG1sUEsBAi0AFAAGAAgAAAAhAKMhTSYeAgAAkwYAABIAAAAAAAAAAAAAAAAAHiwAAHdvcmQvZm9udFRhYmxlLnhtbFBLAQItABQABgAIAAAAIQC1jdXwagEAAN0CAAARAAAAAAAAAAAAAAAAAGwuAABkb2NQcm9wcy9jb3JlLnhtbFBLAQItABQABgAIAAAAIQBzhJgEdwEAAMsCAAAQAAAAAAAAAAAAAAAAAA0xAABkb2NQcm9wcy9hcHAueG1sUEsFBgAAAAALAAsAwQIAALozAAAAAA==
2.2 向ES中添加一条记录
使用kibana
控制台添加一条记录,把上面得到的Base64
内容粘贴到content
字段上(注意要加双引号),
POST /docwrite/_doc?pipeline=attachment
{
"userId": 1001,
"docId": 10003,
"docName": "es.docx",
"docType": "docx",
"content": "[此处放Base64内容]"
}
通过以下查询语句,检查记录中的content
字段是否已被文本抽取管道处理过。
GET /docwrite/_search
可以发现,content
字段已经被IK分词器进行分词存储了。
2.3 测试关键词高亮搜索
我们的最终目的,还是需要通过搜索关键词,把匹配到的文档信息显示出来。
这里就需要用到关键词的高亮搜索。
比如,如果我想搜索关键词“Elasticsearch”,那么可以执行以下语句:
GET /docwrite/_search
{
"query": {
"match": {
"attachment.content": {
"query": "Elasticsearch",
"analyzer": "ik_smart"
}
}
},
"highlight": {
"fields": {
"attachment.content": {
"pre_tags": "<strong>",
"post_tags": "</strong>"
}
}
}
}
这样就能够搜索到相关的记录,在该记录的 “highlight” 字段中,就显示出了和关键词匹配的文本内容,其中关键字是使用了<strong>
标签进行高亮显示。
这就好比我们平时在百度中搜索一个关键词,然后出现和关键词相关的文本内容,而且关键字会进行高亮显示(比如设置为红字)。
三、SpringBoot 实现
如果以上步骤能流畅的走完,SpringBoot
后端的实现就变得很容易了。
3.1 elasticsearch配置
(1)pom.xml
添加elasticsearch
和IOUtils
依赖
<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- IOUtils -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
(2)application.yml
添加elasticsearch
服务器地址、端口号
# 自定义参数
my-config:
# elasticsearch自定义配置
elasticsearch:
url: localhost
port: 9200
(3)ElasticSearchConfig 类
@Configuration
@Slf4j
public class ElasticSearchConfig {
@Value("${my-config.elasticsearch.url}")
private String esHost;
@Value("${my-config.elasticsearch.port}")
private int esPort;
/**
* 获取ES操作对象,注入bean中
* @return ES client对象
*/
@Bean("myESClient")
public RestHighLevelClient myElasticsearchClient() {
return new RestHighLevelClient(RestClient.builder(
new HttpHost(esHost, esPort, "http")
));
}
}
(4)elasticsearch 工具类
@Component
@Slf4j
public class ElasticSearchClient {
@Autowired
@Qualifier("myESClient")
private RestHighLevelClient restHighLevelClient;
/**
* 获得关键词搜索结果
* @param index
* @param sourceBuilder
* @return
*/
public SearchHit[] selectDocumentList(String index, SearchSourceBuilder sourceBuilder) {
try {
SearchRequest request = new SearchRequest(index);
if (sourceBuilder != null) {
// 返回实际命中数
sourceBuilder.trackTotalHits(true);
request.source(sourceBuilder);
}
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
if (response.getHits() != null) {
return response.getHits().getHits();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 插入/修改文档信息
* @param index 索引
* @param data 数据
*/
public void insertDocument(String index, Object data) {
try {
String id = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
IndexRequest request = new IndexRequest(index);
request.timeout(TIME_VALUE_SECONDS);
request.id(id);
// 重要!!必须设置管道
request.setPipeline("attachment");
request.source(JSON.toJSONString(data), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
log.debug("[es] 插入文档的响应状态: status:{},id:{}", response.status().getStatus(), response.getId());
String status = response.status().toString();
if ("CREATED".equals(status) || "OK".equals(status)) {
log.debug("[es] 插入文档成功! ");
return true;
}
} catch (Exception e) {
e.printStackTrace();
log.error("[es] 插入文档失败");
}
return false;
}
}
3.2 DocumentObj 实体类
用于记录文档文件的某些参数
@Data
public class DocumentObj implements Serializable{
/** 当前文件所属用户id */
private Long userId;
/** mysql中的文件id */
private Long docId;
/** 文件名字 */
private String docName;
/** 文件类型 */
private String docType;
/** 文件的base64内容 */
private String content;
private static final long serialVersionUID = 1L;
public DocumentObj() {}
}
3.3 Service 接口
public interface ISearchService {
/**
* (测试)根据关键词,搜索文档
* @param keyword
* @return
*/
List<DocumentObj> testSearch(String keyword);
/**
* (测试)把本地文档加载到elasticsearch中
*/
boolean testLoadDocument();
}
3.4 ServiceImpl 实现类
@Slf4j
@Service
public class SearchServiceImpl implements ISearchService {
@Autowired
private ElasticSearchClient esClient;
@Override
public List<DocumentObj> testSearch(String keyword) {
// 高亮查询,关键词添加红色样式
HighlightBuilder highlightBuilder = new HighlightBuilder()
.field("attachment.content")
.preTags("<font color='red' font-weight='bold'>")
.postTags("</font>");
// 普通全索引查询
SearchSourceBuilder searchSourceBuilder =
new SearchSourceBuilder()
.query(QueryBuilders.matchQuery("attachment.content", keyword).analyzer("ik_smart"))
.highlighter(highlightBuilder);
SearchHit[] searchHits = esClient.selectDocumentList("docwrite", searchSourceBuilder);
// 处理每一条记录(每一个文档),获得高亮文本。
List<DocumentObj> results = new ArrayList<>();
for (SearchHit hit : searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
DocumentObj obj = new DocumentObj();
obj.setDocId( ((Integer) sourceAsMap.get("docId")).longValue() );
obj.setDocName( (String) sourceAsMap.get("docName") );
HighlightField contentHighlightField = hit.getHighlightFields().get("attachment.content");
// 对于一个文档,它的高亮文本有多个结果,这里只拼接前2个结果。
String highLightMessage = contentHighlightField.fragments()[0].toString()
+ " " + contentHighlightField.fragments()[1].toString();
obj.setContent(highLightMessage);
results.add(obj);
}
return results;
}
@Override
public boolean testLoadDocument() {
// 用本地文档进行测试
try {
// 加载文件,得到base64
File file = new File("D:\\桌面文件\\es介绍.docx");
InputStream fileInputStream = new FileInputStream(file);
byte[] bytes = IOUtils.toByteArray(fileInputStream);
String base64 = Base64.getEncoder().encodeToString(bytes);
// 向es添加文档
DocumentObj obj = new DocumentObj();
obj.setUserId(1001L);
obj.setDocId(666L);
obj.setDocName("es介绍.docx");
obj.setDocType("docx");
obj.setContent(base64);
return esClient.insertDocument("docwrite", obj);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
3.5 Controller 层
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/es/search")
public ResponseEntity<?> testSearch(String keyword) {
return ResponseEntity.ok( searchService.testSearch(keyword) );
}
@RequestMapping("/es/addone")
public ResponseEntity<?> testAddone() {
return ResponseEntity.ok( searchService.testLoadDocument() );
}
}
3.6 测试
(1)加载文件
http://localhost:8002/test/es/addone
(2)关键字查询
搜索的关键词是 “Elasticsearch”。
http://localhost:8002/test/es/search?keyword=Elasticsearch
前端把特定的关键词传入接口,接口就会从elasticsearch
服务器中得到对应的记录。