Skip to content

Instantly share code, notes, and snippets.

@vpereira
Created April 9, 2012 09:52
Show Gist options
  • Save vpereira/2342609 to your computer and use it in GitHub Desktop.
Save vpereira/2342609 to your computer and use it in GitHub Desktop.
elasticsearch
class Author < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :author
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title,
indexes :description
indexes :author,type: 'object', properties: {
name: { type: 'multi_field',
fields: { name: { type: 'string', analyzer: 'snowball' },
exact: { type: 'string', index: 'not_analyzed' }
}
} }
end
def to_indexed_json
to_json(:include=>{:author=>{:only=>[:name]}} )
end
def self.search(params = {})
tire.search(load:true) do
query do
boolean do
should { string params[:q] } if params[:q].present?
should { term params[:author] } if params[:author].present?
end
end
filter :term, :active=>true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment