用Java代码在ElasticSearch中索引PDF文件?

≯℡__Kan透↙ 提交于 2020-03-17 01:08:31

以下是我的代码:

            InputStream inputStream = new FileInputStream(new File("mypdf.pdf"));
        try {
            byte[]  fileByteStream = IOUtils.toByteArray(inputStream );
            String base64String = new String(Base64.getEncoder().encodeToString(fileByteStream).getBytes(),"UTF-8");
            String strEncoded = Base64.getEncoder().encodeToString( base64String.getBytes( "utf-8" ));
            this.stream.close();

                    JSONObject correspondenceNode = new JSONObject(); 
                    correspondenceNode.put("data",strEncoded );

                    String strSsonValues = correspondenceNode.toString();
                    HttpEntity entity = new NStringEntity(strSsonValues , ContentType.APPLICATION_JSON);
                    elasticrestClient.put("/2018/documents/"1, entity);

        } catch (IOException e) {
            e.printStackTrace();
        }

以下是解码代码:

String responseBody = elasticrestClient.get("/2018/documents/1");
//some code to fetch the hits
JSONObject h = hitsArray.getJSONObject(0);
source = h.getJSONObject("_source");
String object = (source.getString("data"));
byte[] decodedStr = Base64.getDecoder().decode( object );

FileOutputStream fos = new FileOutputStream("download.pdf");
fos.write(Base64.getDecoder().decode(new String( decodedStr, "utf-8" )));
fos.close();

用户回答回答于 2018-08-02

提取文本和元数据,并将该URL指向二进制文件本身。

{
  "content": "Extracted text here",
  "meta": {
    // Meta data there
  },
  "url": "file://path/to/file"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!