Class: Blacklight::SearchBuilder

Inherits:
AbstractSearchBuilder 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 AbstractSearchBuilder#to_hash).

Direct Known Subclasses

SearchBuilder

Instance Attribute Summary

Attributes inherited from AbstractSearchBuilder

#blacklight_params, #processor_chain, #search_state

Instance Method Summary collapse

Methods inherited from AbstractSearchBuilder

#facet, #facet=, #initialize, #merge, #reverse_merge, #to_hash, #with

Constructor Details

This class inherits a constructor from Blacklight::AbstractSearchBuilder

Instance Method Details

#append(*addl_processor_chain) ⇒ Object

Append additional processor chain directives



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/blacklight/search_builder.rb', line 24

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.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/blacklight/search_builder.rb', line 45

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

#page(value = nil) ⇒ Object

Parameters:

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


83
84
85
86
87
88
89
# File 'lib/blacklight/search_builder.rb', line 83

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

#page=(value) ⇒ Object



76
77
78
79
80
# File 'lib/blacklight/search_builder.rb', line 76

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

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

Parameters:

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


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/blacklight/search_builder.rb', line 97

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



91
92
93
94
# File 'lib/blacklight/search_builder.rb', line 91

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



116
117
118
# File 'lib/blacklight/search_builder.rb', line 116

def sort
  search_state.sort_field&.sort
end

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

Parameters:

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


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

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



58
59
60
61
# File 'lib/blacklight/search_builder.rb', line 58

def start=(value)
  params_will_change!
  @start = value.to_i
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



14
15
16
17
18
19
20
# File 'lib/blacklight/search_builder.rb', line 14

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