当前位置:   article > 正文

Elasticsearch实战之处理邮件附件进行进行内容全文检索_es实现文件搜索 file附件

es实现文件搜索 file附件

目录

一、系统环境和软件要求

二、软件说明

三、定义文本抽取管道(pipeline)

四、建立索引设置文档结构映射

五、插入文档

六、查询文档


需求是将本地邮件内容以及PDF,EXCEL,WORD等附件内容进行处理,保存到ES数据库,实现邮件内容及附件内容的全文检索。

一、系统环境和软件要求

系统:CentOS7.3

elasticsearch版本:7.13.3

kibana版本:7.16.3

ingest-attachment插件版本:7.13.3

二、软件说明

Kibana是一个开源的分析和可视化平台,设计用于和Elasticsearch一起工作。当前我们的用途主要是在kibana的开发工具dev tools中执行一些命令。

Ingest-Attachment是一个开箱即用的插件。可以将常用格式的文件作为附件写入Index。ingest attachment插件通过使用Apache Tika来提取文件,支持的文件格式有TXT、DOC、PPT、XLS和PDF等。 可以进行文本抽取及自动导入。注意:源字段必须是base64编码的二进制。

缺点:在处理xls和xlsx格式的时候,无法将sheet分开索引,只能将整个文件当做一个文档插入es中。

三、安装插件

我这里采用离线方式安装Ingest-Attachment,通过wget方式直接下载跟elasticsearch版本相同的离线文件。

wget https://artifacts.elastic.co/downloads/elasticsearch-plugins/ingest-attachment/ingest-attachment-7.13.3.zip

上传到服务器 目录

/home/es/install/ingest-attachment-7.13.3.zip

进入ES_HOME的主目录,执行下面的命令进行安装

  1. cd /home/elasticsearch/
  2. ./bin/elasticsearch-plugin install file:///home/es/install/ingest-attachment-7.13.3.zip

安装完成后重启elasticsearch服务

插件安装完成!

三、定义文本抽取管道(pipeline)

在kibana的dev tool执行

我这里邮件可能是多个附件,所以定义文本抽取管道(多附件),我这里是设置 处理后移除base64的二进制数据。

需要注意的是,多附件的情况下,field和target_field必须要写成_ingest._value.*,否则不能匹配正确的字段。

  1. PUT _ingest/pipeline/multiple_attachment
  2. {
  3.     "description" : "Extract attachment information from arrays",
  4.     "processors" : [
  5.       {
  6.         "foreach" : {
  7.           "field" : "attachments",
  8.           "processor" : {
  9.             "attachment" : {
  10.               "target_field" : "_ingest._value.attachment",
  11.               "field" : "_ingest._value.content"
  12.             }
  13.           }
  14.         }
  15.       },
  16.       {
  17.         "foreach" : {
  18.           "field" : "attachments",
  19.           "processor" : {
  20.             "remove" : {
  21.               "field" : "_ingest._value.content"
  22.             }
  23.           }
  24.         }
  25.       }
  26.     ]
  27. }

插件ingest attachment的pipeline参数含义

Name是否必须DefaultDescription
fieldyes-从这个字段中获取base64编码
target_fieldnoattachment用于保留attachment信息,主要用于多附件的情况
indexed_charsno100000限制字段的最大保存字符数。-1为无限制。
indexed_chars_fieldno-可以从数据中设定的字段取到indexed_chars限制的值。
propertiesno全属性选择需要存储的属性。例如 contenttitlenameauthorkeywordsdatecontent_typecontent_lengthlanguage
ignore_missingnoFALSE如果使用true,并且 field 不存在, 则会忽略附件直接写入doc;否则则会报错。

四、建立索引设置文档结构映射

  1. PUT mail
  2. {
  3. "settings": {
  4. "index": {
  5. "max_result_window": 100000000
  6. },
  7. "number_of_shards": 3,
  8. "number_of_replicas": 0
  9. },
  10. "mappings": {
  11. "properties": {
  12. "mfrom": {
  13. "type": "keyword"
  14. },
  15. "mto": {
  16. "type": "keyword"
  17. },
  18. "mcc": {
  19. "type": "keyword"
  20. },
  21. "mbcc": {
  22. "type": "keyword"
  23. },
  24. "rcvtime": {
  25. "type": "date",
  26. "format": "yyyy-MM-dd HH:mm:ss"
  27. },
  28. "subject": {
  29. "type": "keyword"
  30. },
  31. "importance": {
  32. "type": "keyword"
  33. },
  34. "savepath": {
  35. "type": "keyword"
  36. },
  37. "mbody": {
  38. "type": "text",
  39. "fields": {
  40. "keyword": {
  41. "ignore_above": 256,
  42. "type": "keyword"
  43. }
  44. }
  45. },
  46. "attachments": {
  47. "properties": {
  48. "attachment": {
  49. "properties": {
  50. "content": {
  51. "type": "text",
  52. "fields": {
  53. "keyword": {
  54. "ignore_above": 256,
  55. "type": "keyword"
  56. }
  57. }
  58. },
  59. "filename": {
  60. "type": "keyword"
  61. },
  62. "type": {
  63. "type": "keyword"
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }

创建成功会返回

  1. {
  2. "acknowledged" : true,
  3. "shards_acknowledged" : true,
  4. "index" : "mail"
  5. }

五、插入文档

可以使用Postman来调用elasticsearch的rest full接口完成文档插入或者更新。

请求类型:POST
请求地址:http://192.168.31.200:9200/mail/_doc?pipeline=multiple_attachment

请求地址中mail是索引名,pipeline=multiple_attachment指定需要使用的管道(pipeline)是multiple_attachment

请求body内容是JSON格式:

  1. {
  2. "mfrom": "microsoft.teams@outlook.com",
  3. "mto": "network@163.com",
  4. "mcc": "",
  5. "mbcc": "",
  6. "rcvtime": "2023-05-18 23:35:29",
  7. "subject": "神奇的邮件2023066- ",
  8. "importance": "1",
  9. "savepath": "d:\\mail\\TEST123.eml",
  10. "mbody": "这是邮件内容",
  11. "attachments": [
  12. {
  13. "filename": "附件名字1.pdf",
  14. "type": ".pdf",
  15. "content": "5oiR54ix5L2g5Lit5Zu9MjAyMw=="
  16. },
  17. {
  18. "filename": "附件名字2.xlsx",
  19. "type": ".xlsx",
  20. "content": "Q2hhdEdQVCDniZvpgLwh"
  21. }
  22. ]
  23. }

attachments是JSON数组,里面放2个附件的信息。filename是附件名字,content是附件解析出来的base64编码字符串。插入时通过管道处理,会自动识别内容,剩下的跟操作普通的索引一样。

下面是执行成功返回的内容:

  1. {
  2. "_index": "mail",
  3. "_type": "_doc",
  4. "_id": "eiCNNIgBUc2qXUv978Tg",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 1,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "_seq_no": 0,
  13. "_primary_term": 1
  14. }

Postman截图

六、查询文档

6.1 根据_id查看文档

GET请求地址 http://192.168.31.200:9200/mail/_doc/eiCNNIgBUc2qXUv978Tg

参数和内容无

其中eiCNNIgBUc2qXUv978Tg为文档_id,mail为需要查询的索引名

返回结果:

  1. {
  2. "_index": "mail",
  3. "_type": "_doc",
  4. "_id": "eiCNNIgBUc2qXUv978Tg",
  5. "_version": 1,
  6. "_seq_no": 0,
  7. "_primary_term": 1,
  8. "found": true,
  9. "_source": {
  10. "savepath": "d:\\mail\\TEST123.eml",
  11. "mbody": "这是邮件内容",
  12. "attachments": [
  13. {
  14. "filename": "附件名字1.pdf",
  15. "attachment": {
  16. "content_type": "text/plain; charset=UTF-8",
  17. "language": "lt",
  18. "content": "我爱你中国2023",
  19. "content_length": 10
  20. },
  21. "type": ".pdf"
  22. },
  23. {
  24. "filename": "附件名字2.xlsx",
  25. "attachment": {
  26. "content_type": "text/plain; charset=UTF-8",
  27. "language": "lt",
  28. "content": "ChatGPT 牛逼!",
  29. "content_length": 12
  30. },
  31. "type": ".pdf"
  32. }
  33. ],
  34. "mbcc": "",
  35. "subject": "神奇的邮件2023066- ",
  36. "importance": "1",
  37. "mfrom": "microsoft.teams@outlook.com",
  38. "mto": "network@163.com",
  39. "mcc": "",
  40. "rcvtime": "2023-05-18 23:35:29"
  41. }
  42. }

Postman截图

6.2 模糊查询附件名字

Post请求地址 ​http://192.168.31.200:9200/mail/_search

​请求内容是JSON字符串,attachments.filename.keyword是附件名字(不分词)

  1. {
  2. "query": {
  3. "bool": {
  4. "should": [{
  5. "wildcard": {
  6. "attachments.filename.keyword": "*附件*"
  7. }
  8. }]
  9. }
  10. }
  11. }

返回结果

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "mail",
  19. "_type": "_doc",
  20. "_id": "eiCNNIgBUc2qXUv978Tg",
  21. "_score": 1.0,
  22. "_source": {
  23. "savepath": "d:\\mail\\TEST123.eml",
  24. "mbody": "这是邮件内容",
  25. "attachments": [
  26. {
  27. "filename": "附件名字1.pdf",
  28. "attachment": {
  29. "content_type": "text/plain; charset=UTF-8",
  30. "language": "lt",
  31. "content": "我爱你中国2023",
  32. "content_length": 10
  33. },
  34. "type": ".pdf"
  35. },
  36. {
  37. "filename": "附件名字2.xlsx",
  38. "attachment": {
  39. "content_type": "text/plain; charset=UTF-8",
  40. "language": "lt",
  41. "content": "ChatGPT 牛逼!",
  42. "content_length": 12
  43. },
  44. "type": ".pdf"
  45. }
  46. ],
  47. "mbcc": "",
  48. "subject": "神奇的邮件2023066- ",
  49. "importance": "1",
  50. "mfrom": "microsoft.teams@outlook.com",
  51. "mto": "network@163.com",
  52. "mcc": "",
  53. "rcvtime": "2023-05-18 23:35:29"
  54. }
  55. }
  56. ]
  57. }
  58. }

6.3 模糊查询附件内容

POST请求地址 http://192.168.31.200:9200/mail/_search

请求内容为JSON格式,attachments.attachment.content是附件内容(不加密)

  1. {
  2. "size":"10000",
  3. "_source" :[
  4. "_id",
  5. "seqnbr",
  6. "subject",
  7. "eml"
  8. ],
  9. "query": {
  10. "match": {
  11. "attachments.attachment.content":"*ChatGPT*"
  12. }
  13. }
  14. }

返回结果

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.2876821,
  16. "hits": [
  17. {
  18. "_index": "mail",
  19. "_type": "_doc",
  20. "_id": "eiCNNIgBUc2qXUv978Tg",
  21. "_score": 0.2876821,
  22. "_source": {
  23. "subject": "神奇的邮件2023066- "
  24. }
  25. }
  26. ]
  27. }
  28. }

七、其他说明


下面是单独说明的定义文本抽取的管道single_attachment

在kibana的dev tool执行

PUT _ingest/pipeline/single_attachment

  1. {
  2.   "description" : "Extract single attachment information",
  3.   "processors" : [
  4.     {
  5.       "attachment" : {
  6.         "field": "data",
  7.         "indexed_chars" : -1,
  8.         "ignore_missing" : true
  9.       }
  10.     }
  11.   ]
  12. }

剩下的就是代码集成的问题了。关于中文分词IK插件的使用,后期需要再详细说明。

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

闽ICP备14008679号