Dilute
It's an ORM for ElasticSearch.
Disclosure: I'm learning ElasticSearch, this is the product of some of my experiments. This has been helpful for my needs, but is very new.
Installation
gem 'dilute'
Usage
In a Rails app called "MyRailsApp" ...
class Note
include Dilute::Modelize
define_type do
attribute(:data).not_indexed
attribute(:header).not_indexed
attribute(:keys).index_analyzer("keyword")
attribute(:created_at, :date)
end
end
This will create a new index called "MyRailsApp" with a new type mapping "notes".
define_type can some options: server_url
, index_name
, type_name
define_type(server_url: "http://example.com/", index_name: "special_name", type_name: "something_else")
With a class set up you can:
note = Note.new({data: "whatever"})
note.save # Stored to ElasticSearch
Note.find(note.id) # fetches that stored record
Note.all # returns a query object, use an enumerable method to get actual records
Note.all.each { |n| n.data }
# Search with "match"
Note.all.match(data: "whatever").each {|n| ... }
ActiveModel stuff: You can do callbacks on save and use ActiveModel validations.
class Note
include Dilute::Modelize
validates_presense_of :data
before_save :hi
def hi
puts "Hi, you're about to save a note!"
end
define_type do
attribute(:data).not_indexed
end
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request