ElasticSearch7.X解析/存储/检索word、pdf文档

es 5.x以后提供Ingest-Attachment插件管道(Pipeline),可以解析上百种的office文件,将文档内容存储到es中。其解析能力配合es原有的全文检索能力配合上分词插件,是我们处理文档类型内容的一大利器。

Linux安装Ingest-Attachment插件
安装插件

进入到es安装目录下的bin目录
执行./elasticsearch-plugin install ingest-attachment
集群模式下需要对每个节点进行安装,安装成功后重启该es节点。
该方式需要我们的服务器可以连接互联网

建立Ingest-Attachment通道

以下操作为kibana中操作,且es版本为7.x 其他版本语法有略微差别

PUT _ingest/pipeline/simple_attachment//simple_attachment为管道名字
{
  "description": "单文件管道流",//备注
  "processors": [
    {
      "attachment": {
        "field": "data",//需要解析的数据key
        "properties": [//将文档解析出content内容title标题content_type类型
          "content",
          "title",
          "content_type"
        ],
        "ignore_missing": true//是否忽略fieId不存在的情况
      }
    },{
     "remove":{"field":"data"}
    }
  ]
}
创建一个存储该文档的索引
POST file_index_test/_doc
{
  "mappings": {
    "info": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "filename": {
          "type": "text",
          "analyzer": "jieba_index"
        },
        "attachment": {
          "properties": {
            "content": {
              "type": "text",
              "analyzer": "jieba_index"
            },
            "content_type": {
              "type": "text",
              "analyzer": "jieba_index"
            },
            "title": {
              "type": "text",
              "analyzer": "jieba_index"
            }
          }
        }
      }
    }
  }
}
文件输入

需要将文件base64编码输入到接口中

PUT file_index_test/_doc/1?pipeline=simple_attachment//simple_attachment为管道名字
{
  "id": "1",
  "filename": "测试附件",
  "data": ""//放入文件的base64码
}

查询改存储结果如下

结果
JAVA环境下存储方式

在JAVA环境下,仅需要在原有的存储过程中,配置上管道组件即可

IndexRequest indexRequest = new IndexRequest(indexName).id(id)
//配置管道组件simple_attachment
.setPipeline("simple_attachment")
.source(docMap);
indexRequest.opType(DocWriteRequest.OpType.CREATE);
//使用elasticsearch.client存储数据
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

你可能感兴趣的:(ElasticSearch7.X解析/存储/检索word、pdf文档)