Sha256: f69e2b3ce3148683546219bfb9ba12f6b2633b9387c8c4180a4d268eeb702df6

Contents?: true

Size: 1.71 KB

Versions: 46

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Spotlight
  ##
  # Expose Solr index metadata about fields
  class FieldMetadata
    FACET_LIMIT = 20

    include Spotlight::SearchHelper

    attr_reader :exhibit, :repository, :blacklight_config

    def initialize(exhibit, repository, blacklight_config)
      @exhibit = exhibit
      @repository = repository
      @blacklight_config = blacklight_config
    end

    def field(key)
      {
        document_count: document_counts.fetch(field_name(key), 0),
        value_count: terms.fetch(field_name(key), []).length
      }
    end

    def search_params
      search_service.search_builder.merge(rows: 0, 'facet.limit' => FACET_LIMIT + 1)
    end

    private

    def field_name(key)
      if blacklight_config.facet_fields[key]
        blacklight_config.facet_fields[key].field
      else
        key
      end
    end

    def solr_response
      @solr_response ||= repository.search(search_params.merge('facet.query' => facet_fields.map { |_key, fields| "#{fields.field}:[* TO *]" },
                                                               'rows' => 0,
                                                               'facet' => true))
    end

    # This gets the number of *documents* with a field
    def document_counts
      @document_count ||= solr_response.facet_queries.each_with_object({}) do |(k, v), h|
        h[k.split(/:/).first] = v
      end
    end

    def terms
      @terms ||= solr_response.aggregations.each_with_object({}) do |(facet_name, facet), h|
        h[facet_name] = facet.items.map(&:label)
      end
    end

    def facet_fields
      blacklight_config.facet_fields.reject { |_k, v| v.pivot || v.query }
    end

    alias current_exhibit exhibit
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
blacklight-spotlight-4.3.6 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.3.5 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.3.4 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.3.3 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.3.2 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.3.1 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.3.0 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.2.0 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.1.2 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.1.1 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.1.0 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.0.3 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.0.2 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.0.1 app/models/spotlight/field_metadata.rb
blacklight-spotlight-4.0.0 app/models/spotlight/field_metadata.rb
blacklight-spotlight-3.6.0.beta10 app/models/spotlight/field_metadata.rb
blacklight-spotlight-3.6.0.beta9 app/models/spotlight/field_metadata.rb
blacklight-spotlight-3.6.0.beta8 app/models/spotlight/field_metadata.rb
blacklight-spotlight-3.5.0.4 app/models/spotlight/field_metadata.rb
blacklight-spotlight-3.6.0.beta7 app/models/spotlight/field_metadata.rb