Module: Caoutsearch::Search::SearchMethods

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/caoutsearch/search/search_methods.rb

Constant Summary collapse

UNSCOPE_KEYS =

rubocop:disable Layout/HashAlignment

{
  "search"          => :@search_criteria,
  "search_criteria" => :@search_criteria,
  "limit"           => :@current_limit,
  "per"             => :@current_limit,
  "aggregate"       => :@current_aggregations,
  "aggregations"    => :@current_aggregations,
  "suggest"         => :@current_suggestions,
  "suggestions"     => :@current_suggestions,
  "context"         => :@current_contexts,
  "order"           => :@current_order,
  "page"            => :@current_page,
  "offset"          => :@current_offset,
  "fields"          => :@current_fields,
  "source"          => :@current_source
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_aggregationsObject (readonly)

Returns the value of attribute current_aggregations.



8
9
10
# File 'lib/caoutsearch/search/search_methods.rb', line 8

def current_aggregations
  @current_aggregations
end

#current_contextsObject (readonly)

Returns the value of attribute current_contexts.



8
9
10
# File 'lib/caoutsearch/search/search_methods.rb', line 8

def current_contexts
  @current_contexts
end

#current_fieldsObject (readonly)

Returns the value of attribute current_fields.



8
9
10
# File 'lib/caoutsearch/search/search_methods.rb', line 8

def current_fields
  @current_fields
end

#current_orderObject (readonly)

Returns the value of attribute current_order.



8
9
10
# File 'lib/caoutsearch/search/search_methods.rb', line 8

def current_order
  @current_order
end

#current_sourceObject (readonly)

Returns the value of attribute current_source.



8
9
10
# File 'lib/caoutsearch/search/search_methods.rb', line 8

def current_source
  @current_source
end

#current_suggestionsObject (readonly)

Returns the value of attribute current_suggestions.



8
9
10
# File 'lib/caoutsearch/search/search_methods.rb', line 8

def current_suggestions
  @current_suggestions
end

Instance Method Details

#aggregateObject



45
46
47
# File 'lib/caoutsearch/search/search_methods.rb', line 45

def aggregate(...)
  spawn.aggregate!(...)
end

#aggregate!(*values) ⇒ Object



126
127
128
129
130
# File 'lib/caoutsearch/search/search_methods.rb', line 126

def aggregate!(*values)
  @current_aggregations ||= []
  @current_aggregations += values.flatten
  self
end

#appendObject



79
80
81
# File 'lib/caoutsearch/search/search_methods.rb', line 79

def append(...)
  spawn.append!(...)
end

#append!(hash) ⇒ Object



162
163
164
165
# File 'lib/caoutsearch/search/search_methods.rb', line 162

def append!(hash)
  @append_hash = hash
  self
end

#contextObject



24
25
26
# File 'lib/caoutsearch/search/search_methods.rb', line 24

def context(...)
  spawn.context!(...)
end

#context!(*values) ⇒ Object



100
101
102
103
104
# File 'lib/caoutsearch/search/search_methods.rb', line 100

def context!(*values)
  @current_contexts ||= []
  @current_contexts += values.flatten
  self
end

#current_context?(name) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/caoutsearch/search/search_methods.rb', line 217

def current_context?(name)
  @current_contexts&.map(&:to_s)&.include?(name.to_s)
end

#current_limitObject



203
204
205
# File 'lib/caoutsearch/search/search_methods.rb', line 203

def current_limit
  @current_limit&.to_i || 10
end

#current_offsetObject



207
208
209
210
211
212
213
214
215
# File 'lib/caoutsearch/search/search_methods.rb', line 207

def current_offset
  if @current_offset
    @current_offset.to_i
  elsif @current_page
    (current_limit * (current_page - 1))
  else
    0
  end
end

#current_pageObject



199
200
201
# File 'lib/caoutsearch/search/search_methods.rb', line 199

def current_page
  @current_page&.to_i || 1
end

#fieldsObject Also known as: returns



53
54
55
# File 'lib/caoutsearch/search/search_methods.rb', line 53

def fields(...)
  spawn.fields!(...)
end

#fields!(*values) ⇒ Object



140
141
142
143
144
# File 'lib/caoutsearch/search/search_methods.rb', line 140

def fields!(*values)
  @current_fields ||= []
  @current_fields += values.flatten
  self
end

#find_criterion(key) ⇒ Object

Criteria handlers




223
224
225
226
227
228
# File 'lib/caoutsearch/search/search_methods.rb', line 223

def find_criterion(key)
  key = key.to_s
  search_criteria.find do |value|
    return value[key] if value.is_a?(Hash) && value.key?(key)
  end
end

#has_criterion?(key) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
233
234
235
# File 'lib/caoutsearch/search/search_methods.rb', line 230

def has_criterion?(key)
  key = key.to_s
  search_criteria.any? do |value|
    return true if value.is_a?(Hash) && value.key?(key)
  end
end

#limitObject Also known as: per



36
37
38
# File 'lib/caoutsearch/search/search_methods.rb', line 36

def limit(...)
  spawn.limit!(...)
end

#limit!(value) ⇒ Object



116
117
118
119
# File 'lib/caoutsearch/search/search_methods.rb', line 116

def limit!(value)
  @current_limit = value
  self
end

#offsetObject



41
42
43
# File 'lib/caoutsearch/search/search_methods.rb', line 41

def offset(...)
  spawn.offset!(...)
end

#offset!(value) ⇒ Object



121
122
123
124
# File 'lib/caoutsearch/search/search_methods.rb', line 121

def offset!(value)
  @current_offset = value
  self
end

#orderObject



28
29
30
# File 'lib/caoutsearch/search/search_methods.rb', line 28

def order(...)
  spawn.order!(...)
end

#order!(value) ⇒ Object



106
107
108
109
# File 'lib/caoutsearch/search/search_methods.rb', line 106

def order!(value)
  @current_order = value
  self
end

#pageObject



32
33
34
# File 'lib/caoutsearch/search/search_methods.rb', line 32

def page(...)
  spawn.page!(...)
end

#page!(value) ⇒ Object



111
112
113
114
# File 'lib/caoutsearch/search/search_methods.rb', line 111

def page!(value)
  @current_page = value
  self
end

#prependObject



75
76
77
# File 'lib/caoutsearch/search/search_methods.rb', line 75

def prepend(...)
  spawn.prepend!(...)
end

#prepend!(hash) ⇒ Object



157
158
159
160
# File 'lib/caoutsearch/search/search_methods.rb', line 157

def prepend!(hash)
  @prepend_hash = hash
  self
end

#searchObject



20
21
22
# File 'lib/caoutsearch/search/search_methods.rb', line 20

def search(...)
  spawn.search!(...)
end

#search!(*values) ⇒ Object

Setters




89
90
91
92
93
94
95
96
97
98
# File 'lib/caoutsearch/search/search_methods.rb', line 89

def search!(*values)
  values = values.flatten.filter_map do |value|
    value = value.stringify_keys if value.is_a?(Hash)
    value
  end

  @search_criteria ||= []
  @search_criteria += values
  self
end

#search_criteriaObject

Getters and predicates




195
196
197
# File 'lib/caoutsearch/search/search_methods.rb', line 195

def search_criteria
  @search_criteria ||= []
end

#search_criteria_keysObject



237
238
239
240
241
# File 'lib/caoutsearch/search/search_methods.rb', line 237

def search_criteria_keys
  search_criteria.each_with_object([]) do |criterion, keys|
    keys.concat(criterion.keys) if criterion.is_a?(Hash)
  end
end

#sourceObject Also known as: with_sources



58
59
60
# File 'lib/caoutsearch/search/search_methods.rb', line 58

def source(...)
  spawn.source!(...)
end

#source!(*values) ⇒ Object



146
147
148
149
150
# File 'lib/caoutsearch/search/search_methods.rb', line 146

def source!(*values)
  @current_source ||= []
  @current_source += values.flatten
  self
end

#suggestObject



49
50
51
# File 'lib/caoutsearch/search/search_methods.rb', line 49

def suggest(...)
  spawn.suggest!(...)
end

#suggest!(values, **options) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
# File 'lib/caoutsearch/search/search_methods.rb', line 132

def suggest!(values, **options)
  raise ArgumentError unless values.is_a?(Hash)

  @current_suggestions ||= []
  @current_suggestions << [values, options]
  self
end

#track_total_hitsObject



71
72
73
# File 'lib/caoutsearch/search/search_methods.rb', line 71

def track_total_hits(...)
  spawn.track_total_hits!(...)
end

#track_total_hits!(value = true) ⇒ Object



152
153
154
155
# File 'lib/caoutsearch/search/search_methods.rb', line 152

def track_total_hits!(value = true)
  @track_total_hits = value
  self
end

#unscopeObject



83
84
85
# File 'lib/caoutsearch/search/search_methods.rb', line 83

def unscope(...)
  spawn.unscope!(...)
end

#unscope!(key) ⇒ Object

rubocop:enable Layout/HashAlignment

Raises:

  • (ArgumentError)


186
187
188
189
190
191
# File 'lib/caoutsearch/search/search_methods.rb', line 186

def unscope!(key)
  raise ArgumentError unless (variable = UNSCOPE_KEYS[key.to_s])

  reset_variable(variable)
  self
end

#without_hitsObject



67
68
69
# File 'lib/caoutsearch/search/search_methods.rb', line 67

def without_hits
  spawn.source!(false).limit!(0)
end

#without_sourcesObject



63
64
65
# File 'lib/caoutsearch/search/search_methods.rb', line 63

def without_sources
  spawn.source!(false)
end