mirror of
https://github.com/CodeforLeipzig/stadtratmonitor.git
synced 2024-12-22 15:43:14 +01:00
use dsl for search definition and fix pagination
This commit is contained in:
parent
aa644818c4
commit
5b677ab3ca
5 changed files with 28 additions and 37 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue