栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Ruby在Elasticsearch中保存图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Ruby在Elasticsearch中保存图像

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与文档一起存储,并仅为图像创建索引。

希望这可以帮助!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号