Elasticsearch - 提取 PDF 内容并使用 base64 编码
Elasticsearch - Extracting PDF content and encoding with base64
我希望能够从 PDF 文件中提取内容并能够使用 ElasticSearch 在该内容中进行搜索。
我确实安装了elasticsearch/elasticsearch-mapper-attachments/2.6.0
我创建了一个名为 "docs" 的新索引。
我确实创建了一个名为 "tmp.json" 的文件,内容为:
{"title": "file.pdf", "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="}
我确实执行了以下操作:
curl -X PUT "http://localhost:9200/docs/attachment/_mapping" -d '{
"attachment": {
"properties" : {
'file" : {
"type" : "attachment",
"fields" : {
"title" : {"store":"yes"},
"file":{
"type":"string",
"term_vector":"with_positions_offsets",
"store":"yes"}
}
}
}
}
}'
及以下:
curl -X POST "http://localhost:9200/docs/attachment" -d @tmp.json
问题是内容按原样存储在文件中。
我期待内容被解码,就像这样:
base64.b64decode("IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg==")
这给出了:
b'"God Save the Queen" (alternatively "God Save the King"'
要在 base64 中编码,我是这样做的:
import json, base64
file64 = base64.b64encode(open('file.pdf', "rb").read()).decode('ascii')
f = open('tmp.json', 'w')
data = {"file":file64, "title":fname}
json.dump(data,f)
f.close()
我希望能够使用 kibana 查看内容(但目前我只能看到 base64 数据...)
这没有用:
curl -X PUT "http://localhost:9200/docs/attachment/_mapping" -d '{
"attachment": {
"properties" : {
"content" : {
"type" : "attachment",
"fields" : {
"title" : {"store":"yes"},
"content":{
"type":"string",
"term_vector":"with_positions_offsets",
"store":"yes"}
}
}
}
}
}'
这成功了,我可以通过 Kibana 查看 PDF 的内容:
curl -X PUT "http://localhost:9200/docs" -d '{
"mappings" : {
"attachment" : {
"properties" : {
"content" : {
"type" : "attachment",
"fields" : {
"content" : { "store" : "yes" },
"author" : { "store" : "yes" },
"title" : { "store" : "yes"},
"date" : { "store" : "yes" },
"keywords" : { "store" : "yes", "analyzer" : "keyword" },
"name" : { "store" : "yes" },
"content_length" : { "store" : "yes" },
"content_type" : { "store" : "yes" }
}
}
}
}
}
}'
我希望能够从 PDF 文件中提取内容并能够使用 ElasticSearch 在该内容中进行搜索。
我确实安装了elasticsearch/elasticsearch-mapper-attachments/2.6.0
我创建了一个名为 "docs" 的新索引。
我确实创建了一个名为 "tmp.json" 的文件,内容为:
{"title": "file.pdf", "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="}
我确实执行了以下操作:
curl -X PUT "http://localhost:9200/docs/attachment/_mapping" -d '{
"attachment": {
"properties" : {
'file" : {
"type" : "attachment",
"fields" : {
"title" : {"store":"yes"},
"file":{
"type":"string",
"term_vector":"with_positions_offsets",
"store":"yes"}
}
}
}
}
}'
及以下:
curl -X POST "http://localhost:9200/docs/attachment" -d @tmp.json
问题是内容按原样存储在文件中。
我期待内容被解码,就像这样:
base64.b64decode("IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg==")
这给出了:
b'"God Save the Queen" (alternatively "God Save the King"'
要在 base64 中编码,我是这样做的:
import json, base64
file64 = base64.b64encode(open('file.pdf', "rb").read()).decode('ascii')
f = open('tmp.json', 'w')
data = {"file":file64, "title":fname}
json.dump(data,f)
f.close()
我希望能够使用 kibana 查看内容(但目前我只能看到 base64 数据...)
这没有用:
curl -X PUT "http://localhost:9200/docs/attachment/_mapping" -d '{
"attachment": {
"properties" : {
"content" : {
"type" : "attachment",
"fields" : {
"title" : {"store":"yes"},
"content":{
"type":"string",
"term_vector":"with_positions_offsets",
"store":"yes"}
}
}
}
}
}'
这成功了,我可以通过 Kibana 查看 PDF 的内容:
curl -X PUT "http://localhost:9200/docs" -d '{
"mappings" : {
"attachment" : {
"properties" : {
"content" : {
"type" : "attachment",
"fields" : {
"content" : { "store" : "yes" },
"author" : { "store" : "yes" },
"title" : { "store" : "yes"},
"date" : { "store" : "yes" },
"keywords" : { "store" : "yes", "analyzer" : "keyword" },
"name" : { "store" : "yes" },
"content_length" : { "store" : "yes" },
"content_type" : { "store" : "yes" }
}
}
}
}
}
}'