Skip to content

Commit

Permalink
clean up event aggregations and facets. #503
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 7, 2020
1 parent 590eb8e commit 65a6c0f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 182 deletions.
157 changes: 29 additions & 128 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ def facet_by_resource_type(arr)
end
end

def facet_by_year_month(arr)
arr.map do |hsh|
month = hsh["key_as_string"][5..6].to_i
title = I18n.t("date.month_names")[month] + " " + hsh["key_as_string"][0..3]

{ "id" => hsh["key_as_string"][0..6],
"title" => title,
"count" => hsh["doc_count"],
"sum" => hsh.dig("total_by_year_month", "value") }
end
end

def facet_by_source(arr)
arr.map do |hsh|
{ "id" => hsh["key"],
Expand All @@ -141,170 +129,83 @@ def facet_by_source(arr)
end
end

def facet_citations_by_year(hash)
arr = hash.dig('years', 'buckets').map do |h|
year = h['key']
title = h['key'].to_i.to_s

{
'id' => year,
'title' => title,
'sum' => h.dig('total_by_year', 'value') }
end
{ "count" => hash.dig("sum_distribution", "value"),
"years" => arr }
end

def facet_citations_by_year_v1(hash)
arr = hash.dig('years', 'buckets').map do |h|
year = h["key_as_string"][0..3].to_i
title = h["key_as_string"][0..3]

{
'id' => year,
'title' => title,
'sum' => h.dig('total_by_year', 'value') }
end
{ "count" => hash.dig("sum_distribution", "value"),
"years" => arr }
end

def facet_counts_by_year_month(hash)
arr = hash.dig('year_months', 'buckets').map do |h|
month = h["key_as_string"][5..6].to_i
title = I18n.t("date.month_names")[month] + " " + h["key_as_string"][0..3]

{
"id" => h["key_as_string"][0..6],
'title' => title,
'sum' => h.dig('total_by_year_month', 'value') }
end
{ "count" => hash.dig("sum_distribution", "value"),
"yearMonths" => arr }
end

def facet_by_relation_type(arr)
arr.map do |hsh|
arr = hsh.dig("year_months", "buckets").map do |h|
month = h["key_as_string"][5..6].to_i
title = I18n.t("date.month_names")[month] + " " + h["key_as_string"][0..3]

year_month_arr = hsh.dig("year_month", "buckets").map do |h|
{
"id" => h["key_as_string"][0..6],
"title" => title,
"sum" => h.dig("total_by_year_month", "value") }
"id" => h["key_as_string"],
"title" => h["key_as_string"],
"sum" => h["doc_count"] }
end

{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh.dig("sum_distribution", "value"),
"yearMonths" => arr }
"count" => hsh["doc_count"],
"yearMonths" => year_month_arr }
end
end

def facet_by_relation_type_v1(arr)
arr.map do |hsh|
arr = hsh.dig("year_months", "buckets").map do |h|
month = h["key_as_string"][5..6].to_i
title = I18n.t("date.month_names")[month] + " " + h["key_as_string"][0..3]

year_month_arr = hsh.dig("year_month", "buckets").map do |h|
{
"id" => h["key_as_string"][0..6],
"title" => title,
"sum" => h.dig("total_by_year_month", "value") }
"id" => h["key_as_string"],
"title" => h["key_as_string"],
"sum" => h["doc_count"] }
end

{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh.dig("sum_distribution", "value"),
"year-months" => arr }
"count" => hsh["doc_count"],
"year-months" => year_month_arr }
end
end

def facet_by_citation_type(arr)
arr.map do |hsh|
arr = hsh.dig("year_months", "buckets").map do |h|
month = h["key_as_string"][5..6].to_i
title = I18n.t("date.month_names")[month] + " " + h["key_as_string"][0..3]

year_month_arr = hsh.dig("year_month", "buckets").map do |h|
{
"id" => h["key_as_string"][0..6],
"title" => title,
"sum" => h.dig("total_by_year_month", "value") }
"id" => h["key_as_string"],
"title" => h["key_as_string"],
"sum" => h["doc_count"] }
end

{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh["doc_count"],
"year-months" => arr }
"yearMonths" => year_month_arr }
end
end

def facet_by_pairings(arr)
def facet_by_citation_type_v1(arr)
arr.map do |hsh|
arr = hsh.dig("recipient", "buckets").map do |h|
title = h["key"]
year_month_arr = hsh.dig("year_month", "buckets").map do |h|
{
"id" => h["key"],
"title" => title,
"sum" => h.dig("total", "value") }
"id" => h["key_as_string"],
"title" => h["key_as_string"],
"sum" => h["doc_count"] }
end
arr.reject! {|h| h["id"] == hsh["key"]}

{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh["doc_count"],
"registrants" => arr }
"year-months" => year_month_arr }
end
end

def facet_by_registrants(arr)
arr.map do |hsh|
arr = hsh.dig("year", "buckets").map do |h|
title = h["key_as_string"][0..3]

year_arr = hsh.dig("year", "buckets").map do |h|
{
"id" => h["key_as_string"][0..3],
"title" => title,
"sum" => h.dig("total_by_year", "value") }
"id" => h["key_as_string"],
"title" => h["key_as_string"],
"sum" => h["doc_count"] }
end

