Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the scroll by id after completing scroll_batches #750

Merged
merged 2 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 64 additions & 63 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Bug fixes

* [#695](https://github.com/toptal/chewy/pull/695): Clear the scroll by id after completing scroll_batches ([@socialchorus][])
* [#749](https://github.com/toptal/chewy/pull/749): Avoid importing everything when given an empty relation ([@JF-Lalonde][][@dalthon][])
* [#736](https://github.com/toptal/chewy/pull/736): Fix nil children when using witchcraft ([@taylor-au][])

Expand Down Expand Up @@ -494,83 +495,83 @@
* Basic index handling
* Initial version


[@JF-Lalonde]: https://github.com/JF-Lalonde
[@dalthon]: https://github.com/dalthon
[@taylor-au]: https://github.com/taylor-au
[@rabotyaga]: https://github.com/rabotyaga
[@lowang]: https://github.com/lowang
[@bhacaz]: https://github.com/bhacaz
[@bbatsov]: https://github.com/bbatsov
[@olancheg]: https://github.com/olancheg
[@0x0badc0de]: https://github.com/0x0badc0de
[@aglushkov]: https://github.com/aglushkov
[@gseddon]: https://github.com/gseddon
[@mrzasa]: https://github.com/mrzasa
[@AlexVPopov]: https://github.com/AlexVPopov
[@pyromaniac]: https://github.com/pyromaniac
[@mattzollinhofer]: https://github.com/mattzollinhofer
[@yahooguntu]: https://github.com/yahooguntu
[@dck]: https://github.com/dck
[@dm1try]: https://github.com/dm1try
[@eManPrague]: https://github.com/eManPrague
[@nattfodd]: https://github.com/nattfodd
[@DNNX]: https://github.com/DNNX
[@robertasg]: https://github.com/robertasg
[@AndreySavelyev]: https://github.com/@AndreySavelyev
[@arion]: https://github.com/@arion
[@arturtr]: https://github.com/@arturtr
[@matchbookmac]: https://github.com/matchbookmac
[@heartfulbird]: https://github.com/heartfulbird
[@Borzik]: https://github.com/Borzik
[@josephchoe]: https://github.com/josephchoe
[@barthez]: https://github.com/barthez
[@sergey-kintsel]: https://github.com/sergey-kintsel
[@parallel588]: https://github.com/parallel588
[@eproulx-petalmd]: https://github.com/eproulx-petalmd
[@averell23]: https://github.com/@averell23
[@averell23]: https://github.com/averell23
[@menglewis]: https://github.com/menglewis
[@baronworks]: https://github.com/@baronworks
[@barthez]: https://github.com/barthez
[@bbatsov]: https://github.com/bbatsov
[@bhacaz]: https://github.com/bhacaz
[@biow0lf]: https://github.com/@biow0lf
[@Borzik]: https://github.com/Borzik
[@caldwecr]: https://github.com/@caldwecr
[@clupprich]: https://github.com/@clupprich
[@dalthon]: https://github.com/dalthon
[@davekaro]: https://github.com/@davekaro
[@dck]: https://github.com/dck
[@dm1try]: https://github.com/dm1try
[@dmitry]: https://github.com/dmitry
[@robacarp]: https://github.com/robacarp
[@sevab]: https://github.com/sevab
[@guigs]: https://github.com/guigs
[@okliv]: https://github.com/okliv
[@0x0badc0de]: https://github.com/0x0badc0de
[@dnd]: https://github.com/dnd
[@mikeyhogarth]: https://github.com/mikeyhogarth
[@DNNX]: https://github.com/@DNNX
[@DNNX]: https://github.com/DNNX
[@eManPrague]: https://github.com/eManPrague
[@eproulx-petalmd]: https://github.com/eproulx-petalmd
[@fabiotomio]: https://github.com/@fabiotomio
[@feymartynov]: https://github.com/@feymartynov
[@reidab]: https://github.com/@reidab
[@sergeygaychuk]: https://github.com/@sergeygaychuk
[@mrbrdo]: https://github.com/@mrbrdo
[@gseddon]: https://github.com/gseddon
[@guigs]: https://github.com/guigs
[@heartfulbird]: https://github.com/heartfulbird
[@henrebotha]: https://github.com/@henrebotha
[@inbeom]: https://github.com/@inbeom
[@jesjos]: https://github.com/@jesjos
[@clupprich]: https://github.com/@clupprich
[@mainameiz]: https://github.com/@mainameiz
[@marshall]: https://github.com/@marshall
[@davekaro]: https://github.com/@davekaro
[@mkcode]: https://github.com/@mkcode
[@AndreySavelyev]: https://github.com/@AndreySavelyev
[@joonty]: https://github.com/@joonty
[@biow0lf]: https://github.com/@biow0lf
[@caldwecr]: https://github.com/@caldwecr
[@JF-Lalonde]: https://github.com/JF-Lalonde
[@jimmybaker]: https://github.com/@jimmybaker
[@jirikolarik]: https://github.com/@jirikolarik
[@jirutka]: https://github.com/@jirutka
[@henrebotha]: https://github.com/@henrebotha
[@SeTeM]: https://github.com/@SeTeM
[@joeljunstrom]: https://github.com/@joeljunstrom
[@jondavidford]: https://github.com/@jondavidford
[@joonty]: https://github.com/@joonty
[@josecoelho]: https://github.com/@josecoelho
[@josephchoe]: https://github.com/josephchoe
[@jshirley]: https://github.com/@jshirley
[@ka8725]: https://github.com/@ka8725
[@lardawge]: https://github.com/@lardawge
[@sharkzp]: https://github.com/@sharkzp
[@DNNX]: https://github.com/@DNNX
[@joeljunstrom]: https://github.com/@joeljunstrom
[@leemhenson]: https://github.com/@leemhenson
[@baronworks]: https://github.com/@baronworks
[@Linuus]: https://github.com/@Linuus
[@jshirley]: https://github.com/@jshirley
[@fabiotomio]: https://github.com/@fabiotomio
[@lowang]: https://github.com/lowang
[@mainameiz]: https://github.com/@mainameiz
[@MarkMurphy]: https://github.com/@MarkMurphy
[@josecoelho]: https://github.com/@josecoelho
[@inbeom]: https://github.com/@inbeom
[@jirikolarik]: https://github.com/@jirikolarik
[@jondavidford]: https://github.com/@jondavidford
[@jimmybaker]: https://github.com/@jimmybaker
[@averell23]: https://github.com/@averell23
[@rschellhorn]: https://github.com/@rschellhorn
[@arion]: https://github.com/@arion
[@marshall]: https://github.com/@marshall
[@matchbookmac]: https://github.com/matchbookmac
[@matthee]: https://github.com/@matthee
[@mattzollinhofer]: https://github.com/mattzollinhofer
[@menglewis]: https://github.com/menglewis
[@mikeyhogarth]: https://github.com/mikeyhogarth
[@mkcode]: https://github.com/@mkcode
[@mrbrdo]: https://github.com/@mrbrdo
[@mrzasa]: https://github.com/mrzasa
[@nattfodd]: https://github.com/nattfodd
[@okliv]: https://github.com/okliv
[@olancheg]: https://github.com/olancheg
[@parallel588]: https://github.com/parallel588
[@pyromaniac]: https://github.com/pyromaniac
[@rabotyaga]: https://github.com/rabotyaga
[@reidab]: https://github.com/@reidab
[@robacarp]: https://github.com/robacarp
[@robertasg]: https://github.com/robertasg
[@rschellhorn]: https://github.com/@rschellhorn
[@sergey-kintsel]: https://github.com/sergey-kintsel
[@sergeygaychuk]: https://github.com/@sergeygaychuk
[@SeTeM]: https://github.com/@SeTeM
[@sevab]: https://github.com/sevab
[@sharkzp]: https://github.com/@sharkzp
[@socialchorus]: https://github.com/socialchorus
[@taylor-au]: https://github.com/taylor-au
[@undr]: https://github.com/@undr
[@webgago]: https://github.com/@webgago
[@yahooguntu]: https://github.com/yahooguntu
5 changes: 4 additions & 1 deletion lib/chewy/search/scrolling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ def scroll_batches(batch_size: Request::DEFAULT_BATCH_SIZE, scroll: Request::DEF
total = [raw_limit_value, result.fetch('hits', {}).fetch('total', 0)].compact.min
last_batch_size = total % batch_size
fetched = 0
scroll_id = nil

loop do
hits = result.fetch('hits', {}).fetch('hits', [])
fetched += hits.size
hits = hits.first(last_batch_size) if last_batch_size != 0 && fetched >= total
yield(hits) if hits.present?
break if fetched >= total
scroll_id = result['_scroll_id']
break if fetched >= total
result = perform_scroll(scroll: scroll, scroll_id: scroll_id)
end
ensure
Chewy.client.clear_scroll(scroll_id: scroll_id) if scroll_id
end

# @!method scroll_hits(batch_size: 1000, scroll: '1m')
Expand Down
5 changes: 5 additions & 0 deletions spec/chewy/search/scrolling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
end
end

it 'clears the scroll after completion' do
expect(Chewy.client).to receive(:clear_scroll).with(scroll_id: anything).once.and_call_original
request.scroll_batches(batch_size: 3) {}
end

context 'instrumentation' do
specify do
outer_payload = []
Expand Down