use dsl for search definition and fix pagination

This commit is contained in:
Lars Henrik Mai 2015-06-01 23:53:45 +02:00 committed by Andreas Haller
parent aa644818c4
commit 5b677ab3ca
5 changed files with 28 additions and 37 deletions

View file

@ -1,35 +1,5 @@
class SearchController < ApplicationController class SearchController < ApplicationController
def index def index
@paper_type = params[:paper_type] @papers = Paper.search(params[:q]).page(params[:page]).results
@originator = params[:originator] end
@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
end end

View file

@ -29,5 +29,26 @@ class Paper < ActiveRecord::Base
end end
puts "Imported #{count - old_count} Papers!" puts "Imported #{count - old_count} Papers!"
end 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
end end

View file

@ -1,6 +1,6 @@
= form_tag(search_path, method: :get) = form_tag(search_path, method: :get)
= text_field_tag(:q, params[:q], placeholder: 'Suche…', autofocus: true) = text_field_tag(:q, params[:q], placeholder: 'Suche…', autofocus: true)
- if @show_filters - if false #@show_filters
fieldset fieldset
legend Ergebnisse filtern legend Ergebnisse filtern
.row .row

View file

@ -2,13 +2,13 @@ div
= render 'search/form' = render 'search/form'
.clearfix .clearfix
- if @show_search_result - if params[:q].present?
.left .left
| #{@papers_count} Treffer | #{@papers.total} Treffer
.right .right
/ button.button. TODO: Suche Abbonieren / button.button. TODO: Suche Abbonieren
- else - else
| #{@papers_count} Dokumente in der Datenbank | #{@papers.total} Dokumente in der Datenbank
div div
ul.no-bullet ul.no-bullet
- @papers.each do |doc| - @papers.each do |doc|

View file

@ -1,5 +1,5 @@
Kaminari.configure do |config| Kaminari.configure do |config|
# config.default_per_page = 25 config.default_per_page = 10
# config.max_per_page = nil # config.max_per_page = nil
# config.window = 4 # config.window = 4
# config.outer_window = 0 # config.outer_window = 0