Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #572 from samvera-labs/issue/551-ordered-references
Queries on ordered properties should return results in order
- Loading branch information
Showing
5 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
module Valkyrie::Persistence::Solr::Queries | ||
# Responsible for returning all {Valkyrie::Resource}s which are referenced in | ||
# a given {Valkyrie::Resource}'s property, in the order given in that property. | ||
class FindOrderedReferencesQuery | ||
attr_reader :resource, :property, :connection, :resource_factory | ||
def initialize(resource:, property:, connection:, resource_factory:) | ||
@resource = resource | ||
@property = property | ||
@connection = connection | ||
@resource_factory = resource_factory | ||
end | ||
|
||
def run | ||
enum_for(:each) | ||
end | ||
|
||
def each | ||
# map them off of the property to fix solr's deduplication | ||
property_values.map { |id| unordered_members.find { |member| member.id == id } } .each do |value| | ||
yield value | ||
end | ||
end | ||
|
||
def unordered_members | ||
docs.map do |doc| | ||
resource_factory.to_resource(object: doc) | ||
end | ||
end | ||
|
||
def docs | ||
options = { q: query, rows: 1_000_000_000 } | ||
options[:defType] = 'lucene' | ||
result = connection.get("select", params: options) | ||
result.fetch('response').fetch('docs') | ||
end | ||
|
||
def property_values | ||
Array.wrap(resource[property]) | ||
end | ||
|
||
def query | ||
"{!join from=#{property}_ssim to=join_id_ssi}id:#{id}" | ||
end | ||
|
||
def id | ||
resource.id.to_s | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters