Java实现Elasticsearch对word/pdf文件创建索引

realnode.png vps-under-title.png 科学上网教程 | 机场推荐 | 云深不知处(机场)

elasticsearch.jpg

环境

  • Elasticsearch 7.1.1;
  • ingest-attachment-7.1.1;
  • CENTOS 7;

这里记录了使用Java将上传附件索引到Elasticsearch的实现方式。

前提

对文档的处理需借助Elastcisearch ingest-attachment插件,准备工作详见下列文章:

实现

 public String indexAttachmentToElasticSearch(String fileFullPath) {
        String result = "error";
        InputStream inputStream;
        IndexRequest request;
        try {
            inputStream = new FileInputStream(new File(fileFullPath));
            byte[] fileByteStream = IOUtils.toByteArray(inputStream);
            String base64String = new String(Base64.getEncoder().encodeToString(fileByteStream).getBytes(), "UTF-8");
            inputStream.close();
            Map attachmentMap = new HashMap();
            attachmentMap.put("data", base64String);
            attachmentMap.put("fileName", "四个空格-https://www.4spaces.org");
            String jsonString = JSONObject.toJSONString(attachmentMap);
            request = new IndexRequest("data_archives_attachment");
            request.id(UUID.randomUUID().toString());
            request.setPipeline("single_attachment");
            request.source(jsonString, XContentType.JSON);
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            result = response.status().toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ElasticsearchException e) {
            e.getDetailedMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

上面代码中,single_attachment为管道流名称,data_archives_attachment为索引名称。

具体实现是借助Elastcisearch ingest-attachment插件处理BASE64字符串,文件转为BASE64字符串的过程为:

inputStream = new FileInputStream(new File(fileFullPath));
byte[] fileByteStream = IOUtils.toByteArray(inputStream);
String base64String = new String(Base64.getEncoder().encodeToString(fileByteStream).getBytes(), "UTF-8");

完整代码示例:https://github.com/aitlp/elastic

赞(0)
未经允许禁止转载:优米格 » Java实现Elasticsearch对word/pdf文件创建索引

评论 1

  1. #1

    这样的话数据库里的结构为{“attachment”:{“content”:”xxx”}},
    要怎么才能变成{“content”:”xxx”},从而使结构保持一致?

    poi3年前 (2020-10-13)回复

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册

Ads Blocker Image Powered by Code Help Pro

检测到广告已被屏蔽

尊敬的优米格用户,

您好,系统检测到您使用了广告屏蔽组件。本网站并不包含任何侵入性广告,如果本站能给您一点帮助,还请将本站加入拦截白名单,这是我们仅有的收入来源!

网站的持续运作和改进,需要您的慷概帮助,感谢您的支持。

此致,
优米格 团队