From 56663cc1e0cdc8a82c2cb59a81b67fd578c5454a Mon Sep 17 00:00:00 2001 From: jrhoads Date: Tue, 7 Jun 2022 17:23:12 -0400 Subject: [PATCH 1/2] Fix Repository facets --- app/graphql/types/base_connection.rb | 10 ++++++ .../repository_connection_with_total_type.rb | 2 +- app/models/reference_repository.rb | 32 ++++++++++++++++--- spec/graphql/types/repository_type_spec.rb | 8 ++--- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/app/graphql/types/base_connection.rb b/app/graphql/types/base_connection.rb index 0bea8bb1b..191891f68 100644 --- a/app/graphql/types/base_connection.rb +++ b/app/graphql/types/base_connection.rb @@ -89,6 +89,16 @@ def facet_by_key(arr) end end + def facet_by_key_raw(arr) + arr.map do |hsh| + { + "id" => hsh["key"], + "title" => hsh["key"], + "count" => hsh["doc_count"], + } + end + end + def facet_by_bool(arr) arr.map do |hsh| id = hsh["key"] == 1 ? "true" : "false" diff --git a/app/graphql/types/repository_connection_with_total_type.rb b/app/graphql/types/repository_connection_with_total_type.rb index 28bf19a70..c5e79c53f 100644 --- a/app/graphql/types/repository_connection_with_total_type.rb +++ b/app/graphql/types/repository_connection_with_total_type.rb @@ -28,7 +28,7 @@ def repository_types end def certificates - facet_by_key(object.aggregations.certificates.buckets) + facet_by_key_raw(object.aggregations.certificates.buckets) end def members diff --git a/app/models/reference_repository.rb b/app/models/reference_repository.rb index fa43da61d..4f093e342 100644 --- a/app/models/reference_repository.rb +++ b/app/models/reference_repository.rb @@ -92,7 +92,24 @@ def as_indexed_json(_options = {}) ReferenceRepositoryDenormalizer.new(self).to_hash end - settings index: { number_of_shards: 1 } do + settings index: { + number_of_shards: 1, + analysis: { + analyzer: { + string_lowercase: { + tokenizer: "keyword", filter: %w[lowercase ascii_folding] + }, + }, + normalizer: { + keyword_lowercase: { type: "custom", filter: %w[lowercase] }, + }, + filter: { + ascii_folding: { + type: "asciifolding", preserve_original: true + }, + }, + }, + } do mapping dynamic: "false" do indexes :id indexes :uid, type: :text, @@ -132,7 +149,14 @@ def as_indexed_json(_options = {}) indexes :provider_type, type: :keyword indexes :repository_type, type: :keyword indexes :data_upload_licenses, type: :keyword - indexes :software, type: :keyword + indexes :software, + type: :text, + fields: { + keyword: { type: "keyword" }, + raw: { + type: "text", analyzer: "string_lowercase", "fielddata": true + }, + } indexes :subject, type: :object, properties: { text: { type: :keyword }, @@ -169,7 +193,7 @@ def query_aggregations(facet_count: 10) }, software: { terms: { - field: "software", + field: "software.keyword", size: facet_count, min_doc_count: 1 }, @@ -315,7 +339,7 @@ def filter(options) end if options[:software].present? retval << { terms: { - "software": options[:software].split(",") + "software.raw": options[:software].split(",") } } end if options[:certificate].present? diff --git a/spec/graphql/types/repository_type_spec.rb b/spec/graphql/types/repository_type_spec.rb index d402e2062..13d83e1a0 100644 --- a/spec/graphql/types/repository_type_spec.rb +++ b/spec/graphql/types/repository_type_spec.rb @@ -159,9 +159,9 @@ expect( response.dig("data", "repositories", "certificates"), ).to eq([ - { "count" => 4, "id" => "CoreTrustSeal", "title" => "Core Trust Seal" }, - { "count" => 1, "id" => "DINI Certificate", "title" => "Dini Certificate" }, - { "count" => 1, "id" => "DSA", "title" => "Dsa" } + { "count" => 4, "id" => "CoreTrustSeal", "title" => "CoreTrustSeal" }, + { "count" => 1, "id" => "DINI Certificate", "title" => "DINI Certificate" }, + { "count" => 1, "id" => "DSA", "title" => "DSA" } ]) end @@ -203,7 +203,7 @@ it "filters based on software" do response = LupoSchema.execute( search_query, - variables: { software: "DataVerse" } + variables: { software: "dataverse" } ).as_json expect(response.dig("data", "repositories", "totalCount")).to eq(2) end From 61c75d79e0376c1aaa89344d503b1b625ea01d71 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Wed, 8 Jun 2022 10:28:26 -0400 Subject: [PATCH 2/2] Replace facet_by_key_raw function with parameter on facet_by_key --- app/graphql/types/base_connection.rb | 15 +++------------ .../repository_connection_with_total_type.rb | 2 +- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/app/graphql/types/base_connection.rb b/app/graphql/types/base_connection.rb index 191891f68..a5dc5ee21 100644 --- a/app/graphql/types/base_connection.rb +++ b/app/graphql/types/base_connection.rb @@ -79,21 +79,12 @@ def facet_by_year(arr) end end - def facet_by_key(arr) + def facet_by_key(arr, title_case: true) arr.map do |hsh| + title = title_case ? hsh["key"].titleize : hsh["key"] { "id" => hsh["key"], - "title" => hsh["key"].titleize, - "count" => hsh["doc_count"], - } - end - end - - def facet_by_key_raw(arr) - arr.map do |hsh| - { - "id" => hsh["key"], - "title" => hsh["key"], + "title" => title, "count" => hsh["doc_count"], } end diff --git a/app/graphql/types/repository_connection_with_total_type.rb b/app/graphql/types/repository_connection_with_total_type.rb index c5e79c53f..50146bc8f 100644 --- a/app/graphql/types/repository_connection_with_total_type.rb +++ b/app/graphql/types/repository_connection_with_total_type.rb @@ -28,7 +28,7 @@ def repository_types end def certificates - facet_by_key_raw(object.aggregations.certificates.buckets) + facet_by_key(object.aggregations.certificates.buckets, title_case: false) end def members