From 2ec222281cb9779e4d6c5a5cb83b531f1f4c32ae Mon Sep 17 00:00:00 2001 From: Lars Henrik Mai Date: Mon, 5 Oct 2015 21:12:59 +0200 Subject: [PATCH] Extract search query into own class --- app/models/paper.rb | 54 +------------------------------- app/models/paper_search.rb | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 app/models/paper_search.rb diff --git a/app/models/paper.rb b/app/models/paper.rb index 7c73ea1..d1c0068 100644 --- a/app/models/paper.rb +++ b/app/models/paper.rb @@ -62,59 +62,7 @@ class Paper < ActiveRecord::Base # 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 = Elasticsearch::DSL::Search.search do - - query do - # search query - unless q.blank? - multi_match do - query q - fields ["name", "content"] - end - else - match_all - end - end - - # apply filter after aggregations - post_filter do - bool do - 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) } - end - end - - aggregation :paper_types do - # filter by originator - f = Elasticsearch::DSL::Search::Filters::Bool.new - f.must { match_all } - f.must { term originator: options[:originator] } if options[:originator].present? - filter f.to_hash do - aggregation :paper_types do - terms do - field 'paper_type' - end - end - end - end - - aggregation :originators do - # filter by paper_type - f = Elasticsearch::DSL::Search::Filters::Bool.new - f.must { match_all } - f.must { term paper_type: options[:paper_type] } if options[:paper_type].present? - filter f.to_hash do - aggregation :originators do - terms do - field 'originator' - end - end - end - end - - end + @search_definition = PaperSearch.definition(q, options) Rails.logger.debug "Query: #{@search_definition.to_json}" __elasticsearch__.search(@search_definition) end diff --git a/app/models/paper_search.rb b/app/models/paper_search.rb new file mode 100644 index 0000000..5770292 --- /dev/null +++ b/app/models/paper_search.rb @@ -0,0 +1,64 @@ +class PaperSearch + + def self.definition(q, options={}) + Elasticsearch::DSL::Search.search do + + sort do + if options[:sort_by] == 'date' + by :published_at, order: 'desc' + end + by '_score' + end + + query do + # search query + unless q.blank? + multi_match do + query q + fields ["name", "content"] + end + else + match_all + end + end + + # apply filter after aggregations + post_filter do + bool do + 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) } + end + end + + aggregation :paper_types do + # filter by originator + f = Elasticsearch::DSL::Search::Filters::Bool.new + f.must { match_all } + f.must { term originator: options[:originator] } if options[:originator].present? + filter f.to_hash do + aggregation :paper_types do + terms do + field 'paper_type' + end + end + end + end + + aggregation :originators do + # filter by paper_type + f = Elasticsearch::DSL::Search::Filters::Bool.new + f.must { match_all } + f.must { term paper_type: options[:paper_type] } if options[:paper_type].present? + filter f.to_hash do + aggregation :originators do + terms do + field 'originator' + end + end + end + end + end + end +end