实战 | Elasticsearch打造知识库检索系统

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
简介: 本文是用ES做个类似于知识库,索引一些pdf、word之类的文件的一些思考。

题记

源自“死磕Elasticsearch”技术群里的讨论问题:
——我想用es做个类似于知识库的东西,所以需要索引一些pdf、word之类的文件,这个你之前有试过吗?能给个方向吗?

我的思考如下:

1、pdf、Office类的文档如何被ES索引?

更确切的说,pdf、Office类文档(word,ppt,excel等)如何导入ES中。
如图所示:

image.png

问题转嫁为:如何将Office类文档、PDF文档导入ES建立索引,并提供全文检索服务?

2、Elasticsearch支持的最大待检索字段的长度是多大?

ES5.X版本以后,keyword支持的最大长度为32766个UTF-8字符,text对字符长度没有限制。
设置ignore_above后,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。
参考我的整理:http://blog.csdn.net/laoyang360/article/details/78207980
参考6.0官网解读:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/ignore-above.html
参考luncene7.1API: http://t.cn/RYWvuGl

3、Office&pdf文档存入Elastisearch注意问题清单

少废话,直接上图。

image.png

4、解析实战代码

/*
**@brief:不同类型的文档解析
**@param:文件路径
**@return:解析结果
*/
public static ImExtInfo readXFile(String inputPath){
String fileExt = FileUtil.getFileExtension(file);
switch(fileExt){
case "pdf":
imExtInfo = readPdfFile(inputPath);
break;
case "docx":
imExtInfo = readDocxFile(inputPath);
break;
case "doc":
imExtInfo = readDocFile(inputPath);
break;
case "json":
case "txt":
case "md":
imExtInfo = readTxtFile(inputPath);
break;
default:
logger.info("unknow type " + fileExt);
imExtInfo = null;
}

/*
**@brief:docx文档解析
**@param:文件路径
**@return:解析结果
*/
public static ImExtInfo readDocxFile(String inputPath) {
ImExtInfo  ImExtInfo = new ImageExtInfo();
try {
File file = new File(inputPath.trim());

//1.获取文件名称
String fileTitle = file.getName();

//2.获取绝对路径
String filePath = file.getAbsolutePath();

//3.获取文件内容
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
StringBuilder contentBuilder = new StringBuilder();
//System.out.println("Total no of paragraph "+paragraphs.size());
for (XWPFParagraph para : paragraphs) {
//System.out.println(para.getText());
contentBuilder.append(para.getText().trim());
}

String content = contentBuilder.toString();
imExtInfo.setTitle(fileTitle);
imExtInfo.setPath(filePath);
imExtInfo.setContent(content.trim());
// 获取当前时间...
String curTimeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
imExtInfo.setCreate_time(curTimeStamp);

document.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return imExtInfo;
}

以上仅列举核心示例代码。

5、小结

从功能和性能角度考量,建立知识库的建议如下:
1)知识库的核心是数据导入ES,导入ES的核心是各种类型文档的解析;
2)提前设定Mapping,定义好字段分词、不分词的策略;
3)对于大于1MB一个字段的存储,建议使用fvh高亮方式,在Mapping中一并设置。

参考:

[1] Java读取Office文档参考:http://poi.apache.org/document/
[2] Html2Md参考:https://github.com/pnikosis/jHTML2Md
[3] Pdf2Html参考:https://github.com/coolwanglu/pdf2htmlEX
[4]OpenOffice参考:https://www.openoffice.org/download/index.html

——————————————————————————————————

作者:铭毅天下
转载请标明出处,原文地址:
http://blog.csdn.net/laoyang360/article/details/78703177

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
15小时前
|
存储 索引
Elasticsearch索引之嵌套类型:深度剖析与实战应用
Elasticsearch索引之嵌套类型:深度剖析与实战应用
4 0
|
14天前
|
存储 JSON 搜索推荐
Springboot2.x整合ElasticSearch7.x实战(三)
Springboot2.x整合ElasticSearch7.x实战(三)
15 0
|
14天前
|
存储 自然语言处理 关系型数据库
Springboot2.x整合ElasticSearch7.x实战(二)
Springboot2.x整合ElasticSearch7.x实战(二)
14 0
|
14天前
|
搜索推荐 数据可视化 Java
Springboot2.x整合ElasticSearch7.x实战(一)
Springboot2.x整合ElasticSearch7.x实战(一)
13 0
|
1月前
|
人工智能 自然语言处理 开发者
Langchain 与 Elasticsearch:创新数据检索的融合实战
Langchain 与 Elasticsearch:创新数据检索的融合实战
59 10
|
1月前
|
自然语言处理 测试技术 网络安全
ElasticSearch7最新实战文档-附带logstash同步方案
ElasticSearch7最新实战文档-附带logstash同步方案
25 0
|
1月前
|
canal 自然语言处理 关系型数据库
Elasticsearch 线上实战问题及解决方案探讨
Elasticsearch 线上实战问题及解决方案探讨
26 0
|
1月前
|
Java Maven 开发工具
【ElasticSearch 】IK 分词器安装
【ElasticSearch 】IK 分词器安装
44 1
|
14天前
Elasticsearch安装配置文件
Elasticsearch安装配置文件
15 0
|
1月前
|
存储 数据可视化 数据挖掘
【ElasticSearch】ElasticSearch安装
【ElasticSearch】ElasticSearch安装
38 2