Skip to content

Commit

Permalink
added tests for scroll api. #371
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 8, 2019
1 parent bfbf574 commit a933dae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def index
options = {}
options[:meta] = {
total: total,
scroll_id: response.scroll_id,
"scroll-id" => response.scroll_id,
}.compact
options[:links] = {
self: request.original_url,
next: results.size < page[:size] || page[:size] == 0 ? nil : request.base_url + "/dois?" + {
scroll_id: response.scroll_id,
"scroll-id" => response.scroll_id,
"page[scroll]" => "3m",
"page[size]" => page[:size] }.compact.to_query
}.compact
Expand Down
7 changes: 5 additions & 2 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def get_aggregations_hash(options={})
end

def query(query, options={})
# support scroll api
# map function is small performance hit
if options[:scroll_id].present? && options.dig(:page, :scroll)
response = __elasticsearch__.client.scroll(body:
{ scroll_id: options[:scroll_id],
Expand Down Expand Up @@ -311,12 +313,13 @@ def query(query, options={})
"max_concurrent_group_searches": 1
}

logger = Logger.new(STDOUT)

# three options for going through results are scroll, cursor and pagination
# the default is pagination
# scroll is triggered by the page[scroll] query parameter
# cursor is triggered by the page[cursor] query parameter

# can't use search wrapper function for scroll api
# map function for scroll is small performance hit
if options.dig(:page, :scroll).present?
response = __elasticsearch__.client.search(
index: self.index_name,
Expand Down
12 changes: 12 additions & 0 deletions spec/concerns/indexable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@
expect(results.to_a.length).to eq(1)
end

it 'query with scroll' do
response = Doi.query(nil, page: { size: 2, scroll: "1m" })
expect(response.total).to eq(4)

# Initial length should match the size
expect(response.results.to_a.length).to eq(2)

# Move onto next based on scroll_id
response = Doi.query(nil, page: { size: 1, scroll: "1m" }, scroll_id: response.scroll_id)
expect(response.results.to_a.length).to eq(2)
end

context "aggregations" do
it 'returns query_aggregation when filters aggregation with empty' do
aggregations = Doi.get_aggregations_hash({aggregations:""})
Expand Down
10 changes: 10 additions & 0 deletions spec/requests/dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
expect(json.dig('meta', 'total')).to eq(3)
end

it 'returns dois with scroll', vcr: true do
get '/dois?page[scroll]=1m', nil, headers

expect(last_response.status).to eq(200)
expect(json['data'].size).to eq(3)
expect(json.dig('meta', 'total')).to eq(3)
expect(json.dig('meta', 'scroll-id')).to be_present
expect(json.dig('links', 'next')).to be_nil
end

it 'returns dois with extra detail', vcr: true do
get '/dois?detail=true', nil, headers

Expand Down

0 comments on commit a933dae

Please sign in to comment.