From 5f73e52a55f453bd7f8ac2786d17ff42ec59cc11 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Tue, 10 Mar 2020 07:46:57 +0100 Subject: [PATCH] fix too many buckets exception --- app/controllers/export_controller.rb | 8 ++++---- app/models/concerns/indexable.rb | 2 ++ app/models/doi.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index 9b4cf2b27..7fa434178 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -233,7 +233,7 @@ def repositories logger.warn "Exporting #{clients.length} repositories." # Get doi counts via DOIs query and combine next to clients. - response = Doi.query(nil, state: "registered,findable", page: { size: 0, number: 1 }, totals_agg: "client") + response = Doi.query(nil, state: "registered,findable", page: { size: 0, number: 1 }, totals_agg: "client_export") client_totals = {} totals_buckets = response.response.aggregations.clients_totals.buckets @@ -286,9 +286,9 @@ def repositories created: export_date(client.created), modified: export_date(client.updated), deleted: client.deleted_at.present? ? export_date(client.deleted_at) : nil, - doisCountCurrentYear: client_totals[client.uid] ? client_totals[client.uid]["this_year"] : nil, - doisCountPreviousYear: client_totals[client.uid] ? client_totals[client.uid]["last_year"] : nil, - doisCountTotal: client_totals[client.uid] ? client_totals[client.uid]["count"] : nil + doisCountCurrentYear: client_totals[client.uid] ? client_totals[client.uid]["this_year"] : 0, + doisCountPreviousYear: client_totals[client.uid] ? client_totals[client.uid]["last_year"] : 0, + doisCountTotal: client_totals[client.uid] ? client_totals[client.uid]["count"] : 0 }.values csv += CSV.generate_line row diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 5a835b870..334aee0e0 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -136,6 +136,8 @@ def query(query, options={}) aggregations = provider_aggregations elsif options[:totals_agg] == "client" aggregations = client_aggregations + elsif options[:totals_agg] == "client_export" + aggregations = client_export_aggregations elsif options[:totals_agg] == "prefix" aggregations = prefix_aggregations else diff --git a/app/models/doi.rb b/app/models/doi.rb index 66b0d25ee..1312a0e81 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -579,6 +579,10 @@ def self.client_aggregations { clients_totals: { terms: { field: 'client_id', size: ::Client.__elasticsearch__.count, min_doc_count: 1 }, aggs: sub_aggregations } } end + def self.client_export_aggregations + { clients_totals: { terms: { field: 'client_id', size: ::Client.__elasticsearch__.count, min_doc_count: 1 }, aggs: export_sub_aggregations } } + end + def self.prefix_aggregations { prefixes_totals: { terms: { field: 'prefix', size: ::Prefix.count, min_doc_count: 1 }, aggs: sub_aggregations } } end @@ -593,6 +597,14 @@ def self.sub_aggregations } end + def self.export_sub_aggregations + { + this_year: { date_range: { field: 'created', ranges: { from: "now/y", to: "now/d" } } }, + last_year: { date_range: { field: 'created', ranges: { from: "now-1y/y", to: "now/y-1d" } } }, + two_years_ago: { date_range: { field: 'created', ranges: { from: "now-2y/y", to: "now-1y/y-1d" } } } + } + end + def self.query_fields ["uid^50", "related_identifiers.relatedIdentifier^3", "funding_references.relatedIdentifier^3", "container.identifier^3", 'titles.title^3', 'creator_names^3', 'creators.name^3', 'creators.id^3', 'publisher^3', 'descriptions.description^3', 'types.resourceTypeGeneral^3', 'subjects.subject^3', 'client.uid^3', 'provider.uid^3', '_all'] end @@ -667,6 +679,8 @@ def self.query(query, options={}) aggregations = provider_aggregations elsif options[:totals_agg] == "client" aggregations = client_aggregations + elsif options[:totals_agg] == "client_export" + aggregations = client_export_aggregations elsif options[:totals_agg] == "prefix" aggregations = prefix_aggregations else