From 79e7e86a4e6c137da75c94d224f059009050a1fa Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 16 May 2019 12:23:39 +0200 Subject: [PATCH] use batch loader for graphql queries. #274 --- app/graphql/active_record_loader.rb | 6 +++++- app/graphql/types/query_type.rb | 7 +++++-- app/models/concerns/indexable.rb | 4 ++-- app/models/doi.rb | 4 ++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/graphql/active_record_loader.rb b/app/graphql/active_record_loader.rb index a4b40e846..2b937f18c 100644 --- a/app/graphql/active_record_loader.rb +++ b/app/graphql/active_record_loader.rb @@ -6,7 +6,11 @@ def initialize(model) end def perform(ids) - @model.where(id: ids).each { |record| fulfill(record.id, record) } + if @model.name == "Prefix" + @model.where(prefix: ids).each { |record| fulfill(record.id, record) } + else + @model.where(id: ids).each { |record| fulfill(record.id, record) } + end ids.each { |id| fulfill(id, nil) unless fulfilled?(id) } end end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 87d2202ec..28f879232 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -6,7 +6,8 @@ class QueryType < BaseObject end def providers(query: nil) - Provider.query(query, page: { number: 1, size: 250 }).records.to_a + ids = Provider.query(query, page: { number: 1, size: 250 }).results.to_a.map { |p| p.uid } + ElasticsearchLoader.for(Provider).load_many(ids) end field :provider, ProviderType, null: false do @@ -24,7 +25,8 @@ def provider(id:) end def clients(query: nil, year: nil, software: nil) - Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).records.to_a + ids = Client.query(query, year: year, software: software, page: { number: 1, size: 2000 }).results.to_a.map { |p| p.uid } + ElasticsearchLoader.for(Client).load_many(ids) end field :client, ClientType, null: false do @@ -55,6 +57,7 @@ def prefixes(query: nil, first: nil) end def prefix(id:) + #ActiveRecordLoader.for(Prefix).load(id) Prefix.where(prefix: id).first end diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 00e308ad0..5a650aa55 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -58,12 +58,12 @@ def find_by_id(ids, options={}) ids = ids.split(",") if ids.is_a?(String) options[:page] ||= {} options[:page][:number] ||= 1 - options[:page][:size] ||= 25 + options[:page][:size] ||= 2000 options[:sort] ||= { created: { order: "asc" }} __elasticsearch__.search({ from: (options.dig(:page, :number) - 1) * options.dig(:page, :size), - size: options[:size] || 25, + size: options.dig(:page, :size), sort: [options[:sort]], query: { terms: { diff --git a/app/models/doi.rb b/app/models/doi.rb index a652812ad..f2fb3f0d4 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -360,12 +360,12 @@ def self.find_by_id(ids, options={}) options[:page] ||= {} options[:page][:number] ||= 1 - options[:page][:size] ||= 25 + options[:page][:size] ||= 1000 options[:sort] ||= { created: { order: "asc" }} __elasticsearch__.search({ from: (options.dig(:page, :number) - 1) * options.dig(:page, :size), - size: options[:size] || 25, + size: options.dig(:page, :size), sort: [options[:sort]], query: { terms: {