Last active
December 10, 2015 04:48
-
-
Save jwoertink/4383340 to your computer and use it in GitHub Desktop.
This file contains 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
class Event | |
include Mongoid::Document | |
include Tire::Model::Search | |
include Tire::Model::Callbacks | |
field :name, type: String | |
has_and_belongs_to_many :organizations | |
has_many :tabs | |
# elasticsearch indexes | |
mapping do | |
indexes :name | |
indexes :tabber_ids, type: 'string', index: :not_analyzed | |
indexes :organization_ids, type: 'string', index: :not_analyzed | |
end | |
def tabber_ids | |
tabs.collect(&:user_id).map(&:to_s) | |
end | |
# This is needed for mongoid. | |
def to_indexed_json | |
self.to_json(methods: [:coordinates, :tabber_ids, :organization_ids]) | |
end | |
end |
This file contains 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
class Search | |
def self.search(params = {}, options = {}) | |
Tire.multi_search do |s| | |
relevant_events(s, params, options) if params[:user] | |
end | |
end | |
def self.relevant_events(s, params = {}, options = {}) | |
performer_ids = params[:user].watching.where(watchable_type: 'Performer').collect(&:watchable_id).map(&:to_s) | |
s.search :relevant_events, index: Event.index_name, load: true do | |
query do | |
filtered do | |
query { string params[:query] } | |
filter :or, {terms: {performer_ids: performer_ids}}, | |
{terms: {watcher_ids: [params[:user].id]}} | |
end | |
end | |
end | |
end | |
end |
This file contains 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
describe Search do | |
let(:current_user) { FactoryGirl.create(:user) } | |
describe '#search' do | |
context 'when a user is provided' do | |
describe 'the returned collection of relevant events' do | |
let(:tabbed_event) do | |
create(:event, name: 'Shrimp Cocktails') | |
end | |
let(:performer) do | |
create(:performer_with_events, events_count: 1, name: 'Bubba-Gump Shrimp') | |
end | |
let(:performer_event) do | |
performer.events.first | |
end | |
subject { Search.search(query: 'shrimp', user: current_user) } | |
before do | |
current_user.tab(tabbed_event) | |
current_user.watch(performer) | |
Tire::Index.refresh_all | |
end | |
# Test passes | |
it 'contains events tabbed by the user' do | |
subject.results[:relevant_events].map(&:id).should include(tabbed_event.id) | |
end | |
# Test fails | |
# expected ["50db53acf8e0167f01000027"] to include "50db53acf8e0167f0100002b" | |
it 'contains events with performers the user watches' do | |
subject.results[:relevant_events].map(&:id).should include(performer_event.id) | |
end | |
end | |
end | |
end | |
end |
This file contains 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
// Sample curl output | |
{"index":"events"} | |
{"query":{"filtered":{"query":{"query_string":{"query":"shrimp"}},"filter":{"and":[{"or":[{"in":{"performer_ids":["50dcbad6f8e01665d600003e"]}},{"in":{"watcher_ids":[{"$oid": "50dcbad6f8e01665d6000037"}]}}]}]}}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment