Skip to content

Commit

Permalink
Fix termnite after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Nurgaliev committed Dec 18, 2023
1 parent ba80fb6 commit d189dde
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/chewy/search/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Request

delegate :hits, :wrappers, :objects, :records, :documents,
:object_hash, :record_hash, :document_hash,
:total, :max_score, :took, :timed_out?, to: :response
:total, :max_score, :took, :timed_out?, :terminated_early?, to: :response
delegate :each, :size, :to_a, :[], to: :wrappers
alias_method :to_ary, :to_a
alias_method :total_count, :total
Expand Down Expand Up @@ -891,7 +891,7 @@ def exists?
def first(limit = UNDEFINED)
request_limit = limit == UNDEFINED ? 1 : limit

if performed? && (request_limit <= size || size == total)
if performed? && (terminated_early? || request_limit <= size || size == total)
limit == UNDEFINED ? wrappers.first : wrappers.first(limit)
else
result = except(EXTRA_STORAGES).limit(request_limit).to_a
Expand Down
7 changes: 7 additions & 0 deletions lib/chewy/search/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def timed_out?
@timed_out ||= @body['timed_out']
end

# Has the request been terminated early?
#
# @return [true, false]
def terminated_early?
@terminated_early ||= @body['terminated_early']
end

# The `suggest` response part. Returns empty hash if suggests
# were not requested.
#
Expand Down
3 changes: 2 additions & 1 deletion lib/chewy/search/scrolling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def scroll_batches(batch_size: Request::DEFAULT_BATCH_SIZE, scroll: Request::DEF
hits = hits.first(last_batch_size) if last_batch_size != 0 && fetched >= total
yield(hits) if hits.present?
scroll_id = result['_scroll_id']
break if fetched >= total

break if result['terminated_early'] || fetched >= total

result = perform_scroll(scroll: scroll, scroll_id: scroll_id)
end
Expand Down

0 comments on commit d189dde

Please sign in to comment.