Image 使用python 3.6.0在elasticsearch中存储图像的正确方法
约束Image 使用python 3.6.0在elasticsearch中存储图像的正确方法,image,python-3.x,elasticsearch,Image,Python 3.x,elasticsearch,约束 语言Python 3.6.0 图像大小高达5MB[格式.PNG.JPG和.JEPG] 我必须在elasticsearch中存储图像。这是一项要求。然而,只要图像可以重建,我使用哪种格式并不重要 我有图像的物理位置。我打开图像并将其转换为base64格式。然后我尝试在运行在我的localhost上的elasticsearch中对其进行索引。但它不起作用。我假设我需要在这里使用批量api。但我发现批量api执行操作或生成器。在我的情况下,如何使用批量在elasticsearch中保存图像?
- 语言Python 3.6.0
- 图像大小高达5MB[格式.PNG.JPG和.JEPG]
- 我必须在elasticsearch中存储图像。这是一项要求。然而,只要图像可以重建,我使用哪种格式并不重要
base64
格式。然后我尝试在运行在我的localhost
上的elasticsearch中对其进行索引。但它不起作用。我假设我需要在这里使用批量api。但我发现批量api执行操作
或生成器
。在我的情况下,如何使用批量在elasticsearch中保存图像?或者在elasticsearch中是否有其他有效的方法来索引图像
请注意,我可以成功地将图像加载并编码为字节
。另外,其他索引
和搜索(GET)
查询在我的localhost:9200
上运行良好
这是我目前的做法
from elasticsearch import Elasticsearch
import uuid
import base64
client = Elasticsearch([{'host': 'localhost', 'port':9200}])
def persist_image_in_elastic(imagePath):
curMethodst = time.time()
# imagePath = 'images/heroalom/image_22.png'
with open(imagePath, "rb") as imageFile:
rawImage = base64.b64encode(imageFile.read())
elasticIndex = 'raw-image-index'
doc_type = 'raw-image'
rawImageModel = {'id': 'f00b5f7c17534d22ab5cfb950bea972c', 'raw': rawImage }
elasticResp = client.index(index = elasticIndex, doc_type = doc_type,id = idForReceivedImage, body = rawImageModel)
弹性搜索的映射
{
"raw-image-index": {
"mappings": {
"raw-image": {
"properties": {
"id": {
"type": "text"
},
"raw": {
"type": "text"
}
}
}
}
}
}
你就快到了。您只需在
str()
调用中包装rawImage
,如下所示:
rawImageModel = {'id': 'f00b5f7c17534d22ab5cfb950bea972c', 'raw': str(rawImage) }
现在我来解释一下。返回类型为的对象,而ElasticSearch客户端需要string
事实上,您提供的python代码引发了一个可用于调试的异常:
Traceback (most recent call last):
File "code.py", line 19, in <module>
persist_image_in_elastic('/Users/vasiliev/Downloads/es_logo_small.png')
File "code.py", line 17, in persist_image_in_elastic
elasticResp = client.index(index = elasticIndex, doc_type = doc_type,id = 'f00b5f7c17534d22ab5cfb950bea972c', body = rawImageModel)
File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped
return func(*args, params=params, **kwargs)
File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 298, in index
_make_path(index, doc_type, id), params=params, body=body)
File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/transport.py", line 278, in perform_request
body = self.serializer.dumps(body)
File "/Users/vasiliev/.virtualenvs/es-blob-3.6/lib/python3.6/site-packages/elasticsearch/serializer.py", line 50, in dumps
raise SerializationError(data, e)
elasticsearch.exceptions.SerializationError: ({'id': 'f00b5f7c17534d22ab5cfb950bea972c', 'raw': b'iVB...mCC'}, TypeError("Unable to serialize b'iVB...mCC' (type: <class 'bytes'>)",))
另一方面,您使用的是哪个IDE?(只是问一下)为了处理这段代码,我只使用了vim。对于较大的python应用程序/脚本,我使用PyCharm,它有社区版(免费),适合我的需要。
{
"raw-image-index": {
"mappings": {
"raw-image": {
"properties": {
"id": {
"type": "text"
},
"raw": {
"type": "text",
"index": "no"
}
}
}
}
}
}