Skip to content

Instantly share code, notes, and snippets.

@pije76
Created April 26, 2014 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pije76/11329588 to your computer and use it in GitHub Desktop.
Save pije76/11329588 to your computer and use it in GitHub Desktop.
# config/initializers/elasticsearch.rb
require 'elasticsearch'
module FooBar
class Application < Rails::Application
YAML.load_file("#{File.join(Rails.root, '/config/environments/', Rails.env, 'elasticsearch.yml')}").each { |k,v| config.send "#{k}=", v }
elasticsearch_client = Elasticsearch::Client.new hosts: Rails.application.config.elasticsearch[:servers], log: true, reload_connections: true
config.send 'elasticsearch_client=', elasticsearch_client
end
end
Rails.application.config.elasticsearch[:indices].each do |es_index|
es_index_name = es_index[0]
es_maps = es_index[1]['mappings']
es_maps.each do |es_type, es_body|
Rails.application.config.elasticsearch_client.indices.put_mapping index: es_index_name, type: es_type, body: es_body
end
end
# config/environments/development/elasticsearch.yml
---
:elasticsearch:
:indices:
foobar:
mappings:
client_view:
properties:
property_a
type: string
has_attachment:
type: boolean
project:
type: string
recipient:
type: string
sender:
type: string
subject:
type: string
url:
type: string
:servers:
- 127.0.0.1:9200

Quick and Dirty Integration of elasticsearch api with rails

Take the following with a grain of salt ...

We started prototyping with ES & Rails about 2 days ago and I quickly discovered that things became a bit unwieldy the more I utilized elasticsearch. For now I am running with this type of integration with rails 3.2.15 to assess elasticsearch.

While we have done preliminary reviews on how to set up a production environment (still scratching my head on how to handle the split brain bug) , there's certainly plenty of what I'm not aware of, so if you see a fault in this approach, please let me know.

I may update this later on with a couple of convenience methods on the data models ...

Note, this is example is very likely not thread-safe!

elastic/elasticsearch-ruby#14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment