From 59d3f444788ea905de5daa106b75c0cfcfe123da Mon Sep 17 00:00:00 2001 From: Seth Call Date: Wed, 7 Nov 2012 07:10:41 -0600 Subject: [PATCH] * VRFS-32; integrating elasticsearch and /api/search completed --- Gemfile | 2 ++ lib/jam_ruby.rb | 8 ++++- lib/jam_ruby/models/band.rb | 55 +++++++++++++++++++++++++++++++ lib/jam_ruby/models/user.rb | 65 ++++++++++++++++++++++++++++++++++--- 4 files changed, 125 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index e69e2ea75..0db9350fa 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,8 @@ gem 'bcrypt-ruby', '3.0.1' gem 'ruby-protocol-buffers', '1.2.2' gem 'eventmachine' gem 'amqp' +gem 'tire' +gem 'will_paginate' group :test do gem 'jam_db', :path=> "#{workspace}/jam-db/target/ruby_package" diff --git a/lib/jam_ruby.rb b/lib/jam_ruby.rb index 0f0fada36..b6f875d3b 100644 --- a/lib/jam_ruby.rb +++ b/lib/jam_ruby.rb @@ -3,12 +3,17 @@ require "active_record" require "jampb" require "uuidtools" require "logging" +require "tire" +require "will_paginate" +require "will_paginate/active_record" require "jam_ruby/errors/permission_error" require "jam_ruby/errors/state_error" require "jam_ruby/errors/jam_argument_error" require "jam_ruby/mq_router" require "jam_ruby/connection_manager" require "jam_ruby/version" +require "jam_ruby/environment" +require "jam_ruby/tire_tasks" require "jam_ruby/message_factory" require "jam_ruby/models/genre" require "jam_ruby/models/user" @@ -22,9 +27,10 @@ require "jam_ruby/models/instrument" require "jam_ruby/models/connection_track" require "jam_ruby/models/musician_instrument" require "jam_ruby/models/band_musician" +require "jam_ruby/models/search" include Jampb module JamRuby - + end diff --git a/lib/jam_ruby/models/band.rb b/lib/jam_ruby/models/band.rb index 965f49c27..c0afa6d91 100644 --- a/lib/jam_ruby/models/band.rb +++ b/lib/jam_ruby/models/band.rb @@ -1,5 +1,7 @@ module JamRuby class Band < ActiveRecord::Base + include Tire::Model::Search + include Tire::Model::Callbacks attr_accessible :name, :website, :biography @@ -24,6 +26,12 @@ module JamRuby @logo_url = "http://www.jamkazam.com/images/bands/logos/#{self.id}.gif" end + def location + # TODO: implement a single string version of location; + # this will be indexed into elasticsearch and returned in search + return "Austin, TX" + end + # helper method for creating / updating a Band def self.save(params) if params[:id].nil? @@ -74,5 +82,52 @@ module JamRuby errors.add(:genres, "No more than 3 genres are allowed.") end end + + + ### Elasticsearch/Tire integration ### + # + # Define the name based on the environment + # We wouldn't like to erase dev data during + # test runs! + # + index_name("#{Environment.mode}-#{Environment.application}-bands") + + def to_indexed_json + { + :name => name, + :logo_url => logo_url, + :photo_url => photo_url, + :location => location + }.to_json + end + + class << self + def create_search_index + Tire.index(Band.index_name) do + create( + :settings => Search.index_settings, + :mappings => { + "jam_ruby/band" => { + :properties => { + :logo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false }, + :photo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false}, + :name => { :type => :string, :boost => 100}, + :location => { :type => :string }, + } + } + } + ) + end + end + + def delete_search_index + search_index.delete + end + + def search_index + Tire.index(Band.index_name) + end + end + ### Elasticsearch/Tire integration end end \ No newline at end of file diff --git a/lib/jam_ruby/models/user.rb b/lib/jam_ruby/models/user.rb index 039068546..9949c3198 100644 --- a/lib/jam_ruby/models/user.rb +++ b/lib/jam_ruby/models/user.rb @@ -1,11 +1,13 @@ module JamRuby class User < ActiveRecord::Base + include Tire::Model::Search + include Tire::Model::Callbacks attr_accessible :name, :email, :password, :password_confirmation attr_accessor :updating_password self.primary_key = 'id' - + # connections (websocket-gateway) has_many :connections, :class_name => "JamRuby::Connection" @@ -15,7 +17,7 @@ module JamRuby # instruments has_many :musician_instruments has_many :instruments, :through => :musician_instruments, :class_name => "JamRuby::Instrument" - + # bands has_many :band_musicians has_many :bands, :through => :band_musicians, :class_name => "JamRuby::Band" @@ -37,7 +39,7 @@ module JamRuby # invitations has_many :received_invitations, :foreign_key => "receiver_id", :inverse_of => :receiver, :class_name => "JamRuby::Invitation" has_many :sent_invitations, :foreign_key => "sender_id", :inverse_of => :sender, :class_name => "JamRuby::Invitation" - + has_secure_password before_save { |user| user.email = email.downcase } @@ -65,6 +67,12 @@ module JamRuby @photo_url = "http://www.jamkazam.com/images/users/photos/#{self.id}.gif"; end + def location + # TODO: implement a single string version of location; + # this will be indexed into elasticsearch and returned in search + return "Austin, TX" + end + def should_validate_password? updating_password || new_record? end @@ -143,9 +151,58 @@ module JamRuby end end + + ### Elasticsearch/Tire integration ### + # + # Define the name based on the environment + # We wouldn't like to erase dev data during + # test runs! + # + index_name("#{Environment.mode}-#{Environment.application}-users") + + def to_indexed_json + { + :name => name, + :photo_url => photo_url, + :location => location, + :musician => musician + }.to_json + end + + class << self + def create_search_index + Tire.index(User.index_name) do + create( + :settings => Search.index_settings, + :mappings => { + "jam_ruby/user" => { + :properties => { + :photo_url => { :type => :string, :index => :not_analyzed, :include_in_all => false}, + :location => { :type => :string }, + :name => { :type => :string, :boost => 100 }, + :is_musician => { :type => :boolean, :index => :not_analyzed, :include_in_all => false} + } + } + } + ) + end + end + + def delete_search_index + search_index.delete + end + + def search_index + Tire.index(User.index_name) + end + end + ### Elasticsearch/Tire integration + + + private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 - end + end end end