elasticsearchingest-attachment对于word、pdf等⽂件内容的索引
【转】
【转】
1.简介
ElasticSearch只能处理⽂本,不能直接处理⽂档。要实现 ElasticSearch 的附件导⼊需要以下两个步骤:
⼀、对多种主流格式的⽂档进⾏⽂本抽取。
⼆、将抽取出来的⽂本内容导⼊ ElasticSearch。
Ingest-Attachment是⼀个开箱即⽤的插件,替代了较早版本的Mapper-Attachment插件,使⽤它可以实现对(PDF,DOC等)主流格式⽂件的⽂本抽取及⾃动导⼊。
Elasticsearch5.x 新增⼀个新的特性 Ingest Node,此功能⽀持定义命名处理器管道 pipeline,pipeline中可以定义多个处理器,在数据插⼊ ElasticSearch 之前进⾏预处理。⽽ Ingest Attachment Processor Plu
python入门教程非常详细wordgin 提供了关键的预处理器 attachment,⽀持⾃动对⼊库⽂档的指定字段作为⽂档⽂件进⾏⽂本抽取,并将抽取后得到的⽂本内容和相关元数据加⼊原始⼊库⽂档。
由于 ElasticSearch 是基于 JSON 格式的⽂档数据库,所以附件⽂档在插⼊ ElasticSearch 之前必须进⾏ Base64 编码。
以下使⽤ REST API 调⽤⽅式。
2.环境
ElasticSearch 5.3.0
ElasticSearch-head-master插件(安装指路 Elasticsearch学习--elasticsearch-head插件安装)
Ingest-attachment插件(官⽅介绍 Ingest Attachment Processor Plugin)
Cygwin  (curl+perl)
3.实现步骤
3.1 建⽴⾃⼰的⽂本抽取管道pipeline
curl -X PUT "localhost:9200/_ingest/pipeline/attachment" -d '{
"description" : "Extract attachment information",
"processors":[
{
"attachment":{
"field":"data",
spring festival christmas"indexed_chars" : -1,
"ignore_missing":true
}
},
{
"remove":{"field":"data"}
}]}'
3.2 创建新的索引
此处索引名为estest。
curl -X PUT “localhost:9200/estest” -d’{
“settings”:{
“index”:{
“number_of_shards”:1,
“number_of_replicas”:0
}}}’
3.3 载⼊数据
⽅法⼀:直接载⼊base64源码
⾸先要确定base64编码正确,否则因为乱码可能⽆法正确⽣成attachment。
这⾥: index-pdftest type-pdf id-1 皆为⾃定义javabean又叫
载⼊结果显⽰:(这⼀版data数据尚未删除)
⽅法⼆:载⼊PDF的同时进⾏转码导⼊
⾸先跳转⾄指定⽂件⽬录
这⾥我的⽂件ABC.pdf放在⽬录D:\ElasticSearch\File下
cd D:/ElasticSearch/File
使⽤perl脚本的解码功能:“’base64 -w 0 ABC.pdf | perl -pe's/\n/\\n/g'‘” 完整代码:
curl -X PUT "localhost:9200/pdftest/pdf/1?pipeline=attachment" -d '
java的概念{
"data":"QmFzZTY057yW56CB6K+05piOCuOAgOOAgEJhc2U2NOe8lueggeimgeaxguaKijPkuKo45L2N5a2X6IqC77yIMyo4PTI077yJ6L2s5YyW5Li6NOS4q }'curl -X PUT "localhost:9200/estest/pdf/10?pipeline=attachment" -d '表示颜的英语单词
{
"data":" '`base64 -w 0 ABC.pdf | perl -pe's/\n/\\n/g'`' "
}'
结果如图,可以导⼊成功:
全⽂索引,查询指定字段
注意查询字段名称,这个真的纠结了我太久……
查询结果:
total=1 意为到⼀个,由此验证字段可查询。
curl -X POST "localhost:9200/pdftest/pdf/_search?pretty" -d '{
"query ":{
"match ":{
"t ":"编码"
}}}'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0446626,
"hits" : [
{
"_index" : "pdftest",
"_type" : "pdf",
"_id" : "6",
"_score" : 1.0446626,
"_source" : {
"data" : "QmFzZTY057yW56CB6K+05piOCuOAgOOAgEJhc2U2NOe8lueggeimgeaxguaKijPkuKo45L2N5a2X6IqC77yIMyo4PTI077yJ6L2s5YyW5Li6N          "attachment" : {
"content_type" : "text/plain; charset=UTF-8",
"language" : "lt",
"content" : "Base64编码说明\n  Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前⾯补两个0,形成8位⼀            "content_length" : 212 }
}
}
]
}
}
5.参考⽬录
convert the file into base64 in elasticsearch for attachment
ElasticSearch 全⽂检索实战
elasticsearch使⽤附件进⾏中⽂检索,⽆法查询中⽂的问题
Getting error while parsing documents
Sending Attachments: Unexpected end-of-input in VALUE_STRING
How to index a pdf file in Elasticsearch 5.0.0 with ingest-attachment plugin?
6.想法
其实流程⾛下来还挺简单,但当时⼩⽩⼊门,我还是困扰了挺久的,查阅各路资料最终成功导⼊。虽然也不厉害,但还是记录⼀下,缕清整
switch用法例题
个流程,希望能帮到⼤家 。^_^
⼀、概述
Elasticsearch 是⼀个分布式的 RESTful 风格的搜索和数据分析引擎,它基于 Lucene 实现了强⼤的全⽂检索功能。本⽂针对⼀个通⽤的
应⽤场景,讲解如何利⽤ ElasticSearch 快速实现对关系型数据库⽂本和常见⽂档格式附件的全⽂检索。
⼆、应⽤场景
描述
数字图书馆有⼀套基于 MySQL 的电⼦书管理系统,电⼦书的基本信息保存在数据库表中,书的数字内容以多种常见的⽂档格式(PDF、
Word、PPT、RTF、TXT、CHM、EPUB等)保存在存储系统中。现在需要利⽤ ElasticSearch 实现⼀套全⽂检索系统,以便⽤户可以通
过对电⼦书的基本信息和数字内容进⾏模糊查询,快速到相关书籍。
数据结构
数据库表 BOOK 结构:
CREATE TABLE book (
id varchar(100) NOT NULL,
title varchar(50) DEFAULT NULL,
desc varchar(1000) DEFAULT NULL,
path varchar(200) DEFAULT NULL,
create_time datetime DEFAULT NULL,
update_time datetime DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
字段 意义
id 主键
title 书名
desc 介绍
path 存储路径
create_time 创建时间
update_time 更新时间
逻辑约束:创建书籍记录时,create_time 等于 update_time,即当前时间,每次更新书籍时,更新 update_time 时间。全⽂检索系统根据 update_time 时间更新书籍索引。
三、技术⽅案
⽰意图
基本思路就是:
1. 定期扫描 MySQL 中的 book 表,根据字段 update_time 批量抓取最新的电⼦书数据。
2. 从 path 字段获取电⼦书数字内容的⽂档存储路径。从存储系统中抓取电⼦书⽂档并进⾏ BASE64编码。
3. 将从 book 表批量抓取的数据转换为 JSON ⽂档,并将 BASE64编码后的电⼦书⽂档合并⼊ JSON,⼀同写⼊ ElasticSearch,利
⽤ ElasticSearch 的插件 Ingest Attachment Processor Plugin 对电⼦书⽂档进⾏⽂本抽取,并进⾏持久化,建⽴全⽂索引。
本⽂采⽤开源数据处理⼯具 Apache NiFi 来实现上述流程,具体使⽤⽅法后续实施过程会详细讲解。如果读者不了解 Apache NiFi ,也可以使⽤ Logstash、Kettle 等⼯具或者使⽤⾃⼰熟悉的编程语⾔开发应⽤来完成上述流程。
Linux/Unix 下运⾏ bin/elasticsearch (在windows操作系统下运⾏ bin\elasticsearch.bat )
ElasticSearch的默认服务端⼝是 9200,所有 API 都可以通过 REST ⽅式调⽤。
关于JVM内存:ElasticSearch是基于Java开发,部署需要配置合理的JVM Heap内存,官⽅建议分配内存不⾼于本机物理内存的⼆分之⼀,最好不要超过32G。具体配置⽅法如下: