Sha256: d2f6f9c320fab2fddd1b2c78443ee80684dfc1126a99e88be9f68ec61ca8bbce

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # Proxy update requests to Solr and inject spotlight's exhibit
  # specific fields.
  #
  # This is an example of how you could integrate external indexing
  # workflows with exhibit-specific content
  class SolrController < Spotlight::ApplicationController
    include Blacklight::SearchHelper

    before_action :authenticate_user!
    before_action :validate_writable_index!

    load_and_authorize_resource :exhibit, class: Spotlight::Exhibit
    delegate :blacklight_config, to: :current_exhibit

    def update
      authorize! :update_solr, @exhibit

      data = solr_documents

      repository.connection.update params: { commitWithin: 500 }, data: data.to_json, headers: { 'Content-Type' => 'application/json' } unless data.empty?

      if params[:resources_json_upload]
        redirect_back fallback_location: exhibit_resources_path(@exhibit)
      else
        head :ok
      end
    end

    private

    def solr_documents
      req = ActiveSupport::JSON.decode(json_content)

      Array.wrap(req).map do |r|
        custom_field_data = r.dup.extract! @exhibit.custom_fields.pluck(:slug)
        other_field_data = r.except(custom_field_data.keys)

        doc = blacklight_config.document_model.new(other_field_data)

        create_or_update_solr_document_sidecar(doc, r)

        doc.to_solr.merge(@exhibit.solr_data).merge(other_field_data)
      end
    end

    def json_content
      if params[:resources_json_upload]
        params[:resources_json_upload][:json].read
      else
        request.body.read
      end
    end

    def validate_writable_index!
      return if Spotlight::Engine.config.writable_index

      render plain: 'Spotlight is unable to write to solr', status: 409
    end

    def create_or_update_solr_document_sidecar(doc, data)
      return if data.blank?

      sidecar = doc.sidecar(@exhibit)
      sidecar.data = sidecar.data.merge(data)
      sidecar.save

      sidecar
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
blacklight-spotlight-2.13.0 app/controllers/spotlight/solr_controller.rb
blacklight-spotlight-2.12.1 app/controllers/spotlight/solr_controller.rb
blacklight-spotlight-2.12.0 app/controllers/spotlight/solr_controller.rb
blacklight-spotlight-2.11.0 app/controllers/spotlight/solr_controller.rb
blacklight-spotlight-2.10.0 app/controllers/spotlight/solr_controller.rb
blacklight-spotlight-2.9.0 app/controllers/spotlight/solr_controller.rb
blacklight-spotlight-2.8.0 app/controllers/spotlight/solr_controller.rb