{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh["doc_count"],
"years" => arr }
end
end

def facet_by_metric_type(arr)
arr.map do |hsh|
{ "id" => hsh["key"],
"title" => hsh["key"].gsub(/-/, ' ').titleize,
"count" => hsh["doc_count"] }
end
end

def facet_by_dois(arr)
arr.map do |hsh|
arr = hsh.dig("relation_types", "buckets").map do |h|
title = h["key"]
{
"id" => h["key"],
"title" => title,
"sum" => h.dig("total_by_type", "value") }
end
arr.reject! {|h| h["id"] == hsh["key"]}
{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh["doc_count"],
"relationTypes" => arr }
end
end

def facet_citations_by_dois(arr)
arr.map do |hsh|
{ "id" => hsh["key"],
"title" => hsh["key"],
"count" => hsh.dig("total", "value")}
"years" => year_arr }
end
end

Expand Down
17 changes: 6 additions & 11 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,12 @@ def index

render json: EventSerializer.new(results, options).serialized_json, status: :ok
else
aggregations = params.fetch(:aggregations, "") || ""

sources = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.sources.buckets) : nil
prefixes = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil
citation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil
relation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil
registrants = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil
pairings = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil
states = total.positive? && aggregations.include?("state_aggregations") ? facet_by_source(response.response.aggregations.states.buckets) : nil

sources = total.positive? ? facet_by_source(response.response.aggregations.sources.buckets) : nil
prefixes = total.positive? ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil
citation_types = total.positive? ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil
relation_types = total.positive? ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil
registrants = total.positive? ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil

results = response.results

options = {}
Expand All @@ -160,7 +156,6 @@ def index
prefixes: prefixes,
"citationTypes" => citation_types,
"relationTypes" => relation_types,
pairings: pairings,
registrants: registrants,
"states": states,
}.compact
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/old_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ def index
else
sources = total > 0 ? facet_by_source(response.aggregations.sources.buckets) : nil
prefixes = total > 0 ? facet_by_source(response.aggregations.prefixes.buckets) : nil
citation_types = total > 0 ? facet_by_citation_type(response.aggregations.citation_types.buckets) : nil
citation_types = total > 0 ? facet_by_citation_type_v1(response.aggregations.citation_types.buckets) : nil
relation_types = total > 0 ? facet_by_relation_type_v1(response.aggregations.relation_types.buckets) : nil
registrants = total > 0 && params[:extra] ? facet_by_registrants(response.aggregations.registrants.buckets) : nil
pairings = total > 0 && params[:extra] ? facet_by_pairings(response.aggregations.pairings.buckets) : nil
registrants = total > 0 ? facet_by_registrants(response.aggregations.registrants.buckets) : nil

results = response.results

Expand All @@ -148,7 +147,6 @@ def index
prefixes: prefixes,
"citation-types" => citation_types,
"relation-types" => relation_types,
pairings: pairings,
registrants: registrants
}.compact

Expand Down
26 changes: 1 addition & 25 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,6 @@ def find_by_id_list(ids, options={})
})
end

def get_aggregations_hash(options={})
aggregations = options[:aggregations] || ""
return send(:query_aggregations) if aggregations.blank?
aggs = {}
aggregations.split(",").each do |agg|
agg = :query_aggregations if agg.blank? || !respond_to?(agg)
aggs.merge! send(agg)
end
aggs
end

def query(query, options={})
# support scroll api
# map function is small performance hit
Expand Down Expand Up @@ -168,7 +157,7 @@ def query(query, options={})
elsif options[:totals_agg] == "prefix"
aggregations = prefix_aggregations
else
aggregations = get_aggregations_hash(options)
aggregations = query_aggregations
end

options[:page] ||= {}
Expand Down Expand Up @@ -346,16 +335,6 @@ def query(query, options={})
}
end

# Collap results list by unique citations
unique = options[:unique].blank? ? nil : {
field: "citation_id",
inner_hits: {
name: "first_unique_event",
size: 1
},
"max_concurrent_group_searches": 1
}

# three options for going through results are scroll, cursor and pagination
# the default is pagination
# scroll is triggered by the page[scroll] query parameter
Expand All @@ -371,7 +350,6 @@ def query(query, options={})
size: options.dig(:page, :size),
sort: sort,
query: es_query,
collapse: unique,
aggregations: aggregations,
track_total_hits: true
}.compact)
Expand All @@ -386,7 +364,6 @@ def query(query, options={})
search_after: search_after,
sort: sort,
query: es_query,
collapse: unique,
aggregations: aggregations,
track_total_hits: true
}.compact)
Expand All @@ -396,7 +373,6 @@ def query(query, options={})
from: from,
sort: sort,
query: es_query,
collapse: unique,
aggregations: aggregations,
track_total_hits: true
}.compact)
Expand Down
2 changes: 0 additions & 2 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,6 @@ def self.query(query, options={})
aggregations = client_export_aggregations
elsif options[:totals_agg] == "prefix"
aggregations = prefix_aggregations
else
aggregations = get_aggregations_hash(options)
end

# Cursor nav use the search after, this should always be an array of values that match the sort.
Expand Down
Loading

0 comments on commit 65a6c0f

Please sign in to comment.