mirror of
https://github.com/CodeforLeipzig/stadtratmonitor.git
synced 2024-12-22 15:43:14 +01:00
add filter by paper_type
This commit is contained in:
parent
7a7083c2ad
commit
41ed4a9674
2 changed files with 27 additions and 9 deletions
|
@ -1,6 +1,9 @@
|
|||
class SearchController < ApplicationController
|
||||
def index
|
||||
@response = Paper.search(params[:q])
|
||||
@paper_type = params[:paper_type]
|
||||
options = params.slice(:paper_type)
|
||||
|
||||
@response = Paper.search(params[:q], options)
|
||||
@papers = @response.page(params[:page]).results
|
||||
@paper_type_facets = @response.response['aggregations']['paper_types']['buckets'].map {|h| [ h['key'], h['doc_count']] }
|
||||
@originator_facets = @response.response['aggregations']['originators']['buckets'].map {|h| [ h['key'], h['doc_count']] }
|
||||
|
|
|
@ -43,17 +43,32 @@ 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)
|
||||
def search(q, options={})
|
||||
@search_definition = Elasticsearch::DSL::Search.search do
|
||||
|
||||
query do
|
||||
unless q.blank?
|
||||
multi_match do
|
||||
query q
|
||||
fields ["name", "content"]
|
||||
filtered do
|
||||
|
||||
query do
|
||||
# search query
|
||||
unless q.blank?
|
||||
multi_match do
|
||||
query q
|
||||
fields ["name", "content"]
|
||||
end
|
||||
else
|
||||
match_all
|
||||
end
|
||||
end
|
||||
else
|
||||
match_all
|
||||
|
||||
# filters
|
||||
filter do
|
||||
bool do
|
||||
must { term paper_type: options[:paper_type] } if options[:paper_type]
|
||||
must { match_all } unless options[:paper_type]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -70,7 +85,7 @@ class Paper < ActiveRecord::Base
|
|||
end
|
||||
|
||||
end
|
||||
puts @search_definition.to_hash
|
||||
Rails.logger.debug "Query: #{@search_definition.to_json}"
|
||||
__elasticsearch__.search(@search_definition)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue