diff --git a/app/graphql/types/data_catalog_connection_with_total_type.rb b/app/graphql/types/data_catalog_connection_with_total_type.rb index 636b11ea0..6a0fc4653 100644 --- a/app/graphql/types/data_catalog_connection_with_total_type.rb +++ b/app/graphql/types/data_catalog_connection_with_total_type.rb @@ -12,6 +12,6 @@ def total_count end def dataset_connection_count - @dataset_connection_count ||= Doi.query("client.re3data_id:*", page: { number: 1, size: 0 }).results.total + @dataset_connection_count ||= Doi.gql_query("client.re3data_id:*", page: { number: 1, size: 0 }).results.total end end diff --git a/app/graphql/types/data_catalog_type.rb b/app/graphql/types/data_catalog_type.rb index 9b775fb17..e0428e564 100644 --- a/app/graphql/types/data_catalog_type.rb +++ b/app/graphql/types/data_catalog_type.rb @@ -79,7 +79,7 @@ def software_application end def datasets(**args) - Doi.query(args[:query], re3data_id: object[:id], user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], license: args[:license], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Dataset", published: args[:published], state: "findable", page: { cursor: args[:cursor].present? ? Base64.urlsafe_decode64(args[:cursor]) : nil, size: args[:size] }) + Doi.gql_query(args[:query], re3data_id: object[:id], user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], license: args[:license], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], resource_type_id: "Dataset", published: args[:published], state: "findable", page: { cursor: args[:cursor].present? ? Base64.urlsafe_decode64(args[:cursor]) : nil, size: args[:size] }) end def view_count @@ -95,6 +95,6 @@ def citation_count end def response - @response ||= Doi.query(nil, re3data_id: object[:id], state: "findable", page: { number: 1, size: 0 }) + @response ||= Doi.gql_query(nil, re3data_id: object[:id], state: "findable", page: { number: 1, size: 0 }) end end diff --git a/app/graphql/types/doi_item.rb b/app/graphql/types/doi_item.rb index 41e76bba2..76d3347d3 100644 --- a/app/graphql/types/doi_item.rb +++ b/app/graphql/types/doi_item.rb @@ -411,7 +411,7 @@ def response(**args) # make sure no dois are returnded if there are no :ids args[:ids] = "999" if args[:ids].blank? - Doi.query(args[:query], ids: args[:ids], funder_id: args[:funder_id], user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], affiliation_id: args[:affiliation_id], has_person: args[:has_person], has_funder: args[:has_funder], has_organization: args[:has_organization], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], ids: args[:ids], funder_id: args[:funder_id], user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], affiliation_id: args[:affiliation_id], has_person: args[:has_person], has_funder: args[:has_funder], has_organization: args[:has_organization], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end def doi_link(url) diff --git a/app/graphql/types/funder_type.rb b/app/graphql/types/funder_type.rb index 23e2c54ec..639bc62ae 100644 --- a/app/graphql/types/funder_type.rb +++ b/app/graphql/types/funder_type.rb @@ -130,22 +130,22 @@ def works(**args) def view_count args = { first: 0 } r = response(args) - r.results.total.positive? ? aggregate_count(r.response.aggregations.views.buckets) : 0 + aggregate_count(r.response.aggregations.views.buckets) end def download_count args = { first: 0 } r = response(args) - r.results.total.positive? ? aggregate_count(r.response.aggregations.downloads.buckets) : 0 + aggregate_count(r.response.aggregations.downloads.buckets) end def citation_count args = { first: 0 } r = response(args) - r.results.total.positive? ? aggregate_count(r.response.aggregations.citations.buckets) : 0 + aggregate_count(r.response.aggregations.citations.buckets) end def response(**args) - Doi.query(args[:query], ids: args[:ids], funder_id: object.id, user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_organization: args[:has_organization], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], ids: args[:ids], funder_id: object.id, user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_organization: args[:has_organization], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end end diff --git a/app/graphql/types/member_type.rb b/app/graphql/types/member_type.rb index 68819bc44..1baca813c 100644 --- a/app/graphql/types/member_type.rb +++ b/app/graphql/types/member_type.rb @@ -164,12 +164,12 @@ def works(**args) end def prefixes(**args) - response = ProviderPrefix.query(args[:query], provider_id: object.uid, state: args[:state], year: args[:year], page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + response = ProviderPrefix.query(args[:query], provider_id: object.uid, state: args[:state], year: args[:year], page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) ElasticsearchModelResponseConnection.new(response, context: self.context, first: args[:first], after: args[:after]) end def repositories(**args) - response = Client.query(args[:query], provider_id: object.uid, year: args[:year], software: args[:software], page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + response = Client.query(args[:query], provider_id: object.uid, year: args[:year], software: args[:software], page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) ElasticsearchModelResponseConnection.new(response, context: self.context, first: args[:first], after: args[:after]) end @@ -192,6 +192,6 @@ def citation_count end def response(**args) - Doi.query(args[:query], ids: args[:ids], user_id: args[:user_id], client_id: args[:repository_id], provider_id: object.member_type == "consortium" ? nil : object.uid, consortium_id: object.member_type == "consortium" ? object.uid : nil, funder_id: args[:funder_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], has_person: args[:has_person], has_funder: args[:has_funder], has_affiliation: args[:has_affiliation], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], language: args[:language], license: args[:license], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], ids: args[:ids], user_id: args[:user_id], client_id: args[:repository_id], provider_id: object.member_type == "consortium" ? nil : object.uid, consortium_id: object.member_type == "consortium" ? object.uid : nil, funder_id: args[:funder_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], has_person: args[:has_person], has_funder: args[:has_funder], has_affiliation: args[:has_affiliation], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], language: args[:language], license: args[:license], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end end diff --git a/app/graphql/types/organization_type.rb b/app/graphql/types/organization_type.rb index 9e34eea3a..090d88e6a 100644 --- a/app/graphql/types/organization_type.rb +++ b/app/graphql/types/organization_type.rb @@ -153,6 +153,6 @@ def citation_count end def response(**args) - Doi.query(args[:query], ids: args[:ids], affiliation_id: object.id, user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], funder_id: args[:funder_id] || object.fundref.join(","), resource_type_id: args[:resource_type_id], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_funder: args[:has_funder], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], ids: args[:ids], affiliation_id: object.id, user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], funder_id: args[:funder_id] || object.fundref.join(","), resource_type_id: args[:resource_type_id], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_funder: args[:has_funder], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end end diff --git a/app/graphql/types/person_type.rb b/app/graphql/types/person_type.rb index b6e7dafbd..9c3e51f0a 100644 --- a/app/graphql/types/person_type.rb +++ b/app/graphql/types/person_type.rb @@ -142,6 +142,6 @@ def citation_count end def response(**args) - Doi.query(args[:query], ids: args[:ids], user_id: object[:id], client_id: args[:repository_id], provider_id: args[:member_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], agency: args[:registration_agency], has_funder: args[:has_funder], has_affiliation: args[:has_affiliation], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], license: args[:license], language: args[:language], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], ids: args[:ids], user_id: object[:id], client_id: args[:repository_id], provider_id: args[:member_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], agency: args[:registration_agency], has_funder: args[:has_funder], has_affiliation: args[:has_affiliation], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], license: args[:license], language: args[:language], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 7bcd49dff..456d84872 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -1081,7 +1081,7 @@ def usage_report(id:) end def response(**args) - Doi.query(args[:query], ids: args[:ids], user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], resource_type_id: args[:resource_type_id], resource_type: args[:resource_type], published: args[:published], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_funder: args[:has_funder], has_organization: args[:has_organization], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], pid_entity: args[:pid_entity], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], ids: args[:ids], user_id: args[:user_id], client_id: args[:repository_id], provider_id: args[:member_id], resource_type_id: args[:resource_type_id], resource_type: args[:resource_type], published: args[:published], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_funder: args[:has_funder], has_organization: args[:has_organization], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], pid_entity: args[:pid_entity], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end def set_doi(id) diff --git a/app/graphql/types/repository_type.rb b/app/graphql/types/repository_type.rb index 3ace346e3..5b9475cdd 100644 --- a/app/graphql/types/repository_type.rb +++ b/app/graphql/types/repository_type.rb @@ -176,6 +176,6 @@ def citation_count end def response(**args) - Doi.query(args[:query], funder_id: args[:funder_id], user_id: args[:user_id], client_id: object.uid, provider_id: args[:member_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_organization: args[:has_organization], has_funder: args[:has_funder], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : nil, size: args[:first] }) + Doi.gql_query(args[:query], funder_id: args[:funder_id], user_id: args[:user_id], client_id: object.uid, provider_id: args[:member_id], affiliation_id: args[:affiliation_id], resource_type_id: args[:resource_type_id], agency: args[:registration_agency], language: args[:language], license: args[:license], has_person: args[:has_person], has_organization: args[:has_organization], has_funder: args[:has_funder], has_citations: args[:has_citations], has_parts: args[:has_parts], has_versions: args[:has_versions], has_views: args[:has_views], has_downloads: args[:has_downloads], field_of_science: args[:field_of_science], published: args[:published], state: "findable", page: { cursor: args[:after].present? ? Base64.urlsafe_decode64(args[:after]) : [], size: args[:first] }) end end diff --git a/app/models/doi.rb b/app/models/doi.rb index 16bd4ee72..49b4b8e55 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -543,6 +543,62 @@ def as_indexed_json(options={}) } end + def self.gql_query_aggregations + { + resource_types: { terms: { field: 'resource_type_id_and_name', size: 16, min_doc_count: 1 } }, + published: { + date_histogram: { + field: 'publication_year', + interval: 'year', + format: 'year', + order: { + _key: "desc" + }, + min_doc_count: 1 + }, + }, + registration_agencies: { terms: { field: 'agency', size: 10, min_doc_count: 1 } }, + affiliations: { terms: { field: 'affiliation_id_and_name', size: 10, min_doc_count: 1 } }, + pid_entities: { + filter: { term: { "subjects.subjectScheme": "PidEntity" } }, + aggs: { + subject: { terms: { field: 'subjects.subject', size: 10, min_doc_count: 1, + include: %w(Dataset Publication Software Organization Funder Person Grant Sample Instrument Repository Project) } }, + }, + }, + fields_of_science: { + filter: { term: { "subjects.subjectScheme": "Fields of Science and Technology (FOS)" } }, + aggs: { + subject: { terms: { field: 'subjects.subject', size: 10, min_doc_count: 1, + include: "FOS:.*" } }, + }, + }, + licenses: { terms: { field: 'rights_list.rightsIdentifier', size: 10, min_doc_count: 1 } }, + languages: { terms: { field: 'language', size: 10, min_doc_count: 1 } }, + views: { + date_histogram: { field: 'publication_year', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 }, + aggs: { + metric_count: { sum: { field: "view_count" } }, + bucket_truncate: { bucket_sort: { size: 10 } }, + }, + }, + downloads: { + date_histogram: { field: 'publication_year', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 }, + aggs: { + metric_count: { sum: { field: "download_count" } }, + bucket_truncate: { bucket_sort: { size: 10 } }, + }, + }, + citations: { + date_histogram: { field: 'publication_year', interval: 'year', format: 'year', order: { _key: "desc" }, min_doc_count: 1 }, + aggs: { + metric_count: { sum: { field: "citation_count" } }, + bucket_truncate: { bucket_sort: { size: 10 } }, + }, + }, + } + end + def self.query_aggregations { resource_types: { terms: { field: 'resource_type_id_and_name', size: 16, min_doc_count: 1 } }, @@ -716,6 +772,133 @@ def self.stats_query(options={}) }) end + # query for graphql, removing options that are not needed + def self.gql_query(query, options={}) + options[:page] ||= {} + aggregations = gql_query_aggregations + + # cursor nav uses search_after, this should always be an array of values that match the sort. + # make sure we have a valid cursor + cursor = [0, ""] + if options.dig(:page, :cursor).is_a?(Array) + timestamp, uid = options.dig(:page, :cursor) + cursor = [timestamp.to_i, uid.to_s] + elsif options.dig(:page, :cursor).is_a?(String) + timestamp, uid = options.dig(:page, :cursor).split(",") + cursor = [timestamp.to_i, uid.to_s] + end + + from = 0 + search_after = cursor + sort = [{ created: "asc", uid: "asc" }] + + # make sure field name uses underscore + # escape forward slash, but not other Elasticsearch special characters + if query.present? + query = query.gsub(/publicationYear/, "publication_year") + query = query.gsub(/relatedIdentifiers/, "related_identifiers") + query = query.gsub(/rightsList/, "rights_list") + query = query.gsub(/fundingReferences/, "funding_references") + query = query.gsub(/geoLocations/, "geo_locations") + query = query.gsub(/landingPage/, "landing_page") + query = query.gsub(/contentUrl/, "content_url") + query = query.gsub("/", '\/') + end + + # turn ids into an array if provided as comma-separated string + options[:ids] = options[:ids].split(",") if options[:ids].is_a?(String) + + if query.present? + must = [{ query_string: { query: query, fields: query_fields, default_operator: "AND", phrase_slop: 1 } }] + else + must = [{ match_all: {} }] + end + + filter = [] + should = [] + minimum_should_match = 0 + + filter << { terms: { doi: options[:ids].map(&:upcase) }} if options[:ids].present? + filter << { term: { "types.resourceTypeGeneral": options[:resource_type_id].underscore.camelize }} if options[:resource_type_id].present? + filter << { terms: { "types.resourceType": options[:resource_type].split(",") }} if options[:resource_type].present? + filter << { terms: { provider_id: options[:provider_id].split(",") } } if options[:provider_id].present? + filter << { terms: { client_id: options[:client_id].to_s.split(",") } } if options[:client_id].present? + filter << { terms: { agency: options[:agency].split(",").map(&:downcase) } } if options[:agency].present? + filter << { terms: { prefix: options[:prefix].to_s.split(",") } } if options[:prefix].present? + filter << { terms: { language: options[:language].to_s.split(",").map(&:downcase) } } if options[:language].present? + filter << { term: { uid: options[:uid] }} if options[:uid].present? + filter << { range: { created: { gte: "#{options[:created].split(",").min}||/y", lte: "#{options[:created].split(",").max}||/y", format: "yyyy" }}} if options[:created].present? + filter << { range: { publication_year: { gte: "#{options[:published].split(",").min}||/y", lte: "#{options[:published].split(",").max}||/y", format: "yyyy" }}} if options[:published].present? + filter << { term: { schema_version: "http://datacite.org/schema/kernel-#{options[:schema_version]}" }} if options[:schema_version].present? + filter << { terms: { "subjects.subject": options[:subject].split(",") } } if options[:subject].present? + if options[:pid_entity].present? + filter << { term: { "subjects.subjectScheme": "PidEntity" } } + filter << { terms: { "subjects.subject": options[:pid_entity].split(",").map(&:humanize) } } + end + if options[:field_of_science].present? + filter << { term: { "subjects.subjectScheme": "Fields of Science and Technology (FOS)" } } + filter << { terms: { "subjects.subject": options[:field_of_science].split(",").map { |s| "FOS: " + s.humanize } } } + end + filter << { terms: { "rights_list.rightsIdentifier" => options[:license].split(",") } } if options[:license].present? + filter << { term: { source: options[:source] } } if options[:source].present? + filter << { range: { reference_count: { "gte": options[:has_references].to_i } } } if options[:has_references].present? + filter << { range: { citation_count: { "gte": options[:has_citations].to_i } } } if options[:has_citations].present? + filter << { range: { part_count: { "gte": options[:has_parts].to_i } } } if options[:has_parts].present? + filter << { range: { part_of_count: { "gte": options[:has_part_of].to_i } } } if options[:has_part_of].present? + filter << { range: { version_count: { "gte": options[:has_versions].to_i } } } if options[:has_versions].present? + filter << { range: { version_of_count: { "gte": options[:has_version_of].to_i } } } if options[:has_version_of].present? + filter << { range: { view_count: { "gte": options[:has_views].to_i } } } if options[:has_views].present? + filter << { range: { download_count: { "gte": options[:has_downloads].to_i } } } if options[:has_downloads].present? + filter << { term: { "landing_page.status": options[:link_check_status] } } if options[:link_check_status].present? + filter << { exists: { field: "landing_page.checked" }} if options[:link_checked].present? + filter << { term: { "landing_page.hasSchemaOrg": options[:link_check_has_schema_org] }} if options[:link_check_has_schema_org].present? + filter << { term: { "landing_page.bodyHasPid": options[:link_check_body_has_pid] }} if options[:link_check_body_has_pid].present? + filter << { exists: { field: "landing_page.schemaOrgId" }} if options[:link_check_found_schema_org_id].present? + filter << { exists: { field: "landing_page.dcIdentifier" }} if options[:link_check_found_dc_identifier].present? + filter << { exists: { field: "landing_page.citationDoi" }} if options[:link_check_found_citation_doi].present? + filter << { range: { "landing_page.redirectCount": { "gte": options[:link_check_redirect_count_gte] } } } if options[:link_check_redirect_count_gte].present? + filter << { terms: { aasm_state: options[:state].to_s.split(",") }} if options[:state].present? + filter << { range: { registered: { gte: "#{options[:registered].split(",").min}||/y", lte: "#{options[:registered].split(",").max}||/y", format: "yyyy" }}} if options[:registered].present? + filter << { term: { "creators.nameIdentifiers.nameIdentifier" => "https://orcid.org/#{orcid_from_url(options[:user_id])}" }} if options[:user_id].present? + filter << { term: { "creators.nameIdentifiers.nameIdentifierScheme" => "ORCID" }} if options[:has_person].present? + filter << { term: { "creators.affiliation.affiliationIdentifierScheme" => "ROR" }} if options[:has_organization].present? + filter << { term: { "funding_references.funderIdentifierType" => "Crossref Funder ID" }} if options[:has_funder].present? + filter << { term: { consortium_id: options[:consortium_id] }} if options[:consortium_id].present? + # TODO align PID parsing + filter << { term: { "client.re3data_id" => doi_from_url(options[:re3data_id]) }} if options[:re3data_id].present? + filter << { term: { "client.opendoar_id" => options[:opendoar_id] }} if options[:opendoar_id].present? + filter << { terms: { "client.certificate" => options[:certificate].split(",") }} if options[:certificate].present? + + # match either ROR ID or Crossref Funder ID if either affiliation_id or funder_id + # is a query parameter + if options[:affiliation_id].present? + should << { term: { "affiliation_id" => ror_from_url(options[:affiliation_id]) }} + minimum_should_match = 1 + end + if options[:funder_id].present? + should << { terms: { "funding_references.funderIdentifier" => options[:funder_id].split(",").map { |f| "https://doi.org/#{doi_from_url(f)}" } } } + minimum_should_match = 1 + end + + es_query = { + bool: { + must: must, + filter: filter, + should: should, + minimum_should_match: minimum_should_match, + } + } + + __elasticsearch__.search({ + size: options.dig(:page, :size), + search_after: search_after, + sort: sort, + query: es_query, + aggregations: aggregations, + track_total_hits: true + }.compact) + end + def self.query(query, options={}) # support scroll api # map function is small performance hit diff --git a/spec/graphql/types/book_chapter_type_spec.rb b/spec/graphql/types/book_chapter_type_spec.rb index 354e8e3a4..e35ff71ba 100644 --- a/spec/graphql/types/book_chapter_type_spec.rb +++ b/spec/graphql/types/book_chapter_type_spec.rb @@ -14,7 +14,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -51,7 +51,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/book_type_spec.rb b/spec/graphql/types/book_type_spec.rb index ede06a323..319fe0a46 100644 --- a/spec/graphql/types/book_type_spec.rb +++ b/spec/graphql/types/book_type_spec.rb @@ -14,7 +14,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -51,7 +51,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/dataset_type_spec.rb b/spec/graphql/types/dataset_type_spec.rb index 421768d3f..cdd5f2f6d 100644 --- a/spec/graphql/types/dataset_type_spec.rb +++ b/spec/graphql/types/dataset_type_spec.rb @@ -14,7 +14,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -87,7 +87,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do @@ -197,7 +197,7 @@ Doi.import Event.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -255,7 +255,7 @@ Doi.import Event.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -306,7 +306,7 @@ Doi.import Event.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -357,7 +357,7 @@ Doi.import Event.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -408,7 +408,7 @@ Doi.import Event.import sleep 3 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -459,7 +459,7 @@ Doi.import Event.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -516,7 +516,7 @@ Doi.import Event.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/dissertation_type_spec.rb b/spec/graphql/types/dissertation_type_spec.rb index ecd95a9c6..69264353a 100644 --- a/spec/graphql/types/dissertation_type_spec.rb +++ b/spec/graphql/types/dissertation_type_spec.rb @@ -15,7 +15,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do @@ -62,7 +62,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do @@ -117,7 +117,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do @@ -175,7 +175,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/instrument_type_spec.rb b/spec/graphql/types/instrument_type_spec.rb index a55fbd093..9983d30e3 100644 --- a/spec/graphql/types/instrument_type_spec.rb +++ b/spec/graphql/types/instrument_type_spec.rb @@ -14,7 +14,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/journal_article_type_spec.rb b/spec/graphql/types/journal_article_type_spec.rb index 85587bf6d..ee2572f2e 100644 --- a/spec/graphql/types/journal_article_type_spec.rb +++ b/spec/graphql/types/journal_article_type_spec.rb @@ -14,7 +14,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do @@ -51,7 +51,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/peer_review_type_spec.rb b/spec/graphql/types/peer_review_type_spec.rb index c3affd6bb..80a761f5f 100644 --- a/spec/graphql/types/peer_review_type_spec.rb +++ b/spec/graphql/types/peer_review_type_spec.rb @@ -14,7 +14,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do @@ -51,7 +51,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/preprint_type_spec.rb b/spec/graphql/types/preprint_type_spec.rb index 9c0b9563f..0f7885a42 100644 --- a/spec/graphql/types/preprint_type_spec.rb +++ b/spec/graphql/types/preprint_type_spec.rb @@ -15,7 +15,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 4 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 4 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/service_type_spec.rb b/spec/graphql/types/service_type_spec.rb index 9ce37e861..afa7f063a 100644 --- a/spec/graphql/types/service_type_spec.rb +++ b/spec/graphql/types/service_type_spec.rb @@ -37,7 +37,7 @@ Client.import Doi.import sleep 3 - @dois = Doi.query(nil, page: { cursor: [], size: 3 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 3 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/software_type_spec.rb b/spec/graphql/types/software_type_spec.rb index 147925d36..0ca83b83e 100644 --- a/spec/graphql/types/software_type_spec.rb +++ b/spec/graphql/types/software_type_spec.rb @@ -13,7 +13,7 @@ before do Doi.import sleep 2 - @dois = Doi.query(nil, page: { cursor: [], size: 1 }).results.to_a + @dois = Doi.gql_query(nil, page: { cursor: [], size: 1 }).results.to_a end let(:query) do diff --git a/spec/graphql/types/work_type_spec.rb b/spec/graphql/types/work_type_spec.rb index 2c6d477af..bb2c423e2 100644 --- a/spec/graphql/types/work_type_spec.rb +++ b/spec/graphql/types/work_type_spec.rb @@ -135,7 +135,7 @@ before do Doi.import sleep 2 - @works = Doi.query(nil, page: { cursor: [], size: 10 }).results.to_a + @works = Doi.gql_query(nil, page: { cursor: [], size: 10 }).results.to_a end it "returns all works" do @@ -210,7 +210,7 @@ before do Doi.import sleep 2 - @works = Doi.query(nil, page: { cursor: [], size: 11 }).results.to_a + @works = Doi.gql_query(nil, page: { cursor: [], size: 11 }).results.to_a end it "returns all works" do @@ -262,7 +262,7 @@ before do Doi.import sleep 2 - @works = Doi.query(nil, page: { cursor: [], size: 11 }).results.to_a + @works = Doi.gql_query(nil, page: { cursor: [], size: 11 }).results.to_a end it "returns all works" do