Skip to content

Commit

Permalink
Feature: Aggretate federated search results (#18)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Bilelkihal authored Oct 14, 2024
1 parent 15ad7d4 commit 0598e00
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion lib/ontologies_api_client/models/class.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions lib/ontologies_api_client/request_federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def federated_get(params = {}, &link)
connections.flatten
end



def request_portals(params = {})
federate = params.delete(:federate) || ::RequestStore.store[:federated_portals]

Expand Down
19 changes: 18 additions & 1 deletion test/models/test_federation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,21 @@ def test_federation_ssl_error
refute_nil ontologies_federate_one.first.errors
WebMock.disable!
end
end

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

0 comments on commit 0598e00

Please sign in to comment.