使用Ruby在Elasticsearch中保存图像 [英] Save Image in Elasticsearch with Ruby

查看:878
本文介绍了使用Ruby在Elasticsearch中保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Elasticsearch作为我的Ruby / Sinatra应用程序的数据存储区,我想要保存图像。有没有办法将图像作为二进制索引到ES?如果是这样,我该怎么做,然后将二进制文件转换成图像,以便在网站上显示?

解决方案

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'#hardcoded内容类型现在
fileContent = Base64.decode64(@image [0] .image);
end

post'/ sell / pictures'do
#adsimagesindex / images
image_id = SecureRandom.hex
file = params [:file] [ :tempfile] #get从body的帖子
fileContent = file.read
fileContent = Base64.encode64(fileContent)
@ es_client.index index:'adsimages',键入:'images', id:image_id,body:{id:image_id,image:fileContent}
redirect'/ ads / sell / pictures'
end

然后使用表单提交图像

 < form class = form-horizo​​ntalaction =/ ads / sell / picturesmethod =post> 
< div class =container>
< div class =form-group>
< label for =upload-pictures>上传图片< / label>
< input type =fileid =filename =upload-pictures []multiple>
< / div>

< button type =submitclass =btn btn-default> Next< / button>
< / div>
< / form>

检索图像'GET / ads / sell / pictures / 7a911a0355ad1cc3cfc78bbf6038699b'
< a href =https://i.stack.imgur.com/9Kq3s.jpg =nofollow noreferrer>



如果要将图像与文档一起存储(根据用例),可以在执行时省略图像字段通过指定要返回的字段进行搜索。或者,您可以将图像ID存储在文档中,并为图像创建索引。



希望这有帮助!


I'm using Elasticsearch as a datastore for my Ruby/Sinatra app, and I'm wanting to save images. Is there a way to index an image as a binary into ES? If so, how should I go about doing this and then converting the binary back to an image in order to display it on the site?

解决方案

Elasticsearch can store binary data using the binary type. The binary type needs to be base64 encoded and will not be indexed by default. Here is an example es mapping

POST http://localhost:9200/adimages/
{
    "mappings" : {
    "images" : {
        "properties" : {
            "image" : { "type" : "binary"},
            "id" : {"type" : "string"}
        }
    }
}

Some sinatra/ruby code

  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' #hardcoded content type for now
    fileContent = Base64.decode64(@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.encode64(fileContent)
    @es_client.index index: 'adsimages', type: 'images', id: image_id, body: {id: image_id, image: fileContent}
    redirect '/ads/sell/pictures' 
  end 

You then use a form to submit the image

<form class="form-horizontal" action="/ads/sell/pictures" method="post">
    <div class="container"> 
      <div class="form-group">
        <label for="upload-pictures">Upload Pictures</label>
        <input type="file" id="file" name="upload-pictures[]" multiple>
      </div>

      <button type="submit" class="btn btn-default">Next</button>
    </div>
</form>

to retrieve the image do 'GET /ads/sell/pictures/7a911a0355ad1cc3cfc78bbf6038699b'

If you want to store images along with the your documents (depending on your use case) you can omit the image field when doing the search by specifying the fields you want returned back. Alternatively you can just store the image id(s) with your documents and create an index only for images.

Hope this helps!

这篇关于使用Ruby在Elasticsearch中保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