Elasticsearch可以使用二进制类型存储二进制数据。二进制类型需要使用base64编码,并且默认情况下不会被索引。这是一个ES映射示例
POST http://localhost:9200/adimages/{ "mappings" : { "images" : { "properties" : { "image" : { "type" : "binary"}, "id" : {"type" : "string"} } }}一些sinatra / ruby代码
get '/pictures/:name' do |name| @image = @es_client.search index: 'adsimages', body: { query: { match: { id: name } } } @image = AdHelpers.get_hashie_list(@image) content_type 'image/png' #hardpred content type for now fileContent = base64.depre64(@image[0].image); end post '/sell/pictures' do #adsimagesindex/images image_id = SecureRandom.hex file = params[:file][:tempfile] #get the post from body fileContent = file.read fileContent = base64.enpre64(fileContent) @es_client.index index: 'adsimages', type: 'images', id: image_id, body: {id: image_id, image: fileContent} redirect '/ads/sell/pictures' end然后,您使用表格提交图像
<form action="/ads/sell/pictures" method="post"> <div > <div > <label for="upload-pictures">Upload Pictures</label> <input type="file" id="file" name="upload-pictures[]" multiple> </div> <button type="submit" >Next</button> </div></form>
检索图像,请执行’GET / ads / sell / pictures / 7a911a0355ad1cc3cfc78bbf6038699b’
如果要与文档一起存储图像(取决于用例),可以在搜索时通过指定要返回的字段来省略图像字段。或者,您可以仅将图像ID与文档一起存储,并仅为图像创建索引。
希望这可以帮助!



