aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfrancis <francis>2008-04-16 00:44:40 +0000
committerfrancis <francis>2008-04-16 00:44:40 +0000
commitdbb983feb0e5462dcebac7ad4331de7f06ea2367 (patch)
tree45284cfa390192e3fd2a9d6e85e68ce8a649c3b6
parentf95573cb2ef05586be88439b5b645460029e76e2 (diff)
Load associations relating to search
-rw-r--r--app/models/info_request.rb6
-rw-r--r--vendor/plugins/acts_as_solr/lib/parser_methods.rb22
2 files changed, 24 insertions, 4 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index f0cf4ee11..ad5196778 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -22,7 +22,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: info_request.rb,v 1.87 2008-04-15 18:31:16 francis Exp $
+# $Id: info_request.rb,v 1.88 2008-04-16 00:44:40 francis Exp $
require 'digest/sha1'
@@ -123,7 +123,9 @@ class InfoRequest < ActiveRecord::Base
:fields => ["solr_text_main", "title", # InfoRequestEvent
"name", "short_name", # PublicBody
"name" # User
- ]}, :order => order
+ ]},
+ :order => order,
+ :include => { :InfoRequestEvent => [ :incoming_message, :outgoing_message ] }
)
end
diff --git a/vendor/plugins/acts_as_solr/lib/parser_methods.rb b/vendor/plugins/acts_as_solr/lib/parser_methods.rb
index 2a5223332..7a112618a 100644
--- a/vendor/plugins/acts_as_solr/lib/parser_methods.rb
+++ b/vendor/plugins/acts_as_solr/lib/parser_methods.rb
@@ -6,7 +6,7 @@ module ActsAsSolr #:nodoc:
# Method used by mostly all the ClassMethods when doing a search
def parse_query(query=nil, options={}, models=nil)
- valid_options = [:offset, :limit, :facets, :models, :results_format, :order, :scores, :operator, :highlight]
+ valid_options = [:offset, :limit, :facets, :models, :results_format, :order, :scores, :operator, :highlight, :include]
query_options = {}
return if query.nil?
raise "Invalid parameters: #{(options.keys - valid_options).join(',')}" unless (options.keys - valid_options).empty?
@@ -129,7 +129,25 @@ module ActsAsSolr #:nodoc:
result = []
docs = solr_data.docs
if options[:results_format] == :objects
- docs.each{|doc| k = doc.fetch('id').to_s.split(':'); result << k[0].constantize.find_by_id(k[1])}
+ # NOTE: All this bit is new, by mySociety, to reduce number of SQL queries
+ # find all ids for each class
+ lhash = {}
+ lhash.default = []
+ for doc in docs
+ k = doc.fetch('id').to_s.split(':')
+ lhash[k[0]] = lhash[k[0]] + [k[1]]
+ end
+ # for each class, look up all ids
+ chash = {}
+ for cls, ids in lhash
+ conditions = [ "#{cls.constantize.table_name}.#{primary_key} in (?)", ids ]
+ found = reorder(cls.constantize.find(:all, :conditions => conditions, :include => options[:include][cls.to_sym]), ids)
+ for f in found
+ chash[[cls, f.id]] = f
+ end
+ end
+ # now get them in right order again
+ docs.each{|doc| k = doc.fetch('id').to_s.split(':'); result << chash[[k[0], k[1].to_i]]}
elsif options[:results_format] == :ids
docs.each{|doc| result << {"id"=>doc.values.pop.to_s}}
end