Skip to content

Instantly share code, notes, and snippets.

@gudata
Forked from dedico/search_controller_v2.rb
Created May 22, 2013 09:59
Show Gist options
  • Save gudata/5626500 to your computer and use it in GitHub Desktop.
Save gudata/5626500 to your computer and use it in GitHub Desktop.
def search
paginate_per = 30
group_id = params[:part_group]
make_id = params[:make]
model_id = params[:model]
vehicle_category_id = params[:category]
vehicle_power = params[:power]
capacity = params[:capacity]
q = params[:q]
@results = Part.search do |search|
search.query do |query|
query.boolean do
must { all } if q.blank?
must { string q, :default_operator => 'AND' } unless q.blank?
must { term 'vehicles.vehicle_category_id', vehicle_category_id } unless vehicle_category_id.blank?
must { term 'vehicles.make_id', make_id } unless make_id.blank?
must { term 'vehicles.model_id', model_id } unless model_id.blank?
end
end
# filters
search.filter :term, 'vehicles.power_km' => vehicle_power unless vehicle_power.blank?
search.filter :term, 'vehicles.engine_capacity' => capacity unless capacity.blank?
# facets
search.facet 'power' do
terms 'vehicles.power_km', :size => 1000, :all_terms => true
end
if vehicle_power.blank?
search.facet 'capacity' do
terms 'vehicles.engine_capacity', :size => 1000, :all_terms => true
end
else
search.facet 'capacity' do
terms 'vehicles.engine_capacity', :size => 1000, :facet_filter => {:term => {'vehicles.power_km' => vehicle_power}}
end
end
# pagination
search.from ((params[:page].present? ? params[:page].to_i : 1).to_i * paginate_per) - paginate_per
search.size paginate_per
# debugging
@json_query = search.to_json
@curl_query = search.to_curl
#raise search.to_json
#raise search.to_curl
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment