Skip to content

Instantly share code, notes, and snippets.

@dedico
Created July 8, 2012 13:25
Show Gist options
  • Save dedico/3070955 to your computer and use it in GitHub Desktop.
Save dedico/3070955 to your computer and use it in GitHub Desktop.
Search action without filters on facets
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
search.facet 'capacity' do
terms 'vehicles.engine_capacity', :size => 1000, :all_terms => true
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