Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Aggretate federated search results #18

Merged
merged 16 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
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
Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
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

Bilelkihal marked this conversation as resolved.
Show resolved Hide resolved
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
Loading