* VRFS-32; elaticsearch simple search api done

This commit is contained in:
Seth Call 2012-11-07 07:36:00 -06:00
parent d6431973c7
commit 6fe362a86b
12 changed files with 127 additions and 9 deletions

View File

@ -24,6 +24,7 @@ gem 'gon' # for passthrough of Ruby variables to Javascript variables
gem 'eventmachine'
gem 'amqp'
gem 'logging-rails', :require => 'logging/rails'
gem 'tire'
group :development, :test do
gem 'rspec-rails', '2.11.0'

View File

@ -1,10 +1,9 @@
class ApiGenresController < ApplicationController
class ApiGenresController < ApiController
# have to be signed in currently to see this screen
before_filter :signed_in_user
#respond_to :json, :xml
respond_to :html
respond_to :json
def index
@genres = Genre.find(:all)

View File

@ -1,10 +1,9 @@
class ApiInstrumentsController < ApplicationController
class ApiInstrumentsController < ApiController
# have to be signed in currently to see this screen
before_filter :signed_in_user
#respond_to :json, :xml
respond_to :html
respond_to :json
def index
@instruments = Instrument.where('instruments.popularity > 0').order('instruments.popularity DESC, instruments.description ASC')

View File

@ -8,7 +8,6 @@ class ApiMusicSessionsController < ApiController
def initialize
@mq_router = MQRouter.new
@message_factory = MessageFactory.new
end
def index
@ -88,7 +87,7 @@ class ApiMusicSessionsController < ApiController
raise ActiveRecord::RecordNotound
end
@music_session.delete
@music_session.destroy # required to make 'tire' integration work
respond_with @music_session, responder: ApiResponder
end

View File

@ -0,0 +1,12 @@
class ApiSearchController < ApiController
# have to be signed in currently to see this screen
before_filter :signed_in_user
respond_to :json
def index
@search = Search.search(params[:query])
end
end

View File

@ -45,7 +45,7 @@ class ApiUsersController < ApplicationController
def delete
@user = User.find(params[:id])
@user.delete
@user.destroy # required to make 'tire' integration work
respond_with @user, responder: ApiResponder
end

View File

@ -0,0 +1,17 @@
object @search
child(:bands => :bands) {
attributes :id, :name, :location, :photo_url, :logo_url
}
child(:musicians => :musicians) {
attributes :id, :name, :location, :photo_url
}
child(:fans => :fans) {
attributes :id, :name, :location, :photo_url
}
child(:recordings => :recordings) {
attributes :id, :name
}

View File

@ -71,7 +71,15 @@ module SampleApp
# Add the assets/fonts directory to assets.paths
config.assets.paths << "#{Rails.root}/app/assets/fonts"
# where is rabbitmq?
config.rabbitmq_host = "localhost"
config.rabbitmq_port = 5672
# where is elasticsearch?
config.elasticsearch_uri = "http://localhost:9200"
# or 'verify' or 'none'. 'autorepair will automatically rebuild the elasticsearch index on startup of rails,
# if an inconsistency is detected'
config.elasticsearch_verify_mode = "autorepair"
end
end

View File

@ -0,0 +1,16 @@
Tire.configure do
logger Rails.root + "log/tire_#{Rails.env}.log"
url Rails.application.config.elasticsearch_uri
end
User.create_search_index unless User.search_index.exists?
Band.create_search_index unless Band.search_index.exists?
# Verify elasticsearch integrity
if Rails.application.config.elasticsearch_verify_mode == "autorepair"
unless TireTasks.verify
TireTasks.rebuild_indexes
end
elsif Rails.application.config.elasticsearch_verify_mode == "verify"
TireTasks.verify
end

View File

@ -85,5 +85,8 @@ SampleApp::Application.routes.draw do
# invitations
match '/instruments/:id' => 'api_instruments#show', :via => :get, :as => 'api_instrument_detail'
match '/instruments' => 'api_instruments#index', :via => :get
# search
match '/search' => 'api_search#index', :via => :get
end
end

View File

@ -216,6 +216,7 @@ describe "Music Session API ", :type => :api do
client = FactoryGirl.create(:connection, :user => user, :ip_address => "1.1.1.1")
post '/api/sessions.json', { :description => "a session", :client_id => client.client_id, :genres => ["classical"], :musician_access => true, :tracks => [{"instrument_id" => "electric guitar", "sound" => "mom"}]}.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should eql(422)
JSON.parse(last_response.body)["errors"]["connection_tracks"][0].should == "is invalid"

View File

@ -0,0 +1,63 @@
require 'spec_helper'
describe "Search API ", :type => :api do
include Rack::Test::Methods
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
Band.delete_search_index
Band.create_search_index
User.delete_search_index
User.create_search_index
post '/sessions', "session[email]" => user.email, "session[password]" => user.password
rack_mock_session.cookie_jar["remember_token"].should == user.remember_token
end
it "empty search" do
get '/api/search.json'
last_response.status.should == 200
JSON.parse(last_response.body).should eql(JSON.parse('{"fans" : [], "musicians" : [], "bands": [], "recordings" : []}'))
end
it "simple search" do
User.delete_search_index # so that the user created before the test and logged in doesn't show up
User.create_search_index
@musician = User.save(name: "Peach", email: "user@example.com",
password: "foobar", password_confirmation: "foobar", musician: true)
@fan = User.save(name: "Peach Peach", email: "fan@example.com",
password: "foobar", password_confirmation: "foobar", musician: false)
@band = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@band2 = Band.save(name: "Peach", website: "www.bands2.com", biography: "zomg we rock")
User.search_index.refresh
Band.search_index.refresh
get '/api/search.json?query=peach'
last_response.status.should == 200
response = JSON.parse(last_response.body)
response["musicians"].length.should == 1
musician = response["musicians"][0]
musician["id"].should == @musician.id
response["fans"].length.should == 1
fan = response["fans"][0]
fan["id"].should == @fan.id
response["bands"].length.should == 2
band = response["bands"][0]
band["id"].should == @band2.id
band = response["bands"][1]
band["id"].should == @band.id
response["recordings"].length.should == 0
band = response["recordings"][0]
end
end
end