Class: Hyrax::ValkyrieIndexer

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

Overview

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

The default ValkyrieIndexer 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_document| transform(index_document) }; 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.

Examples:

defining a custom indexer with composition

class MyIndexer < ValkyrieIndexer
  def to_solr
    super.tap do |index_document|
      index_document[:my_field_tesim]   = resource.my_field.map(&:to_s)
      index_document[:other_field_ssim] = resource.other_field
    end
  end
end

pairing an indexer with a model class

class Book < Hyrax::Resource
  attribute :author
end

# match by name "#{model_class}Indexer"
class BookIndexer < ValkyrieIndexer
  def to_solr
    super.tap do |index_document|
      index_document[:author_si] = resource.author
    end
  end
end

ValkyrieIndexer.for(resource: Book.new) # => #<BookIndexer:0x0000563715a9f1f8 ...>

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:) ⇒ ValkyrieIndexer

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 ValkyrieIndexer.

Parameters:

  • resource (Valkyrie::Resource)

61
62
63
# File 'app/indexers/hyrax/valkyrie_indexer.rb', line 61

def initialize(resource:)
  @resource = resource
end

Instance Attribute Details

#resourceObject (readonly)


56
57
58
# File 'app/indexers/hyrax/valkyrie_indexer.rb', line 56

def resource
  @resource
end

Class Method Details

.for(resource:) ⇒ Valkyrie::Indexer

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 return default Hyrax::ValkyrieIndexer if an indexer class following the naming convention is not found.

Returns an instance of ValkyrieIndexer or an inherited class based on naming convention.

Examples:

ValkyrieIndexer.for(resource: Book.new) # => #<BookIndexer ...>

Parameters:

  • resource (Valkyrie::Resource)

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

Returns:

  • (Valkyrie::Indexer)

    an instance of ValkyrieIndexer or an inherited class based on naming convention


100
101
102
103
104
105
106
107
# File 'app/indexers/hyrax/valkyrie_indexer.rb', line 100

def for(resource:)
  case resource
  when Hyrax::FileSet
    Hyrax::ValkyrieFileSetIndexer.new(resource: resource)
  else
    indexer_class_for(resource).new(resource: resource)
  end
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.


82
83
84
# File 'app/indexers/hyrax/valkyrie_indexer.rb', line 82

def generate_solr_document
  to_solr.stringify_keys
end

#to_solrHashWithIndifferentAccess<Symbol, Object>

Returns:

  • (HashWithIndifferentAccess<Symbol, Object>)

68
69
70
71
72
73
74
75
76
77
# File 'app/indexers/hyrax/valkyrie_indexer.rb', line 68

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.internal_resource
  }.with_indifferent_access
end