Sha256: c84c4ea3704b2c0e2dc3408ca54e8cbff3b63a63feb6471673942a369556e468

Contents?: true

Size: 1.35 KB

Versions: 43

Compression:

Stored size: 1.35 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 } } .reject(&:nil?).each do |value|
        yield value
      end
    end

    def unordered_members
      @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

43 entries across 43 versions & 1 rubygems

Version Path
valkyrie-3.5.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.4.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.3.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.1.5 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.1.4 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.1.3 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.1.2 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.1.1 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.1.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.3 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.2 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.1 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.0.pre.rc.1 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.0.pre.beta.3 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.0.pre.beta.2 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-3.0.0.pre.beta.1 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-2.2.0 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-2.1.2 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb
valkyrie-2.1.1 lib/valkyrie/persistence/solr/queries/find_ordered_references_query.rb