aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/application.rb4
-rw-r--r--app/controllers/general_controller.rb17
-rw-r--r--app/views/general/search.rhtml4
-rw-r--r--vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb17
4 files changed, 37 insertions, 5 deletions
diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index c6f346907..9a1ef035b 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: application.rb,v 1.51 2008-10-07 22:05:06 francis Exp $
+# $Id: application.rb,v 1.52 2009-01-08 16:57:16 francis Exp $
class ApplicationController < ActionController::Base
@@ -188,6 +188,8 @@ class ApplicationController < ActionController::Base
return ['created_at', true]
elsif sortby == 'described'
return ['described_at', true] # use this for some RSS
+ elsif sortby == 'relevant'
+ return [nil, nil]
else
raise "Unknown sort order " + @sortby
end
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb
index e95dee266..922d26590 100644
--- a/app/controllers/general_controller.rb
+++ b/app/controllers/general_controller.rb
@@ -5,7 +5,7 @@
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: general_controller.rb,v 1.45 2009-01-05 16:24:23 francis Exp $
+# $Id: general_controller.rb,v 1.46 2009-01-08 16:57:16 francis Exp $
class GeneralController < ApplicationController
@@ -55,7 +55,7 @@ class GeneralController < ApplicationController
@bodies = false # searching from front page, largely for a public authority
# XXX currently /described isn't linked to anywhere, just used in RSS and for /list/successful
# This is because it's confusingly different from /newest - but still useful for power users.
- if combined.size > 1 && (['newest', 'described', 'bodies'].include?(combined[-1]))
+ if combined.size > 1 && (['newest', 'described', 'bodies', 'relevant'].include?(combined[-1]))
@postfix = combined[-1]
combined = combined[0..-2]
if @postfix == 'bodies'
@@ -66,6 +66,19 @@ class GeneralController < ApplicationController
end
@query = combined.join("/")
+ @inputted_sortby = @sortby
+ if @sortby.nil?
+ # Parse query, so can work out if it has prefix terms only - if so then it is a
+ # structured query which should show newest first, rather than a free text search
+ # where we want most relevant as default.
+ dummy_query = ::ActsAsXapian::Search.new([InfoRequestEvent], @query, :limit => 1)
+ if dummy_query.has_normal_search_terms?
+ @sortby = 'relevant'
+ else
+ @sortby = 'newest'
+ end
+ end
+
# Query each type separately for separate display (XXX we are calling
# perform_search multiple times and it clobbers per_page for each one,
# so set as separate var)
diff --git a/app/views/general/search.rhtml b/app/views/general/search.rhtml
index cafcc2a3f..94668e59d 100644
--- a/app/views/general/search.rhtml
+++ b/app/views/general/search.rhtml
@@ -21,7 +21,7 @@
<% form_tag({:action => "search_redirect"}, {:id => "search_form"}) do %>
<p>
<%= text_field_tag 'query', @query, { :size => 40 } %>
- <%= hidden_field_tag 'sortby', @sortby %>
+ <%= hidden_field_tag 'sortby', @inputted_sortby %>
<% if @bodies %>
<%= hidden_field_tag 'bodies', 1 %>
<% end %>
@@ -34,7 +34,7 @@
<% if !@query.nil? %>
<p>
- <%=link_to_unless @sortby.nil?, "Show most relevant results first", search_url(@query, nil) %>
+ <%=link_to_unless @sortby == 'relevant', "Show most relevant results first", search_url(@query, 'relevant') %>
|
<%=link_to_unless @sortby == 'newest', "Newest results first", search_url(@query, 'newest') %>
<% if @sortby == 'described' %>
diff --git a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
index 9b801e42b..dde18b091 100644
--- a/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
+++ b/vendor/plugins/acts_as_xapian/lib/acts_as_xapian.rb
@@ -279,6 +279,23 @@ module ActsAsXapian
self.query.description
end
+ # Does the query have non-prefixed search terms in it?
+ def has_normal_search_terms?
+ ret = false
+ #x = ''
+ for t in self.query.terms
+ term = t.term
+ #x = x + term.to_yaml + term.size.to_s + term[0..0] + "*"
+ if term.size >= 2 && term[0..0] == 'Z'
+ # normal terms begin Z (for stemmed), then have no capital letter prefix
+ if term[1..1] == term[1..1].downcase
+ ret = true
+ end
+ end
+ end
+ return ret
+ end
+
# Estimate total number of results
def matches_estimated
self.matches.matches_estimated