stadtratmonitor/app/controllers/search_controller.rb

42 lines
1,023 B
Ruby
Raw Normal View History

SearchFacet = Struct.new("SearchFacet", :term, :count) do
def term_with_count
"#{term} (#{count})"
end
end
class SearchController < ApplicationController
def index
2015-10-10 09:48:18 +02:00
@search_definition = params[:paper_search].present? ? PaperSearch.new(search_params) : PaperSearch.new
2015-10-10 07:45:30 +02:00
@search_definition.sort_by ||= "score"
2015-06-22 20:38:24 +02:00
2015-10-10 09:48:18 +02:00
execute_search
end
def show
@search_definition = PaperSearch.find params[:id]
execute_search
render action: "index"
end
private
def execute_search
@response = Paper.search(@search_definition.to_definition)
2015-06-13 21:18:55 +02:00
@papers = @response.page(params[:page]).results
2015-06-22 21:11:51 +02:00
@paper_type_facets = extract_facets('paper_types')
@originator_facets = extract_facets('originators')
end
2015-06-22 21:11:51 +02:00
2015-10-10 09:48:18 +02:00
def search_params
params.require(:paper_search).permit(:query, :paper_type, :originator, :sort_by)
end
2015-06-22 21:11:51 +02:00
def extract_facets(name)
@response.
response['aggregations'][name.to_s][name.to_s]['buckets'].
map {|m| SearchFacet.new(m['key'], m['doc_count'])}
2015-06-22 21:11:51 +02:00
end
end