Class: Blacklight::SearchBuilder

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
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)

      scope 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)

      scope the scope where the filter methods reside in.



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

def initialize(*options)
  @scope = case options.size
  when 1
    options.first
  when 2
    if options.first == true
      Deprecation.warn Blacklight::SearchBuilder, "SearchBuilder#initialize now takes only one parameter, the scope. Passing `true' will be removed in Blacklight 7"
    else
      @processor_chain = options.first
    end
    options.last
  else
    raise ArgumentError, "wrong number of arguments. (#{options.size} for 1..2)"
  end

  @processor_chain ||= default_processor_chain.dup
  @blacklight_params = {}
  @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

#scopeObject (readonly, protected)

Returns the value of attribute scope.



257
258
259
# File 'lib/blacklight/search_builder.rb', line 257

def scope
  @scope
end

Instance Method Details

#append(*addl_processor_chain) ⇒ Object

Append additional processor chain directives



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/blacklight/search_builder.rb', line 59

def append(*addl_processor_chain)
  params_will_change!
  builder = self.class.new(processor_chain + addl_processor_chain, scope)
      .with(blacklight_params)
      .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

#clear_changesObject (protected)



271
272
273
# File 'lib/blacklight/search_builder.rb', line 271

def clear_changes
  @dirty = false
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.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/blacklight/search_builder.rb', line 80

def except(*except_processor_chain)
  builder = self.class.new(processor_chain - except_processor_chain, scope)
      .with(blacklight_params)
      .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)


216
217
218
219
220
221
222
# File 'lib/blacklight/search_builder.rb', line 216

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



210
211
212
213
# File 'lib/blacklight/search_builder.rb', line 210

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

#merge(extra_params, &block) ⇒ Object

Merge additional, repository-specific parameters



95
96
97
98
99
100
101
# File 'lib/blacklight/search_builder.rb', line 95

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

#page(value = nil) ⇒ Object

Parameters:

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


179
180
181
182
183
184
185
# File 'lib/blacklight/search_builder.rb', line 179

def page(value = nil)
  if value
    self.page = value
    return self
  end
  @page ||= blacklight_params[:page].blank? ? 1 : blacklight_params[:page].to_i
end

#page=(value) ⇒ Object



172
173
174
175
176
# File 'lib/blacklight/search_builder.rb', line 172

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

#params_changed?Boolean (protected)

Returns:

  • (Boolean)


263
264
265
# File 'lib/blacklight/search_builder.rb', line 263

def params_changed?
  !!@dirty
end

#params_need_update?Boolean (protected)

Returns:

  • (Boolean)


267
268
269
# File 'lib/blacklight/search_builder.rb', line 267

def params_need_update?
  params_changed? || @params.nil?
end

#params_will_change!Object (protected)



259
260
261
# File 'lib/blacklight/search_builder.rb', line 259

def params_will_change!
  @dirty = true
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.



144
145
146
147
148
149
150
# File 'lib/blacklight/search_builder.rb', line 144

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

#requestObject (protected)



249
250
251
# File 'lib/blacklight/search_builder.rb', line 249

def request
  Blacklight::Solr::Request.new
end

#reverse_merge(extra_params, &block) ⇒ Object

“Reverse merge” additional, repository-specific parameters



105
106
107
108
109
110
111
# File 'lib/blacklight/search_builder.rb', line 105

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

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

Parameters:

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


193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/blacklight/search_builder.rb', line 193

def rows(value = nil)
  if value
    self.rows = value
    return self
  end
  @rows ||= begin
    # user-provided parameters should override any default row
    r = [:rows, :per_page].map {|k| blacklight_params[k] }.reject(&:blank?).first
    r ||= blacklight_config.default_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



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

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

#search_fieldObject



243
244
245
# File 'lib/blacklight/search_builder.rb', line 243

def search_field
  blacklight_config.search_fields[blacklight_params[:search_field]]
end

#should_add_field_to_request?(field_name, field) ⇒ Boolean (protected)

Returns:

  • (Boolean)


253
254
255
# File 'lib/blacklight/search_builder.rb', line 253

def should_add_field_to_request? field_name, field
  field.include_in_request || (field.include_in_request.nil? && blacklight_config.add_field_configuration_to_solr_request)
end

#sortObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/blacklight/search_builder.rb', line 224

def sort
  sort_field = if blacklight_params[:sort].blank?
    # no sort param provided, use default
    blacklight_config.default_sort_field
  else
    # check for sort field key
    blacklight_config.sort_fields[blacklight_params[:sort]]
  end

  field = if sort_field.present?
    sort_field.sort
  else
    # just pass the key through
    blacklight_params[:sort]
  end

  field unless field.blank?
end

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

Parameters:

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


160
161
162
163
164
165
166
167
168
169
# File 'lib/blacklight/search_builder.rb', line 160

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



154
155
156
157
# File 'lib/blacklight/search_builder.rb', line 154

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:



117
118
119
120
121
122
123
# File 'lib/blacklight/search_builder.rb', line 117

def to_hash
  return @params unless params_need_update?
  @params = processed_parameters.
              reverse_merge(@reverse_merged_params).
              merge(@merged_params).
              tap { self.clear_changes }
end

#where(conditions) ⇒ Object

Update the :q (query) parameter



51
52
53
54
55
# File 'lib/blacklight/search_builder.rb', line 51

def where(conditions)
  params_will_change!
  @blacklight_params[:q] = conditions
  self
end

#with(blacklight_params = {}) ⇒ Object

Set the parameters to pass through the processor chain



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

def with(blacklight_params = {})
  params_will_change!
  @blacklight_params = blacklight_params.dup
  self
end