Sha256: d96726c60361a60412dfd3ae3477bf31953f864b231c1c6cafd3bbdfcf62afbf

Contents?: true

Size: 1.94 KB

Versions: 15

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true
module Hyrax
  module Statistics
    # An abstract class for running a query against the Solr terms component
    # you must implement `index_key` in the concrete class.
    #
    # WARNING: you must use a term that isn't parsed (i.e. use _sim instead of _tesim)
    class TermQuery
      # @api public
      # @param [Integer] limit - Limit the number of responses
      # @return [Array<Hyrax::Statistics::TermQuery::Result>]
      def self.query(limit: 5)
        new(limit).query
      end

      def initialize(limit = 5)
        @limit = limit
      end

      def query
        term = index_key
        # Grab JSON response (looks like {"terms": {"depositor_tesim": {"mjg36": 3}}} for depositor)
        json = Hyrax::SolrService.get(path: 'terms',
                                      'terms.fl': term,
                                      'terms.sort': 'count',
                                      'terms.limit': @limit,
                                      wt: 'json',
                                      'json.nl': 'map',
                                      omitHeader: 'true')
        unless json
          Rails.logger.error "Unable to reach TermsComponent via Solr connection. Is it enabled in your solr config?"
          return []
        end

        Result.build(json['terms'][term])
      end

      class Result
        # @param [Array<Array>] rows list of of tuples (label, value)
        def self.build(rows)
          rows.map { |row| Result.new(*row) }
        end

        def initialize(label, value)
          @data = { label: label, data: value }
        end

        def label
          @data[:label]
        end

        def value
          @data[:data]
        end

        # This enables hash equivalence
        def ==(other)
          other == @data
        end

        # Allows us to create a Flot charts pie-graph
        def as_json(opts)
          @data.as_json(opts)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
hyrax-3.6.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.5.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.4.2 app/services/hyrax/statistics/term_query.rb
hyrax-4.0.0.beta1 app/services/hyrax/statistics/term_query.rb
hyrax-3.4.1 app/services/hyrax/statistics/term_query.rb
hyrax-3.4.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.3.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.2.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.1.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.0.2 app/services/hyrax/statistics/term_query.rb
hyrax-3.0.1 app/services/hyrax/statistics/term_query.rb
hyrax-3.0.0 app/services/hyrax/statistics/term_query.rb
hyrax-3.0.0.pre.rc4 app/services/hyrax/statistics/term_query.rb
hyrax-3.0.0.pre.rc3 app/services/hyrax/statistics/term_query.rb
hyrax-3.0.0.pre.rc2 app/services/hyrax/statistics/term_query.rb