Skip to content

Combining Persistence & Indexing

Trey Pendragon edited this page Sep 14, 2021 · 5 revisions

A common use case is to save an object to a canonical datastore, while at the same time indexing the object to keep search functionality current. You can do this using a Valkyrie::Persistence::CompositePersister and giving it your two back-ends, e.g.:

# in config/initializers/valkyrie.rb
Valkyrie::MetadataAdapter.register(
  Valkyrie::AdapterContainer.new(
    persister: Valkyrie::Persistence::CompositePersister.new(
      Valkyrie.config.metadata_adapter.persister,
      Valkyrie::MetadataAdapter.find(:index_solr).persister
    ),
    query_service: Valkyrie.config.metadata_adapter.query_service
  ),
  :my_composite_persister
)

When you save or save all to the composite persister, your resource/s will be saved to both back-ends concurrently.

If you want to write your own adapter, a nice addition to this strategy is to combine the composite persister with a Valkyrie::Persistence::BufferedPersister. This allows you to save all your updates to the datastore while keeping them in memory. When the datastore saves are finished, you can write all the updates at once to your indexer, which can be more efficient. See an example of such a persister in Figgy

Write-Only & Indexing

The default Solr adapter has a "write_only" mode which doesn't map the indexed Solr response back to a resource. This results in a roughly 15% performance improvement and is useful in cases, like this, where you don't need the return value of the persister.

# in config/initializers/valkyrie.rb
Valkyrie::MetadataAdapter.register(
  Valkyrie::AdapterContainer.new(
    persister: Valkyrie::Persistence::CompositePersister.new(
      Valkyrie.config.metadata_adapter.persister,
      Valkyrie::Persistence::Solr::MetadataAdapter.new(
        connection: Blacklight.default_index.connection,
        write_only: true
      )
    ),
    query_service: Valkyrie.config.metadata_adapter.query_service
  ),
  :my_composite_persister
)