Created
April 9, 2012 09:52
-
-
Save vpereira/2342609 to your computer and use it in GitHub Desktop.
elasticsearch
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 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