Last active
August 29, 2015 14:02
-
-
Save jurgens/50baf7f3fc10f0458713 to your computer and use it in GitHub Desktop.
elasticsearch/tire setup
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
class Model < ActiveRecord::Base | |
include Tire::Model::Search | |
include Tire::Model::Callbacks | |
mapping do | |
indexes :id, :index => :not_analyzed | |
indexes :custom_id, :index => :not_analyzed | |
indexes :slug, :index => :not_analyzed | |
indexes :name, :analyzer => 'snowball', boost: 3 | |
indexes :description, :analyzer => 'snowball' | |
indexes :program, :analyzer => 'snowball' | |
indexes :team, :analyzer => 'snowball' | |
end | |
def to_indexed_json | |
to_json only: [:id, :slug, :name, :description, :program, :team], methods: [:custom_id] | |
end | |
end |
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
class Search | |
class << self | |
def search(phrase) | |
Tire.search [tire_indexes] do | |
query { string phrase } | |
end.results | |
end | |
def tire_indexes | |
[Event, People, Project, Stewardship].map { |e| e.tire.index.name } | |
end | |
end | |
end |
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
Tire.configure do | |
prefix = "#{Rails.application.class.parent_name.downcase}_#{Rails.env.to_s.downcase}" | |
Tire::Model::Search.index_prefix(prefix) | |
logger 'log/elasticsearch.log', :level => 'debug' | |
end |
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 'spec_helper' | |
describe Search, elastic: true do | |
context 'when there is no data' do | |
specify do | |
results = Search.search 'anything' | |
expect(results.count).to be(0) | |
end | |
end | |
context 'when data exists' do | |
before do | |
@person = create :person, first_name: 'Angelo', last_name: 'Badalamenti' | |
@project = create :project, name: 'Project name', description: 'Lorem ipsum dolor angelo sit amet' | |
@stewardship = create :stewardship, name: 'Stewardship Angelo name' | |
end | |
before { People.tire.index.refresh } | |
before { Project.tire.index.refresh } | |
before { Stewardship.tire.index.refresh } | |
specify do | |
results = Search.search 'Angelo' | |
expect(results.count).to be(3) | |
end | |
end | |
end |
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
RSpec.configure do |config| | |
tire_models = [Event, People, Project, Stewardship] | |
config.before(:each, elastic: true) do | |
# re-create ES index for AR models integrated with Tire | |
tire_models.each do |klass| | |
klass.tire.index.delete | |
load Rails.root.join("app/models/#{klass.name.underscore}.rb") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment