Sha256: 0d2fb25b9d9baeecda1413045ffcab21bcea9357471ab2fd9781afefd3ff9a7f

Contents?: true

Size: 1.78 KB

Versions: 47

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true
module Valkyrie::Persistence::Solr::Queries
  # Responsible for efficently returning all {Valkyrie::Resource}s which are referenced in
  # a given {Valkyrie::Resource}'s property.
  class FindReferencesQuery
    attr_reader :resource, :property, :connection, :resource_factory

    # @param [Valkyrie::Resource] resource
    # @param [String] property resource property referencing other resources
    # @param [RSolr::Client] connection
    # @param [ResourceFactory] resource_factory
    def initialize(resource:, property:, connection:, resource_factory:)
      @resource = resource
      @property = property
      @connection = connection
      @resource_factory = resource_factory
    end

    # Iterate over each Solr Document and convert each Document into a Valkyrie Resource
    # @return [Array<Valkyrie::Resource>]
    def run
      enum_for(:each)
    end

    # Queries for all Documents in the Solr index
    # For each Document, it yields the Valkyrie Resource which was converted from it
    # @yield [Valkyrie::Resource]
    def each
      docs = DefaultPaginator.new
      while docs.has_next?
        params = { q: query, defType: 'lucene' }
        result = connection.paginate(docs.next_page, docs.per_page, 'select', params: params)
        docs = result.fetch('response').fetch('docs')
        docs.each do |doc|
          yield resource_factory.to_resource(object: doc)
        end
      end
    end

    # Generate the Solr join query using the id_ssi field
    # @see https://lucene.apache.org/solr/guide/other-parsers.html#join-query-parser
    # @return [String]
    def query
      "{!join from=#{property}_ssim to=join_id_ssi}id:#{id}"
    end

    # Retrieve the string value for the ID
    # @return [String]
    def id
      resource.id.to_s
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

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