Created
April 24, 2014 19:49
-
-
Save hegeltrigo/11267229 to your computer and use it in GitHub Desktop.
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
#searchable do | |
# text :name | |
# text :owner_name | |
# integer :designer_ids, :multiple => true do | |
# designers.map(&:id) | |
# end | |
# string :letters, :multiple => true do | |
# designers.map(&:name).map{|letter| letter.first.capitalize} | |
# end | |
# #integer :season_ids, :multiple => true do | |
# # | |
# #end | |
# integer :style_ids, :multiple => true do | |
# styles.map(&:id) | |
# end | |
# string :trends, :multiple => true do | |
# tag_list | |
# end | |
# string :country_continents, :multiple => true do | |
# country_continent = [] | |
# country_continent << CODE_COUNTRIES[country] if self.country.present? # for get the continent | |
# country_continent << country_name if self.country.present? # for get the country | |
# end | |
#end | |
mapping do | |
indexes :name, boost: 10 | |
indexes :owner_name | |
indexes :designer_ids do | |
indexes :id, :type => :integer, :index => "not_analyzed" | |
end | |
indexes :style_ids do | |
indexes :id, :type => :integer, :index => "not_analyzed" | |
end | |
indexes :letters do | |
indexes :name, :type => :string, :index => "not_analyzed" | |
end | |
indexes :trends do | |
indexes :name, :type => :string, :index => "not_analyzed" | |
end | |
indexes :country_continents do | |
indexes :id, :type => :string, :index => "not_analyzed" | |
end | |
end | |
def self.search(params) | |
tire.search(load: true, page: params[:page], per_page: params[:per_page]) do | |
query { string params[:search], default_operator: "AND" } if params[:search].present? | |
filter :terms, :designer_ids => params[:designer_ids] if params[:designer_ids].present? | |
filter :terms, :letters => params[:letters] if params[:letters].present? and !params[:letters].include?("@") | |
filter :terms, :style_ids => params[:style_ids] if params[:style_ids].present? | |
filter :terms, :trends => params[:trends] if params[:trends].present? | |
filter :terms, :country_continents => params[:country_continents] if params[:country_continents].present? | |
end | |
end | |
# self.include_root_in_json = false (necessary before Rails 3.1) | |
def to_indexed_json | |
to_json(methods: [:designer_ids, :letters, :style_ids, :trends, :country_continents]) | |
end | |
def designer_ids | |
designers.map(&:id) | |
end | |
def letters | |
designers.map(&:name).map{|letter| letter.first.capitalize} | |
end | |
def style_ids | |
styles.map(&:id) | |
end | |
def trends | |
tag_list | |
end | |
def country_continents | |
country_continent = [] | |
country_continent << CODE_COUNTRIES[country] if self.country.present? # for get the continent | |
country_continent << country_name if self.country.present? # for get the country | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment