From f759a8af65124374279ecfecd6115bd12efa4d59 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 3 Feb 2015 12:25:11 +0000 Subject: Do not allow requests for search results after the first 500 The pages for these search results are extremely slow to load. This is not an ideal solution by any means. Really we want to dig into why high offsets are so slow, and whether there's anything we can do to fix that. --- app/controllers/general_controller.rb | 12 ++++++++++++ app/views/general/search.html.erb | 6 +++--- spec/controllers/general_controller_spec.rb | 6 ++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index 2c8abbaf4..6aef0cb1b 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -9,6 +9,8 @@ require 'open-uri' class GeneralController < ApplicationController + MAX_RESULTS = 500 + # New, improved front page! def frontpage medium_cache @@ -124,11 +126,18 @@ class GeneralController < ApplicationController end end + @page = get_search_page_from_params + # Query each type separately for separate display (TODO: we are calling # perform_search multiple times and it clobbers per_page for each one, # so set as separate var) requests_per_page = params[:requests_per_page] ? params[:requests_per_page].to_i : 25 + # Later pages are very expensive to load + if @page > MAX_RESULTS / requests_per_page + raise ActiveRecord::RecordNotFound.new("Sorry. No pages after #{MAX_RESULTS / requests_per_page}.") + end + @this_page_hits = @total_hits = @xapian_requests_hits = @xapian_bodies_hits = @xapian_users_hits = 0 if @requests @xapian_requests = perform_search([InfoRequestEvent], @query, @sortby, 'request_collapse', requests_per_page) @@ -138,6 +147,7 @@ class GeneralController < ApplicationController @xapian_requests_total_hits = @xapian_requests.matches_estimated @total_hits += @xapian_requests.matches_estimated @request_for_spelling = @xapian_requests + @max_requests = (@xapian_requests.matches_estimated > MAX_RESULTS) ? MAX_RESULTS : @xapian_requests.matches_estimated end if @bodies @xapian_bodies = perform_search([PublicBody], @query, @sortby, nil, 5) @@ -147,6 +157,7 @@ class GeneralController < ApplicationController @xapian_bodies_total_hits = @xapian_bodies.matches_estimated @total_hits += @xapian_bodies.matches_estimated @request_for_spelling = @xapian_bodies + @max_bodies = (@xapian_bodies.matches_estimated > MAX_RESULTS) ? MAX_RESULTS : @xapian_bodies.matches_estimated end if @users @xapian_users = perform_search([User], @query, @sortby, nil, 5) @@ -156,6 +167,7 @@ class GeneralController < ApplicationController @xapian_users_total_hits = @xapian_users.matches_estimated @total_hits += @xapian_users.matches_estimated @request_for_spelling = @xapian_users + @max_users = (@xapian_users.matches_estimated > MAX_RESULTS) ? MAX_RESULTS : @xapian_users.matches_estimated end # Spelling and highight words are same for all three queries diff --git a/app/views/general/search.html.erb b/app/views/general/search.html.erb index 3914a2f30..0a04f544e 100644 --- a/app/views/general/search.html.erb +++ b/app/views/general/search.html.erb @@ -160,7 +160,7 @@ <% end %> - <%= will_paginate WillPaginate::Collection.new(@page, @bodies_per_page, @xapian_bodies.matches_estimated) %> + <%= will_paginate WillPaginate::Collection.new(@page, @bodies_per_page, @max_bodies) %> <% elsif @variety_postfix == 'bodies' %>

<%= raw(_('Browse all or ask us to add one.', :browse_url => list_public_bodies_default_path.html_safe, :add_url => (help_requesting_path + '#missing_body').html_safe)) %>

@@ -179,7 +179,7 @@ <%= render :partial => 'user/user_listing_single', :locals => { :display_user => result[:model] } %> <% end %> - <%= will_paginate WillPaginate::Collection.new(@page, @users_per_page, @xapian_users.matches_estimated) %> + <%= will_paginate WillPaginate::Collection.new(@page, @users_per_page, @max_users) %> <% end %> @@ -199,7 +199,7 @@ <% end %> - <%= will_paginate WillPaginate::Collection.new(@page, @requests_per_page, @xapian_requests.matches_estimated) %> + <%= will_paginate WillPaginate::Collection.new(@page, @requests_per_page, @max_requests) %> <% end %> <% end %> diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index cb00b301c..8652d9b17 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -262,4 +262,10 @@ describe GeneralController, 'when using xapian search' do response.body.should include('Track this search') end + it 'should not show high page offsets as these are extremely slow to generate' do + lambda { + get :search, :combined => 'bob/all', :page => 25 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end -- cgit v1.2.3 From d5b3973c614aeadb9a13b54c7540fd2a91b53e8e Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 3 Feb 2015 13:29:46 +0000 Subject: Remove unused variable --- app/controllers/general_controller.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index 6aef0cb1b..438bbfd3f 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -138,11 +138,10 @@ class GeneralController < ApplicationController raise ActiveRecord::RecordNotFound.new("Sorry. No pages after #{MAX_RESULTS / requests_per_page}.") end - @this_page_hits = @total_hits = @xapian_requests_hits = @xapian_bodies_hits = @xapian_users_hits = 0 + @total_hits = @xapian_requests_hits = @xapian_bodies_hits = @xapian_users_hits = 0 if @requests @xapian_requests = perform_search([InfoRequestEvent], @query, @sortby, 'request_collapse', requests_per_page) @requests_per_page = @per_page - @this_page_hits += @xapian_requests.results.size @xapian_requests_hits = @xapian_requests.results.size @xapian_requests_total_hits = @xapian_requests.matches_estimated @total_hits += @xapian_requests.matches_estimated @@ -152,7 +151,6 @@ class GeneralController < ApplicationController if @bodies @xapian_bodies = perform_search([PublicBody], @query, @sortby, nil, 5) @bodies_per_page = @per_page - @this_page_hits += @xapian_bodies.results.size @xapian_bodies_hits = @xapian_bodies.results.size @xapian_bodies_total_hits = @xapian_bodies.matches_estimated @total_hits += @xapian_bodies.matches_estimated @@ -162,7 +160,6 @@ class GeneralController < ApplicationController if @users @xapian_users = perform_search([User], @query, @sortby, nil, 5) @users_per_page = @per_page - @this_page_hits += @xapian_users.results.size @xapian_users_hits = @xapian_users.results.size @xapian_users_total_hits = @xapian_users.matches_estimated @total_hits += @xapian_users.matches_estimated -- cgit v1.2.3 From bac98e595afe6304bcd1f1f06e3472de33687182 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 5 Feb 2015 10:38:50 +0000 Subject: Bump version number --- config/initializers/alaveteli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index ee9ad8316..e4672ccbc 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -10,7 +10,7 @@ load "debug_helpers.rb" load "util.rb" # Application version -ALAVETELI_VERSION = '0.20.0.5' +ALAVETELI_VERSION = '0.20.0.7' # Add new inflection rules using the following format # (all these examples are active by default): -- cgit v1.2.3