mirror of
https://github.com/CodeforLeipzig/stadtratmonitor.git
synced 2025-04-20 07:11:33 +02:00
Use PaperSearch definition in controller
This commit is contained in:
parent
8929f07745
commit
f883df3347
4 changed files with 32 additions and 27 deletions
|
@ -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!
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue