From 0598e0068e545a5be9bbb501b0155c8bb2d9afb5 Mon Sep 17 00:00:00 2001 From: Bilel Kihal <61744974+Bilelkihal@users.noreply.github.com> Date: Mon, 14 Oct 2024 10:57:42 +0200 Subject: [PATCH] Feature: Aggretate federated search results (#18) * handle ssl error exception when federated call to a collection * federate search endpoint and merge results * fix activesupport gem version issue * fix search params to keep federated and non federated calls work * put collections mergin only for class search * pass federation errors in federated search results * remove unused parallel dependency in class.rb * clean class.rb federation code * update the activesupport gem version * remove uncompleted federated search test * use the key "collection" instead of "results" in federated search result hash to maintain the endpoint expected schema * add test for federated search --- Gemfile.lock | 1 + lib/ontologies_api_client/models/class.rb | 22 ++++++++++++++++++- .../request_federation.rb | 2 ++ test/models/test_federation.rb | 19 +++++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index dd5fb94..b147d35 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -73,6 +73,7 @@ GEM hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS + ruby x86_64-darwin-21 x86_64-darwin-23 x86_64-linux diff --git a/lib/ontologies_api_client/models/class.rb b/lib/ontologies_api_client/models/class.rb index e249887..6362c98 100644 --- a/lib/ontologies_api_client/models/class.rb +++ b/lib/ontologies_api_client/models/class.rb @@ -1,11 +1,14 @@ require "cgi" require_relative "../base" +require_relative "../request_federation" module LinkedData module Client module Models + class Class < LinkedData::Client::Base HTTP = LinkedData::Client::HTTP + include LinkedData::Client::RequestFederation @media_type = %w[http://www.w3.org/2002/07/owl#Class http://www.w3.org/2004/02/skos/core#Concept] @include_attrs = "prefLabel,definition,synonym,obsolete,hasChildren,inScheme,memberOf" @include_attrs_full = "prefLabel,definition,synonym,obsolete,properties,hasChildren,childre,inScheme,memberOf" @@ -61,10 +64,27 @@ def self.find(id, ontology, params = {}) def self.search(*args) query = args.shift + params = args.shift || {} + params[:q] = query + raise ArgumentError, "You must provide a search query: Class.search(query: 'melanoma')" if query.nil? || !query.is_a?(String) - HTTP.post("/search", params) + + + search_result = federated_get(params) do |url| + "#{url}/search" + end + merged_collections = {collection: [], errors: []} + search_result.each do |result| + if result.collection + merged_collections[:collection].concat(result.collection) + elsif result.errors + merged_collections[:errors] << result.errors + end + end + merged_collections + end def expanded? diff --git a/lib/ontologies_api_client/request_federation.rb b/lib/ontologies_api_client/request_federation.rb index ff1c9e3..b57d3db 100644 --- a/lib/ontologies_api_client/request_federation.rb +++ b/lib/ontologies_api_client/request_federation.rb @@ -25,6 +25,8 @@ def federated_get(params = {}, &link) connections.flatten end + + def request_portals(params = {}) federate = params.delete(:federate) || ::RequestStore.store[:federated_portals] diff --git a/test/models/test_federation.rb b/test/models/test_federation.rb index 4890af5..600f2bf 100644 --- a/test/models/test_federation.rb +++ b/test/models/test_federation.rb @@ -119,4 +119,21 @@ def test_federation_ssl_error refute_nil ontologies_federate_one.first.errors WebMock.disable! end -end \ No newline at end of file + + def test_federated_search + query = 'test' + + time1 = Benchmark.realtime do + @search_results = LinkedData::Client::Models::Class.search(query)[:collection] + end + + time2 = Benchmark.realtime do + @federated_search_results = LinkedData::Client::Models::Class.search(query, {federate: 'true'})[:collection] + end + + puts "Search results: #{@search_results .length} in #{time1}s" + puts "Federated search results: #{@federated_search_results.length} in #{time2}s" + + refute_equal @search_results.length, @federated_search_results.length + end +end