Skip to content

Instantly share code, notes, and snippets.

@squarism
Created May 13, 2013 03:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save squarism/5566066 to your computer and use it in GitHub Desktop.
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.
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