Sha256: 8ebf760c9625a1aaa80314cb0ab4f5b3e823cb43a7e556a2d69f8cc9fc4c5087

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
valkyrie-1.2.1 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-1.2.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-1.2.0.rc3 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-1.2.0.rc2 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb