Class: Hyrax::Indexers::ResourceIndexer

Inherits:
Object
  • Object
show all
Defined in:
app/indexers/hyrax/indexers/resource_indexer.rb

Overview

Transforms Valkyrie::Resource models to solr-ready key-value hashes. Use #to_solr to retrieve the indexable hash.

The default ResourceIndexer implementation provides minimal indexing for the Valkyrie id and the reserved #created_at and #updated_at attributes.

Custom indexers inheriting from others are responsible for providing a full index hash. A common pattern for doing this is to employ method composition to retrieve the parent’s data, then modify it: def to_solr; super.tap { |index_doc| transform(index_doc) }; end. This technique creates infinitely composible index building behavior, with indexers that can always see the state of the resource and the full current index document.

It’s recommended to never modify the state of resource in an indexer.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:) ⇒ ResourceIndexer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ResourceIndexer.

Parameters:

  • resource (Valkyrie::Resource)


34
35
36
# File 'app/indexers/hyrax/indexers/resource_indexer.rb', line 34

def initialize(resource:)
  @resource = resource
end

Instance Attribute Details

#resourceObject (readonly)



29
30
31
# File 'app/indexers/hyrax/indexers/resource_indexer.rb', line 29

def resource
  @resource
end

Class Method Details

.for(resource:) ⇒ Hyrax::Indexers::ResourceIndexer

Note:

This factory will attempt to return an indexer following a naming convention where the indexer for a resource class is expected to be the class name appended with ‘Indexer’. It will then attempt to select an indexer based on the class of the resource, and will return a default Hyrax::Indexers::ResourceIndexer if an indexer class is otherwise not found.

Returns an instance of Hyrax::Indexers::ResourceIndexer or an inherited class.

Examples:

Hyrax::Indexers::ResourceIndexer.for(resource: Book.new) # => #<BookIndexer ...>

Parameters:

  • resource (Valkyrie::Resource)

    an instance of a Valkyrie::Resource or an inherited class

Returns:



77
78
79
80
81
82
# File 'app/indexers/hyrax/indexers/resource_indexer.rb', line 77

def for(resource:)
  klass = "#{resource.class.name}Indexer".safe_constantize
  klass = nil unless klass.is_a?(Class) && klass.instance_methods.include?(:to_solr)
  klass ||= Hyrax::Indexers::ResourceIndexer(resource.class)
  klass.new(resource: resource)
end

Instance Method Details

#generate_solr_documentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

provided for ActiveFedora compatibility.



57
58
59
# File 'app/indexers/hyrax/indexers/resource_indexer.rb', line 57

def generate_solr_document
  to_solr.stringify_keys
end

#to_solrHashWithIndifferentAccess<Symbol, Object>

Returns:

  • (HashWithIndifferentAccess<Symbol, Object>)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/indexers/hyrax/indexers/resource_indexer.rb', line 41

def to_solr
  {
    "id": resource.id.to_s,
    "date_uploaded_dtsi": resource.created_at,
    "date_modified_dtsi": resource.updated_at,
    "system_create_dtsi": resource.created_at,
    "system_modified_dtsi": resource.updated_at,
    "has_model_ssim": resource.to_rdf_representation,
    "human_readable_type_tesim": resource.human_readable_type,
    "alternate_ids_sim": resource.alternate_ids.map(&:to_s)
  }.with_indifferent_access
end