fix aggregations to work more like drill down

This commit is contained in:
Lars Henrik Mai 2015-06-22 22:08:27 +02:00 committed by Andreas Haller
parent 2b7ab65432
commit d27f78afba

View file

@ -47,42 +47,52 @@ class Paper < ActiveRecord::Base
@search_definition = Elasticsearch::DSL::Search.search do @search_definition = Elasticsearch::DSL::Search.search do
query do query do
filtered do # search query
unless q.blank?
query do multi_match do
# search query query q
unless q.blank? fields ["name", "content"]
multi_match do
query q
fields ["name", "content"]
end
else
match_all
end
end
# filters
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 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
end end
aggregation :paper_types do aggregation :paper_types do
terms do # filter by originator
field 'paper_type' 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
end end
aggregation :originators do aggregation :originators do
terms do # filter by paper_type
field 'originator' 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