当前位置:   article > 正文

ElasticSearch7.6.2 ingest-attachment 对于word,pdf等文件内容索引

ingest-attachment

请多多留言指教

ElasticSearch7.6.2服务器配置节点不在重复介绍,可查看文章。

Ingest-Attachment是一个开箱即用的插件,可以实现对(PDF,DOC等)主流格式文件的文本抽取及自动导入。

 

安装(可以手动下载插件包放入到es plugin目录下):

cmd 进入到elasticsearch bin目录下,执行以下命令,等待安装插件

elasticsearch-plugin install ingest-attachment

卸载:

cmd 进入到elasticsearch bin目录下,执行以下命令

elasticsearch-plugin remove ingest-attachment
 

kibana tool工具操作 使用ingest-attachment

建立ElasticSearch文件存储,用于检索文件名称、文件内容

1、建立文本抽取管道pipeline(全局执行一次即可使用)

  1. PUT _ingest/pipeline/attachment
  2. {
  3. "description": "Extract attachment information",
  4. "processors": [
  5. {
  6. "attachment": {
  7. "field": "data",
  8. "indexed_chars": -1,
  9. "ignore_missing": true
  10. }
  11. },
  12. {
  13. "remove": {
  14. "field": "data"
  15. }
  16. }
  17. ]
  18. }

2、建立索引filedata

属性列:文件名称,文件扩展名,文件路径,读取的文件内容

  1. PUT /filedata
  2. {
  3. "mappings": {
  4. "properties": {
  5. "filename": {
  6. "type": "text",
  7. "analyzer": "ik_max_word"
  8. },
  9. "fileext": {
  10. "type": "keyword"
  11. },
  12. "filepath": {
  13. "type": "keyword"
  14. },
  15. "attachment.data": {
  16. "type": "text",
  17. "analyzer": "ik_max_word"
  18. }
  19. }
  20. }
  21. }

3、kibana tool批量创建数据

  1. PUT /filedata/_bulk?pipeline=attachment&pretty=true
  2. {"index":{}}
  3. {"filename":"小黑","fileext":"txt","filepath":"d:/tempfile", "data":"5LiJ5aSp5LiN5omT5LiK5oi/5o+t55OmIOS9oOivtOeahOWvueS4jeWvuQ=="}
  4. {"index":{}}
  5. {"filename":"小白","fileext":"txt","filepath":"d:/tempfile","data":"5Lit5Y2O5Lq65ZCN5YWx5ZKM5Zu9IOaIkeeahOelluWbvQ=="}

存储的数据如下:

 

4、通过IK分词插件查询

term根据IK分词查询,highlight高亮显示,此查询为根据文件名称查询

  1. GET /filedata/_search
  2. {
  3. "query": {
  4. "term": {
  5. "filename": {
  6. "value": "小"
  7. }
  8. }
  9. },
  10. "highlight": {
  11. "fragment_size": 40,
  12. "fields": {
  13. "filename": { }
  14. }
  15. }
  16. }

查询结果如下图:

 

5、ingest-attachment 通过管道pipeline提取文本数据,根据文本内容查询

match根据属性查询,highlight高亮显示

  1. GET /filedata/_search
  2. {
  3. "query": {
  4. "match": {
  5. "attachment.content": "共和国"
  6. }
  7. },
  8. "highlight": {
  9. "fragment_size": 40,
  10. "fields": {
  11. "attachment.content": { }
  12. }
  13. }
  14. }

查询结果如下

6、Elasticsearch bool过滤查询,match和term联合查询

  1. GET /filedata/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "term": {
  8. "filename": {
  9. "value": "黑"
  10. }
  11. }
  12. },
  13. {
  14. "match": {
  15. "attachment.content": "共和国"
  16. }
  17. }
  18. ]
  19. }
  20. },
  21. "highlight": {
  22. "fragment_size": 100,
  23. "fields": {
  24. "attachment.content": { }
  25. }
  26. }
  27. }

查询结果如下

 

到此ingest-attachment插件,安装,应用已完成(ingest-attachment应用体现在管道pipeline提取文本数据)。

注:在使用时,需要将其文本数据转成base64的编码,使用管道将其base64编码放入es 即可,ingest-attachment 会自动从你添加的base64的编码中提取文本放入 attament.content 中。

 

其它:应用nodejs操作ES命令

nodejs中@elastic/elasticsearch 读取word、pdf等文件内容存储到ES,并对其文本索引检索

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/802342
推荐阅读
相关标签
  

闽ICP备14008679号