diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 45f2ab0..5a80d0f 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,35 +1,5 @@ class SearchController < ApplicationController def index - @paper_type = params[:paper_type] - @originator = params[:originator] - - @show_search_result = @show_filters = params[:q].present? - papers_found = if @show_search_result - Paper.search(params[:q]).records - else - Paper.all - end - - if @show_filters - - @paper_types_found = papers_found.map(&:paper_type).uniq - @originators_found = papers_found.map(&:originator).uniq - - if @paper_type.present? - papers_found = papers_found.where(paper_type: @paper_type) - end - - if @originator.present? - papers_found = papers_found.where(originator: @originator) - end - end - - @papers_count = papers_found.page(params[:page]).total_count - @papers = papers_found.page(params[:page]) - # Fixme Pagination - #order(published_at: :desc). - # page(params[:page]) - - - end + @papers = Paper.search(params[:q]).page(params[:page]).results + end end diff --git a/app/models/paper.rb b/app/models/paper.rb index 77ac812..42b0004 100644 --- a/app/models/paper.rb +++ b/app/models/paper.rb @@ -29,5 +29,26 @@ class Paper < ActiveRecord::Base end puts "Imported #{count - old_count} Papers!" end + + # 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) + @search_definition = Elasticsearch::DSL::Search.search do + query do + unless q.blank? + multi_match do + query q + fields ["name", "content"] + end + else + match_all + end + end + end + puts @search_definition.to_hash + __elasticsearch__.search(@search_definition) + end + end end diff --git a/app/views/search/_form.slim b/app/views/search/_form.slim index 30eadb5..3d2edd7 100644 --- a/app/views/search/_form.slim +++ b/app/views/search/_form.slim @@ -1,6 +1,6 @@ = form_tag(search_path, method: :get) = text_field_tag(:q, params[:q], placeholder: 'Sucheā€¦', autofocus: true) - - if @show_filters + - if false #@show_filters fieldset legend Ergebnisse filtern .row diff --git a/app/views/search/index.html.slim b/app/views/search/index.html.slim index bef6891..76aca9b 100644 --- a/app/views/search/index.html.slim +++ b/app/views/search/index.html.slim @@ -2,13 +2,13 @@ div = render 'search/form' .clearfix - - if @show_search_result + - if params[:q].present? .left - | #{@papers_count} Treffer + | #{@papers.total} Treffer .right / button.button. TODO: Suche Abbonieren - else - | #{@papers_count} Dokumente in der Datenbank + | #{@papers.total} Dokumente in der Datenbank div ul.no-bullet - @papers.each do |doc| diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb index b1799b0..2f8aa08 100644 --- a/config/initializers/kaminari_config.rb +++ b/config/initializers/kaminari_config.rb @@ -1,5 +1,5 @@ Kaminari.configure do |config| - # config.default_per_page = 25 + config.default_per_page = 10 # config.max_per_page = nil # config.window = 4 # config.outer_window = 0