Class: Noiseless::Pagination::SearchPaginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/noiseless/pagination.rb

Overview

Search paginator - builds and executes paginated queries

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, page: 1, per_page: nil) ⇒ SearchPaginator

Returns a new instance of SearchPaginator.



135
136
137
138
139
140
141
142
# File 'lib/noiseless/pagination.rb', line 135

def initialize(model_class, page: 1, per_page: nil)
  @model_class = model_class
  @current_page = [page.to_i, 1].max
  @per_page = [(per_page || DEFAULT_PER_PAGE).to_i, MAX_PER_PAGE].min
  @query_builder = QueryBuilder.new(model_class)
  @executed = false
  @results = nil
end

Instance Attribute Details

#current_pageObject (readonly)

Pagination info



153
154
155
# File 'lib/noiseless/pagination.rb', line 153

def current_page
  @current_page
end

Instance Method Details

#aggregation(name, type) ⇒ Object



237
238
239
240
# File 'lib/noiseless/pagination.rb', line 237

def aggregation(name, type, **)
  @query_builder.aggregation(name, type, **)
  self
end

#aggregationsObject



258
259
260
261
# File 'lib/noiseless/pagination.rb', line 258

def aggregations
  execute_search unless @executed
  @results&.aggregations
end

#eachObject

Enumerable interface



205
206
207
208
209
# File 'lib/noiseless/pagination.rb', line 205

def each(&)
  return enum_for(__method__) unless block_given?

  to_a.each(&)
end

#each_with_hitObject

Records-specific methods



279
280
281
282
283
284
285
286
287
288
# File 'lib/noiseless/pagination.rb', line 279

def each_with_hit(&)
  return enum_for(__method__) unless block_given?

  execute_search unless @executed
  if @results.respond_to?(:each_with_hit)
    @results.each_with_hit(&)
  else
    to_a.each_with_index { |record, index| yield(record, hits[index]) }
  end
end

#empty?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/noiseless/pagination.rb', line 200

def empty?
  size.zero?
end

#filter(field, value) ⇒ Object



227
228
229
230
# File 'lib/noiseless/pagination.rb', line 227

def filter(field, value, **)
  @query_builder.filter(field, value, **)
  self
end

#first_page?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/noiseless/pagination.rb', line 178

def first_page?
  current_page == 1
end

#geo_distance(field, lat:, lon:, distance:) ⇒ Object



242
243
244
245
# File 'lib/noiseless/pagination.rb', line 242

def geo_distance(field, lat:, lon:, distance:, **)
  @query_builder.geo_distance(field, lat: lat, lon: lon, distance: distance, **)
  self
end

#hitsObject



268
269
270
271
# File 'lib/noiseless/pagination.rb', line 268

def hits
  execute_search unless @executed
  @results&.hits || []
end

#last_page?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/noiseless/pagination.rb', line 182

def last_page?
  current_page >= total_pages
end

#lengthObject



196
197
198
# File 'lib/noiseless/pagination.rb', line 196

def length
  size
end

#limit_valueObject



155
156
157
# File 'lib/noiseless/pagination.rb', line 155

def limit_value
  @per_page
end

#map_with_hitObject



290
291
292
293
294
# File 'lib/noiseless/pagination.rb', line 290

def map_with_hit(&)
  return enum_for(__method__) unless block_given?

  each_with_hit.map(&)
end

#match(field, value) ⇒ Object

Query building delegation



217
218
219
220
# File 'lib/noiseless/pagination.rb', line 217

def match(field, value, **)
  @query_builder.match(field, value, **)
  self
end

#multi_match(query, fields) ⇒ Object



222
223
224
225
# File 'lib/noiseless/pagination.rb', line 222

def multi_match(query, fields, **)
  @query_builder.multi_match(query, fields, **)
  self
end

#next_pageObject



170
171
172
# File 'lib/noiseless/pagination.rb', line 170

def next_page
  current_page < total_pages ? current_page + 1 : nil
end

#offset_valueObject



190
191
192
# File 'lib/noiseless/pagination.rb', line 190

def offset_value
  (@current_page - 1) * @per_page
end

#out_of_range?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/noiseless/pagination.rb', line 186

def out_of_range?
  current_page > total_pages
end

#page(num) ⇒ Object



144
145
146
# File 'lib/noiseless/pagination.rb', line 144

def page(num)
  SearchPaginator.new(@model_class, page: num, per_page: @per_page)
end

#pagination_metadataObject

JSON metadata for API responses



297
298
299
300
301
302
303
304
305
306
# File 'lib/noiseless/pagination.rb', line 297

def 
  {
    current_page: current_page,
    per_page: @per_page,
    total_count: total_count,
    total_pages: total_pages,
    next_page: next_page,
    prev_page: prev_page
  }
end

#per(num) ⇒ Object



148
149
150
# File 'lib/noiseless/pagination.rb', line 148

def per(num)
  SearchPaginator.new(@model_class, page: @current_page, per_page: num)
end

#prev_pageObject



174
175
176
# File 'lib/noiseless/pagination.rb', line 174

def prev_page
  current_page > 1 ? current_page - 1 : nil
end

#resultsObject

Response access



253
254
255
256
# File 'lib/noiseless/pagination.rb', line 253

def results
  execute_search unless @executed
  @results
end

#sort(field, direction = :asc) ⇒ Object



232
233
234
235
# File 'lib/noiseless/pagination.rb', line 232

def sort(field, direction = :asc, **)
  @query_builder.sort(field, direction, **)
  self
end

#suggestionsObject



263
264
265
266
# File 'lib/noiseless/pagination.rb', line 263

def suggestions
  execute_search unless @executed
  @results&.suggestions
end

#to_aObject



211
212
213
214
# File 'lib/noiseless/pagination.rb', line 211

def to_a
  execute_search unless @executed
  @results.to_a
end

#tookObject



273
274
275
276
# File 'lib/noiseless/pagination.rb', line 273

def took
  execute_search unless @executed
  @results&.took
end

#total_countObject



159
160
161
162
# File 'lib/noiseless/pagination.rb', line 159

def total_count
  execute_search unless @executed
  @total_count || 0
end

#total_pagesObject



164
165
166
167
168
# File 'lib/noiseless/pagination.rb', line 164

def total_pages
  return 1 if total_count.zero?

  (total_count.to_f / @per_page).ceil
end

#vector(field, embedding) ⇒ Object



247
248
249
250
# File 'lib/noiseless/pagination.rb', line 247

def vector(field, embedding, **)
  @query_builder.vector(field, embedding, **)
  self
end