From 147e44bb2665202d8a7a252561132c6e47ecba79 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Thu, 27 Jun 2024 03:04:22 +0200 Subject: [PATCH] add filter search results attributes by language --- controllers/search_controller.rb | 3 +++ helpers/search_helper.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/controllers/search_controller.rb b/controllers/search_controller.rb index 9f701714..cf2d76c6 100644 --- a/controllers/search_controller.rb +++ b/controllers/search_controller.rb @@ -230,6 +230,9 @@ def process_search(params = nil) doc[:submission] = submission doc[:ontology_rank] = (ontology_rank[doc[:submissionAcronym]] && !ontology_rank[doc[:submissionAcronym]].empty?) ? ontology_rank[doc[:submissionAcronym]][:normalizedScore] : 0.0 doc[:properties] = MultiJson.load(doc.delete(:propertyRaw)) if include_param_contains?(:properties) + + doc = filter_attrs_by_language(doc) + instance = doc[:provisional] ? LinkedData::Models::ProvisionalClass.read_only(doc) : LinkedData::Models::Class.read_only(doc) docs.push(instance) end diff --git a/helpers/search_helper.rb b/helpers/search_helper.rb index 1c8dc431..06e6a78f 100644 --- a/helpers/search_helper.rb +++ b/helpers/search_helper.rb @@ -221,6 +221,37 @@ def add_matched_fields(solr_response, default_match) solr_response["match_types"] = all_matches end + def portal_language + Goo.main_languages.first + end + + def request_language + params['lang'] || params['languages'] || portal_language + end + + + def filter_attrs_by_language(doc) + lang_values = {} + doc.each do |k, v| + attr, lang = k.to_s.split('_') + + next unless lang + + if lang.eql?('none') || request_language.eql?(lang) + lang_values[attr.to_sym] ||= [] + lang_values[attr.to_sym] = lang.eql?('none') ? lang_values[attr.to_sym] + v : v + lang_values[attr.to_sym] + end + end + + lang_values.each do |k, v| + doc[k] = v unless v.empty? + end + + doc[:prefLabel] = doc["prefLabel_#{request_language}".to_sym]&.first || doc[:prefLabel]&.first + doc + end + + # see https://github.com/rsolr/rsolr/issues/101 # and https://github.com/projecthydra/active_fedora/commit/75b4afb248ee61d9edb56911b2ef51f30f1ce17f #