Java code index PDF files ElasticSearch in?

The following is my code:

            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();
        }

The following are decoded code:

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();

Users answer answer to 2018-08-02

Extract text and metadata, and the URL points to the binary file itself.

{
  "content": "Extracted text here",
  "meta": {
    // Meta data there
  },
  "url": "file://path/to/file"
}
Published 298 original articles · won praise 107 · Views 140,000 +

Guess you like

Origin blog.csdn.net/ywl470812087/article/details/104875188