.net core 操作Ingest-Attachment插件

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

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

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

elasticsearch-plugin install ingest-attachment

卸载:

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

elasticsearch-plugin remove ingest-attachment 

使用ingest-attachment前先建立管道(全局执行一次即可使用)

使用kibana建立抽取管道

PUT _ingest/pipeline/file_attachment
{
  "description": "Extract attachment information",
    "processors": [
      {
        "attachment": {
          "field": "data",
          "indexed_chars": -1,
          "ignore_missing": true,
          "properties":["content","title","content_type"]
        }
      },
      {
        "remove": {
          "field": "data"
        }
      }
    ]
}
建立抽取管道

注意:file_attachment 为管道名称,可以随便命名,但必须跟代码对应,否则会出现创建索引失败

创建索引

PUT /idx_file
{
   "mappings": {
     "properties": {
     "addtime":{
       "type": "keyword"
     },
     "address":{
        "type": "text"
     },
     "adduser":{
        "type": "keyword"
     },
     "filename":{
        "type": "keyword"
     },
     "path":{
        "type": "text"
     },
     "title":{
        "type": "text"
     },
       "attachment": {
        "properties": {
          "data":{
            "type": "text",
            "analyzer": "ik_smart"
          }
        }
      }
     }
   }
}
创建索引

创建webapi程序,并添加NEST包。

向ES添加文件代码如下

    IndexDto dto = new IndexDto()
                {
                    addtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    adduser = "admin",
                    data = str,
                    filename = input.FileName,
                    id = input.Id,
                    path = input.Path,
                    title = input.Title,
                };
                 
                var response = await _elasticClient.IndexAsync(dto, x => x.Pipeline("file_attachment"));
View Code

注意:data为文件内容的BASE64编码,文件必须为UTF8编码格式,否则不能正常显示文件内容

添加文件后,查询结果

 

 

 

具体示例见附件

posted @ 2022-05-28 17:38  都市之夜  阅读(433)  评论(0编辑  收藏  举报