From b50adc72e533a9c20aca03063a676f6af4d32b85 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 18 Jan 2012 08:46:06 +0000 Subject: Fix paging bug for type ahead search. This wasn't caught because the spec didn't call "integrate_views", so the rendering part was mocked by RSpec instead of executed. --- app/controllers/application_controller.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0c8544932..2cdd5ee35 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -365,7 +365,10 @@ class ApplicationController < ActionController::Base def get_search_page_from_params return (params[:page] || "1").to_i end + def perform_search_typeahead(query, model) + @page = get_search_page_from_params + @per_page = 10 query_words = query.split(/ +(?![-+]+)/) if query_words.last.nil? || query_words.last.strip.length < 3 xapian_requests = nil @@ -376,8 +379,8 @@ class ApplicationController < ActionController::Base collapse = 'request_collapse' end options = { - :offset => 0, - :limit => 5, + :offset => (@page - 1) * @per_page, + :limit => @per_page, :sort_by_prefix => nil, :sort_by_ascending => true, :collapse_by_prefix => collapse, -- cgit v1.2.3 From 936c3b2fb1365e46239ff98ab962460d0c459730 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 18 Jan 2012 09:07:44 +0000 Subject: Catch timeouts and other extra errors when connection to 3rd party websites fails. --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2cdd5ee35..1c73f06a7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -524,7 +524,7 @@ class ApplicationController < ActionController::Base def quietly_try_to_open(url) begin result = open(url).read.strip - rescue OpenURI::HTTPError, SocketError + rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH logger.warn("Unable to open third-party URL #{url}") result = "" end -- cgit v1.2.3 From 22b2db48c005be1d1b87dd6b58f830a2a74d2108 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Thu, 19 Jan 2012 13:50:26 +0000 Subject: Limit Xapian wildcard expansions Wildcard searches in Xapian can expand uncontrollably, consuming all available RAM and crashing the server. This has been a real problem on WhatDoTheyKnow.com. The underlying issue is tracked in http://trac.xapian.org/ticket/350 This changeset imposes a limit on wildcard expansion. The type-ahead search will first try a wildcard query, and if that fails because of too much expansion will fall back to a plain non-wildcard search. --- app/controllers/application_controller.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'app/controllers/application_controller.rb') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0c8544932..eae27c667 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -385,11 +385,24 @@ class ApplicationController < ActionController::Base ActsAsXapian.readable_init old_default_op = ActsAsXapian.query_parser.default_op ActsAsXapian.query_parser.default_op = Xapian::Query::OP_OR - user_query = ActsAsXapian.query_parser.parse_query( - query, - Xapian::QueryParser::FLAG_LOVEHATE | Xapian::QueryParser::FLAG_PARTIAL | - Xapian::QueryParser::FLAG_SPELLING_CORRECTION) - xapian_requests = ActsAsXapian::Search.new([model], query, options, user_query) + begin + user_query = ActsAsXapian.query_parser.parse_query( + query.strip + '*', + Xapian::QueryParser::FLAG_LOVEHATE | Xapian::QueryParser::FLAG_WILDCARD | + Xapian::QueryParser::FLAG_SPELLING_CORRECTION) + xapian_requests = ActsAsXapian::Search.new([model], query, options, user_query) + rescue RuntimeError => e + if e.message =~ /^QueryParserError: Wildcard/ + # Wildcard expands to too many terms + logger.info "Wildcard query '#{query.strip + '*'}' caused: #{e.message}" + + user_query = ActsAsXapian.query_parser.parse_query( + query, + Xapian::QueryParser::FLAG_LOVEHATE | + Xapian::QueryParser::FLAG_SPELLING_CORRECTION) + xapian_requests = ActsAsXapian::Search.new([model], query, options, user_query) + end + end ActsAsXapian.query_parser.default_op = old_default_op end return xapian_requests -- cgit v1.2.3