Class: Blacklight::AbstractSearchBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/blacklight/abstract_search_builder.rb

Overview

Blacklight’s SearchBuilder converts blacklight request parameters into query parameters appropriate for search index. It does so by evaluating a chain of processing methods to populate a result hash (see #to_hash).

Direct Known Subclasses

FacetSearchBuilder, SearchBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ AbstractSearchBuilder #initialize(processor_chain, scope) ⇒ AbstractSearchBuilder

Returns a new instance of AbstractSearchBuilder.

Overloads:

  • #initialize(scope) ⇒ AbstractSearchBuilder

    Parameters:

    • scope (Object)

      scope the scope where the filter methods reside in.

  • #initialize(processor_chain, scope) ⇒ AbstractSearchBuilder

    Parameters:

    • processor_chain (List<Symbol>, TrueClass)

      options a list of filter methods to run or true, to use the default methods

    • scope (Object)

      the scope where the filter methods reside in.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blacklight/abstract_search_builder.rb', line 19

def initialize(*options)
  case options.size
  when 1
    @processor_chain = default_processor_chain.dup
    @scope = options.first
  when 2
    @processor_chain, @scope = options
  else
    raise ArgumentError, "wrong number of arguments. (#{options.size} for 1..2)"
  end

  @blacklight_params = {}
  search_state_class = @scope.try(:search_state_class) || Blacklight::SearchState
  @search_state = search_state_class.new(@blacklight_params, @scope&.blacklight_config, @scope)
  @additional_filters = {}
  @merged_params = {}
  @reverse_merged_params = {}
end

Instance Attribute Details

#blacklight_paramsObject (readonly)

Returns the value of attribute blacklight_params.



12
13
14
# File 'lib/blacklight/abstract_search_builder.rb', line 12

def blacklight_params
  @blacklight_params
end

#processor_chainObject (readonly)

Returns the value of attribute processor_chain.



12
13
14
# File 'lib/blacklight/abstract_search_builder.rb', line 12

def processor_chain
  @processor_chain
end

#search_stateObject (readonly)

Returns the value of attribute search_state.



12
13
14
# File 'lib/blacklight/abstract_search_builder.rb', line 12

def search_state
  @search_state
end

Instance Method Details

#facet(value = nil) ⇒ Object

Parameters:

  • value (Object) (defaults to: nil)


56
57
58
59
60
61
62
# File 'lib/blacklight/abstract_search_builder.rb', line 56

def facet(value = nil)
  if value
    self.facet = value
    return self
  end
  @facet
end

#facet=(value) ⇒ Object

sets the facet that this query pertains to, for the purpose of facet pagination



50
51
52
53
# File 'lib/blacklight/abstract_search_builder.rb', line 50

def facet=(value)
  params_will_change!
  @facet = value
end

#merge(extra_params) ⇒ Object

Merge additional, repository-specific parameters



66
67
68
69
70
71
72
# File 'lib/blacklight/abstract_search_builder.rb', line 66

def merge(extra_params, &)
  if extra_params
    params_will_change!
    @merged_params.merge!(extra_params.to_hash, &)
  end
  self
end

#reverse_merge(extra_params) ⇒ Object

“Reverse merge” additional, repository-specific parameters



76
77
78
79
80
81
82
# File 'lib/blacklight/abstract_search_builder.rb', line 76

def reverse_merge(extra_params, &)
  if extra_params
    params_will_change!
    @reverse_merged_params.reverse_merge!(extra_params.to_hash, &)
  end
  self
end

#to_hashBlacklight::Solr::Response Also known as: query, to_h

a solr query method

Returns:



88
89
90
91
92
93
94
95
# File 'lib/blacklight/abstract_search_builder.rb', line 88

def to_hash
  return @params unless params_need_update?

  @params = processed_parameters
            .reverse_merge(@reverse_merged_params)
            .merge(@merged_params)
            .tap { clear_changes }
end

#with(blacklight_params_or_search_state = {}) ⇒ Object

Set the parameters to pass through the processor chain



42
43
44
45
46
47
# File 'lib/blacklight/abstract_search_builder.rb', line 42

def with(blacklight_params_or_search_state = {})
  params_will_change!
  @search_state = blacklight_params_or_search_state.is_a?(Blacklight::SearchState) ? blacklight_params_or_search_state : @search_state.reset(blacklight_params_or_search_state)
  @blacklight_params = @search_state.params.dup
  self
end