我正在尝试使用Java代码在elastic search 6.3.2中索引PDF文件。到目前为止,我已经编写了以下代码在ES中保存pdf。代码运行良好,我能够保存我的Base64编码的字符串在ES中。我想知道我所遵循的方法是正确的还是错误的?有没有更好的方法呢?以下是我的代码:
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();
}
基本上,我在这里做的是,我正在转换的Base64String文件,并保存为ES,而阅读,我正在转换它回来。
解码代码如下:
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-01 04:28:46
在elasticsearch中存储BASE64内容可能是正确的,但这里可能缺少一些内容:
相反,我会提取文本和元数据,并将其与二进制文件本身的URL建立索引。像这样:
{
"content": "Extracted text here",
"meta": {
// Meta data there
},
"url": "file://path/to/file"
}
你也可以看看FSCrawler (包括它的代码),它基本上做到了这一点。
https://stackoverflow.com/questions/51620450
复制相似问题