Class: Blacklight::SearchBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/blacklight/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).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SearchBuilder.

Overloads:

  • #initialize(scope) ⇒ SearchBuilder

    Parameters:

    • scope (Object)

      the scope where the filter methods reside in.

  • #initialize(processor_chain, scope) ⇒ SearchBuilder

    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/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/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/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/search_builder.rb', line 12

def search_state
  @search_state
end

Instance Method Details

#append(*addl_processor_chain) ⇒ Object

Append additional processor chain directives This is used in blacklight_range_limit



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/blacklight/search_builder.rb', line 63

def append(*addl_processor_chain)
  params_will_change!
  builder = self.class.new(processor_chain + addl_processor_chain, scope)
                .with(search_state)
                .merge(@merged_params)
                .reverse_merge(@reverse_merged_params)

  builder.start = @start if @start
  builder.rows  = @rows if @rows
  builder.page  = @page if @page
  builder.facet = @facet if @facet
  builder
end

#except(*except_processor_chain) ⇒ Object

Converse to append, remove processor chain directives, returning a new builder that’s a copy of receiver with specified change.

Methods in argument that aren’t currently in processor chain are ignored as no-ops, rather than raising.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/blacklight/search_builder.rb', line 84

def except(*except_processor_chain)
  builder = self.class.new(processor_chain - except_processor_chain, scope)
                .with(search_state)
                .merge(@merged_params)
                .reverse_merge(@reverse_merged_params)

  builder.start = @start if @start
  builder.rows  = @rows if @rows
  builder.page  = @page if @page
  builder.facet = @facet if @facet
  builder
end

#facet(value = nil) ⇒ Object

Parameters:

  • value (Object) (defaults to: nil)


220
221
222
223
224
225
226
# File 'lib/blacklight/search_builder.rb', line 220

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



214
215
216
217
# File 'lib/blacklight/search_builder.rb', line 214

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

#facet_suggestion_query(value = nil) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/blacklight/search_builder.rb', line 233

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

#facet_suggestion_query=(value) ⇒ Object



228
229
230
231
# File 'lib/blacklight/search_builder.rb', line 228

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

#merge(extra_params) ⇒ Object

Merge additional, repository-specific parameters



99
100
101
102
103
104
105
# File 'lib/blacklight/search_builder.rb', line 99

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

#page(value = nil) ⇒ Object

Parameters:

  • value (#to_i) (defaults to: nil)


184
185
186
187
188
189
190
# File 'lib/blacklight/search_builder.rb', line 184

def page(value = nil)
  if value
    self.page = value
    return self
  end
  @page ||= search_state.page
end

#page=(value) ⇒ Object



177
178
179
180
181
# File 'lib/blacklight/search_builder.rb', line 177

def page=(value)
  params_will_change!
  @page = value.to_i
  @page = 1 if @page < 1
end

#processed_parametersObject

The CatalogController #index action uses this. Solr parameters can come from a number of places. From lowest precedence to highest:

1. General defaults in blacklight config (are trumped by)
2. defaults for the particular search field identified by  params[:search_field] (are trumped by)
3. certain parameters directly on input HTTP query params
   * not just any parameter is grabbed willy nilly, only certain ones are allowed by HTTP input)
   * for legacy reasons, qt in http query does not over-ride qt in search field definition default.
4.  extra parameters passed in as argument.

spellcheck.q will be supplied with the [:q] value unless specifically specified otherwise.

Incoming parameter :f is mapped to :fq solr parameter.

Returns:

  • a params hash for searching solr.



149
150
151
152
153
154
155
# File 'lib/blacklight/search_builder.rb', line 149

def processed_parameters
  request.tap do |request_parameters|
    processor_chain.each do |method_name|
      send(method_name, request_parameters)
    end
  end
end

#reverse_merge(extra_params) ⇒ Object

“Reverse merge” additional, repository-specific parameters



109
110
111
112
113
114
115
# File 'lib/blacklight/search_builder.rb', line 109

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

#rows(value = nil) ⇒ Object Also known as: per

Parameters:

  • value (#to_i) (defaults to: nil)


198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/blacklight/search_builder.rb', line 198

def rows(value = nil)
  if value
    self.rows = value
    return self
  end
  @rows ||= begin
    # user-provided parameters should override any default row
    r = search_state.per_page
    # ensure we don't excede the max page size
    r.nil? ? nil : [r, blacklight_config.max_per_page].map(&:to_i).min
  end
end

#rows=(value) ⇒ Object



192
193
194
195
# File 'lib/blacklight/search_builder.rb', line 192

def rows=(value)
  params_will_change!
  @rows = [value, blacklight_config.max_per_page].map(&:to_i).min
end

#sortString

Decode the user provided ‘sort’ parameter into a sort string that can be passed to the search. This sanitizes the input by ensuring only configured search values are passed through to the search.

Returns:

  • (String)

    the field/fields to sort by



245
246
247
# File 'lib/blacklight/search_builder.rb', line 245

def sort
  search_state.sort_field&.sort
end

#start(value = nil) ⇒ Object Also known as: padding

Parameters:

  • value (#to_i) (defaults to: nil)


165
166
167
168
169
170
171
172
173
174
# File 'lib/blacklight/search_builder.rb', line 165

def start(value = nil)
  if value
    self.start = value
    return self
  end
  @start ||= (page - 1) * (rows || 10)
  val = @start || 0
  val = 0 if @start < 0
  val
end

#start=(value) ⇒ Object



159
160
161
162
# File 'lib/blacklight/search_builder.rb', line 159

def start=(value)
  params_will_change!
  @start = value.to_i
end

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

a solr query method

Returns:



121
122
123
124
125
126
127
128
# File 'lib/blacklight/search_builder.rb', line 121

def to_hash
  return @params unless params_need_update?

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

#where(conditions) ⇒ Object

Update the :q (query) parameter

Examples:

search_builder.where(id: [1,2,3]) # produces: q:"{!lucene}id:(1 OR 2 OR 3)"

Parameters:

  • conditions (Hash<Symbol,Object>)

    the field and values to query on



52
53
54
55
56
57
58
# File 'lib/blacklight/search_builder.rb', line 52

def where(conditions)
  params_will_change!
  @search_state = @search_state.reset(@search_state.params.merge(q: conditions))
  @blacklight_params = @search_state.params
  @additional_filters = conditions
  self
end

#with(blacklight_params_or_search_state = {}) ⇒ Object

Set the parameters to pass through the processor chain



40
41
42
43
44
45
# File 'lib/blacklight/search_builder.rb', line 40

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