Created
May 13, 2013 03:54
-
-
Save squarism/5566066 to your computer and use it in GitHub Desktop.
RethinkDB json store, using nobrainer ORM, automatically update elasticsearch index on save callback. This is fantastic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'tire' | |
require 'nobrainer' | |
NoBrainer.connect 'rethinkdb://server/company' | |
class Employee | |
include NoBrainer::Document | |
field :name | |
field :title | |
field :bio | |
# has_many :jobs | |
after_save do | |
employee = self | |
Tire.index 'bios' do | |
delete | |
create | |
store employee | |
refresh | |
end | |
end | |
validates :name, :presence => true | |
def to_indexed_json | |
{:name => self.name, :title => self.title, :bio => self.bio}.to_json | |
end | |
end | |
# pry or main or usage or whatever below | |
tony = Employee.create(name: 'Tony Tates', title: 'Gossip Dragon', bio: 'Pie is great.') | |
s = Tire.search('bios') do | |
query do | |
string 'pie' | |
end | |
end | |
# => [<Item name: "Tony Tates", title: "Gossip Dragon", bio: "Pie is great.", | |
# id: "519062d7cb759b91a3000001", _score: 0.11506981, _type: "document", | |
# _index: "bios", _version: nil, sort: nil, highlight: nil, _explanation: nil>] | |
# rad. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment