From f883df33472328527facfc48bfff666449521efa Mon Sep 17 00:00:00 2001 From: Lars Henrik Mai Date: Mon, 5 Oct 2015 22:13:53 +0200 Subject: [PATCH] Use PaperSearch definition in controller --- app/controllers/search_controller.rb | 12 +++++------- app/models/paper.rb | 7 +++---- app/models/paper_search.rb | 11 ++++++++++- app/views/search/_form.slim | 29 ++++++++++++++-------------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 9af1020..56771cc 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -7,14 +7,12 @@ end class SearchController < ApplicationController def index - @paper_type = params[:paper_type] - @originator = params[:originator] - - options = params.slice(:paper_type, :originator, :sort_by) + @search_definition = PaperSearch.new(query: params[:q], + paper_type: params[:paper_type], + originator: params[:originator], + sort_by: params[:sort_by]) - @show_filters = true - - @response = Paper.search(params[:q], options) + @response = Paper.search(@search_definition) @papers = @response.page(params[:page]).results @paper_type_facets = extract_facets('paper_types') @originator_facets = extract_facets('originators') diff --git a/app/models/paper.rb b/app/models/paper.rb index d1c0068..8e49670 100644 --- a/app/models/paper.rb +++ b/app/models/paper.rb @@ -61,10 +61,9 @@ class Paper < ActiveRecord::Base # use DSL to define search queries # see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl # and https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-rails/lib/rails/templates - def search(q, options={}) - @search_definition = PaperSearch.definition(q, options) - Rails.logger.debug "Query: #{@search_definition.to_json}" - __elasticsearch__.search(@search_definition) + def search(search_definition) + Rails.logger.debug "Query: #{search_definition.to_json}" + __elasticsearch__.search(search_definition) end def reset_index! diff --git a/app/models/paper_search.rb b/app/models/paper_search.rb index 5770292..92035ab 100644 --- a/app/models/paper_search.rb +++ b/app/models/paper_search.rb @@ -1,5 +1,14 @@ class PaperSearch + include ActiveModel::Model + + attr_accessor :query, :paper_type, :originator, :sort_by + + def to_hash + options = {paper_type: @paper_type, originator: @originator, sort_by: @sort_by} + PaperSearch.definition(@query, options) + end + def self.definition(q, options={}) Elasticsearch::DSL::Search.search do @@ -28,7 +37,7 @@ class PaperSearch must { term paper_type: options[:paper_type] } if options[:paper_type].present? must { term originator: options[:originator] } if options[:originator].present? # catchall when no filters set - must { match_all } if options.keys.none? {|k| [:paper_type, :originator].include?(k) } + must { match_all } unless (options[:paper_type].present? || options[:originator].present?) end end diff --git a/app/views/search/_form.slim b/app/views/search/_form.slim index 9d04566..6756ea9 100644 --- a/app/views/search/_form.slim +++ b/app/views/search/_form.slim @@ -1,16 +1,15 @@ = form_tag(search_path, method: :get) - = text_field_tag(:q, params[:q], placeholder: 'Suche…', autofocus: true) - - if @show_filters - fieldset - legend Ergebnisse filtern - .row - .small-4.columns - = filter_select("paper_type", "Typ", @paper_type_facets, @paper_type) - .small-4.columns - = filter_select("originator", "Einreicher", @originator_facets, @originator) - .small-4.columns.end - label Sortierung - input type="radio" name="sort_by" value="date" id="sort_by_date" - label for="sort_by_date" Nach Datum - input type="radio" name="sort_by" value="score" id="sort_by_score" - label for="sort_by_score" Nach Relevanz + = text_field_tag(:q, @search_definition.query, placeholder: 'Suche…', autofocus: true) + fieldset + legend Ergebnisse filtern + .row + .small-4.columns + = filter_select("paper_type", "Typ", @paper_type_facets, @search_definition.paper_type) + .small-4.columns + = filter_select("originator", "Einreicher", @originator_facets, @search_definition.originator) + .small-4.columns.end + label Sortierung + input type="radio" name="sort_by" value="date" id="sort_by_date" + label for="sort_by_date" Nach Datum + input type="radio" name="sort_by" value="score" id="sort_by_score" + label for="sort_by_score" Nach Relevanz