Sha256: 13b6008d382dea493692e51302798d674df25b7c6f03dc5e9e586cbeaea21a86

Contents?: true

Size: 1.93 KB

Versions: 14

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

class Blacklight::Solr::InvalidParameter < ArgumentError; end

class Blacklight::Solr::Request < ActiveSupport::HashWithIndifferentAccess
  # @deprecated
  SINGULAR_KEYS = %w(facet fl q qt rows start spellcheck spellcheck.q sort per_page wt hl group defType)

  # @deprecated
  ARRAY_KEYS = %w(facet.field facet.query facet.pivot fq hl.fl)

  def initialize(constructor = {})
    if constructor.is_a?(Hash)
      super()
      update(constructor)
    else
      super(constructor)
    end
  end

  def append_query(query)
    return if query.nil?

    if self['q'] || dig(:json, :query, :bool)
      self[:json] ||= { query: { bool: { must: [] } } }
      self[:json][:query] ||= { bool: { must: [] } }
      self[:json][:query][:bool][:must] << query

      if self['q']
        self[:json][:query][:bool][:must] << self['q']
        delete 'q'
      end
    else
      self['q'] = query
    end
  end

  def append_boolean_query(bool_operator, query)
    return if query.blank?

    self[:json] ||= { query: { bool: { bool_operator => [] } } }
    self[:json][:query] ||= { bool: { bool_operator => [] } }
    self[:json][:query][:bool][bool_operator] ||= []

    if self['q']
      self[:json][:query][:bool][:must] ||= []
      self[:json][:query][:bool][:must] << self['q']
      delete 'q'
    end

    self[:json][:query][:bool][bool_operator] << query
  end

  def append_filter_query(query)
    self['fq'] ||= []
    self['fq'] = Array(self['fq']) if self['fq'].is_a? String

    self['fq'] << query
  end

  def append_facet_fields(values)
    self['facet.field'] ||= []
    self['facet.field'] += Array(values)
  end

  def append_facet_query(values)
    self['facet.query'] ||= []
    self['facet.query'] += Array(values)
  end

  def append_facet_pivot(query)
    self['facet.pivot'] ||= []
    self['facet.pivot'] << query
  end

  def append_highlight_field(query)
    self['hl.fl'] ||= []
    self['hl.fl'] << query
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
blacklight-8.2.2 lib/blacklight/solr/request.rb
blacklight-8.2.1 lib/blacklight/solr/request.rb
blacklight-8.2.0 lib/blacklight/solr/request.rb
blacklight-8.1.0 lib/blacklight/solr/request.rb
blacklight-8.0.1 lib/blacklight/solr/request.rb
blacklight-8.0.0 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta8 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta7 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta6 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta5 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta4 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta3 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta2 lib/blacklight/solr/request.rb
blacklight-8.0.0.beta1 lib/blacklight/solr/request.rb