Skip to content

Custom Indexing

Trey Pendragon edited this page Dec 7, 2018 · 2 revisions

In your config/initializer/valkyrie.rb set a custom resource_indexer like this:

  Valkyrie::MetadataAdapter.register(
    Valkyrie::Persistence::Solr::MetadataAdapter.new(
      connection: Blacklight.default_index.connection,
      resource_indexer: CompositeIndexer.new(
        Valkyrie::Indexers::AccessControlsIndexer,
        CollectionIndexer,
        MemberOfIndexer,
        FacetIndexer,
        ProjectIndexer,
        HumanReadableTypeIndexer,
        ImportedMetadataIndexer
      )
    ),
    :index_solr
  )

Then your HumanReadableTypeIndexer must provide a #to_solr method:

# frozen_string_literal: true
class HumanReadableTypeIndexer
  attr_reader :resource
  def initialize(resource:)
    @resource = resource
  end

  def to_solr
    return {} unless decorated_resource.try(:human_readable_type)
    {
      human_readable_type_ssim: decorated_resource.human_readable_type
    }
  end

  def decorated_resource
    @decorated_resource ||= resource.decorate
  end
end