Save Image in Elasticsearch with Ruby


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' <a href="https://i.stack.imgur.com/9Kq3s.jpg" rel="nofollow"><img alt="obligatory lena" class="b-lazy" data-src="https://i.stack.imgur.com/9Kq3s.jpg" data-original="https://i.stack.imgur.com/9Kq3s.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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!


  • Add routes to Dashing dashboard
  • How to render a partial in sinatra view (haml in haml)?
  • rails 3, heroku, aws-s3, simply trying to upload a file to S3 that is POSTed (http/multipart) to our
  • Rails 3 routing how to match multiple?
  • Get picture position in Apache POI from Excel xls HSSF
  • FSO returns non-existing subfolders
  • iOS - How to access the device's file library?
  • Hiding Directories Programatically in C#
  • How do I capture a rsolr Sunspot exception raised on a different thread from a model callback?
  • SSL client cert authentication for only some URLs?
  • CORS with socket.io
  • mave 3.2 not able to access local nexus instance return 502 code
  • VSCode change debug shell to bash on windows
  • Tomcat memory Leak
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Spring Cloud Microservice Architecture Confusion
  • How does document.ready work with angular element directives?
  • Not able to aggregate on nested fields in elasticsearch
  • gspread or such: help me get cell coordinates (not value)
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is it possible to access block's scope in method?
  • Cannot resolve symbol 'MyApi'
  • Q promise. Difference between .when and .then
  • AT Commands to Send SMS not working in Windows 8.1
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Acquiring multiple attributes from .xml file in c#
  • C# - Getting references of reference
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • JaxB to read class hierarchy
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal