@@ -19,6 +19,9 @@ def results
1919 else
2020 params [ :tab ] || 'primo' # Default to primo for new tabbed interface
2121 end
22+
23+ # Include the active tab in the enhanced query so it's available for pagination and other uses.
24+ params [ :tab ] = @active_tab unless Feature . enabled? ( :gdt )
2225 @enhanced_query = Enhancer . new ( params ) . enhanced_query
2326
2427 # Route to appropriate search based on active tab
@@ -47,28 +50,41 @@ def load_gdt_results
4750 @errors = extract_errors ( response )
4851 return unless @errors . nil?
4952
50- @pagination = Analyzer . new ( @enhanced_query , response ) . pagination
53+ @pagination = Analyzer . new ( @enhanced_query , response , :timdex ) . pagination
5154 raw_results = extract_results ( response )
5255 @results = NormalizeTimdexResults . new ( raw_results , @enhanced_query [ :q ] ) . normalize
5356 @filters = extract_filters ( response )
5457 end
5558
5659 def load_primo_results
60+ current_page = @enhanced_query [ :page ] || 1
61+ per_page = @enhanced_query [ :per_page ] || 20
62+ offset = ( current_page - 1 ) * per_page
63+
64+ # Check if we're beyond Primo API limits before making the request.
65+ if offset >= Analyzer ::PRIMO_MAX_OFFSET
66+ @show_primo_continuation = true
67+ return
68+ end
69+
5770 primo_response = query_primo
5871 @results = NormalizePrimoResults . new ( primo_response , @enhanced_query [ :q ] ) . normalize
5972
60- # Enhanced pagination using cached response
61- if @results . present?
62- total_hits = primo_response . dig ( 'info' , 'total' ) || @results . count
63- per_page = @enhanced_query [ :per_page ] || 20
64- current_page = @enhanced_query [ :page ] || 1
65-
66- @pagination = {
67- hits : total_hits ,
68- start : ( ( current_page - 1 ) * per_page ) + 1 ,
69- end : [ current_page * per_page , total_hits ] . min
70- }
73+ # Handle empty results from Primo API. Sometimes Primo will return no results at a given offset,
74+ # despite claiming in the initial query that more are available. This happens randomly and
75+ # seemingly for no reason (well below the recommended offset of 2,000). While the bug also
76+ # exists in Primo UI, sending users there seems like the best we can do.
77+ if @results . empty?
78+ docs = primo_response [ 'docs' ] if primo_response . is_a? ( Hash )
79+ if docs . nil? || docs . empty?
80+ @show_primo_continuation = true
81+ else
82+ @errors = [ { 'message' => 'No more results available at this page number.' } ]
83+ end
7184 end
85+
86+ # Use Analyzer for consistent pagination across all search types
87+ @pagination = Analyzer . new ( @enhanced_query , primo_response , :primo ) . pagination
7288 rescue StandardError => e
7389 @errors = handle_primo_errors ( e )
7490 end
@@ -80,7 +96,7 @@ def load_timdex_results
8096 @errors = extract_errors ( response )
8197 return unless @errors . nil?
8298
83- @pagination = Analyzer . new ( @enhanced_query , response ) . pagination
99+ @pagination = Analyzer . new ( @enhanced_query , response , :timdex ) . pagination
84100 raw_results = extract_results ( response )
85101 @results = NormalizeTimdexResults . new ( raw_results , @enhanced_query [ :q ] ) . normalize
86102 end
@@ -117,7 +133,10 @@ def query_primo
117133 Rails . cache . fetch ( "#{ cache_key } /primo" , expires_in : 12 . hours ) do
118134 primo_search = PrimoSearch . new
119135 per_page = @enhanced_query [ :per_page ] || 20
120- primo_search . search ( @enhanced_query [ :q ] , per_page )
136+ current_page = @enhanced_query [ :page ] || 1
137+ offset = ( current_page - 1 ) * per_page
138+
139+ primo_search . search ( @enhanced_query [ :q ] , per_page , offset )
121140 end
122141 end
123142
0 commit comments