Commit 4e977f084e556e9eb52682d4c0e52ad2a8a3f617

Authored by Braulio Bhavamitra
1 parent 0c822dbe

Update acts_as_solr_reloaded with upstream changes

vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr.rb
@@ -27,6 +27,10 @@ module ActsAsSolr @@ -27,6 +27,10 @@ module ActsAsSolr
27 @config ||= YAML::load_file("#{Rails.root}/config/solr.yml")[Rails.env] 27 @config ||= YAML::load_file("#{Rails.root}/config/solr.yml")[Rails.env]
28 end 28 end
29 29
  30 + def options
  31 + @options ||= credentials.merge( :timeout => config['timeout'] )
  32 + end
  33 +
30 def credentials 34 def credentials
31 @credentials ||= {:username => config['username'], :password => config['password']} 35 @credentials ||= {:username => config['username'], :password => config['password']}
32 end 36 end
@@ -36,7 +40,7 @@ module ActsAsSolr @@ -36,7 +40,7 @@ module ActsAsSolr
36 end 40 end
37 41
38 def execute(request, core = nil) 42 def execute(request, core = nil)
39 - connection = Solr::Connection.new(url(core), credentials) 43 + connection = Solr::Connection.new(url(core), options)
40 connection.send request 44 connection.send request
41 end 45 end
42 end 46 end
vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/class_methods.rb
@@ -13,13 +13,17 @@ module ActsAsSolr #:nodoc: @@ -13,13 +13,17 @@ module ActsAsSolr #:nodoc:
13 # results = Book.find_by_solr params[:query] 13 # results = Book.find_by_solr params[:query]
14 # end 14 # end
15 # 15 #
16 - # You can also search for specific fields by searching for 'field:value' 16 + # For specific fields searching use :filter_queries options
17 # 17 #
18 # ====options: 18 # ====options:
19 # offset:: - The first document to be retrieved (offset) 19 # offset:: - The first document to be retrieved (offset)
20 # page:: - The page to be retrieved 20 # page:: - The page to be retrieved
21 # limit:: - The number of rows per page 21 # limit:: - The number of rows per page
22 # per_page:: - Alias for limit 22 # per_page:: - Alias for limit
  23 + # filter_queries:: - Use solr filter queries to sort by fields
  24 + #
  25 + # Book.find_by_solr 'ruby', :filter_queries => ['price:5']
  26 + #
23 # order:: - Orders (sort by) the result set using a given criteria: 27 # order:: - Orders (sort by) the result set using a given criteria:
24 # 28 #
25 # Book.find_by_solr 'ruby', :order => 'description asc' 29 # Book.find_by_solr 'ruby', :order => 'description asc'
vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/common_methods.rb
@@ -5,28 +5,21 @@ module ActsAsSolr #:nodoc: @@ -5,28 +5,21 @@ module ActsAsSolr #:nodoc:
5 # Converts field types into Solr types 5 # Converts field types into Solr types
6 def get_solr_field_type(field_type) 6 def get_solr_field_type(field_type)
7 if field_type.is_a?(Symbol) 7 if field_type.is_a?(Symbol)
8 - case field_type  
9 - when :float, :decimal  
10 - return "f"  
11 - when :integer  
12 - return "i"  
13 - when :boolean  
14 - return "b"  
15 - when :string  
16 - return "s"  
17 - when :date  
18 - return "d"  
19 - when :range_float  
20 - return "rf"  
21 - when :range_integer  
22 - return "ri"  
23 - when :facet  
24 - return "facet"  
25 - when :text  
26 - return "t"  
27 - else  
28 - raise "Unknown field_type symbol: #{field_type}"  
29 - end 8 + h = {
  9 + :float => "f",
  10 + :decimal => "f",
  11 + :integer => "i",
  12 + :boolean => "b",
  13 + :string => "s",
  14 + :date => "d",
  15 + :range_float => "rf",
  16 + :range_integer => "ri",
  17 + :facet => "facet",
  18 + :text => "t",
  19 + }
  20 + t = h[field_type]
  21 + raise "Unknown field_type symbol: #{field_type}" if t.nil?
  22 + t
30 elsif field_type.is_a?(String) 23 elsif field_type.is_a?(String)
31 return field_type 24 return field_type
32 else 25 else
vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/parser_methods.rb
@@ -4,13 +4,24 @@ module ActsAsSolr #:nodoc: @@ -4,13 +4,24 @@ module ActsAsSolr #:nodoc:
4 4
5 # Method used by mostly all the ClassMethods when doing a search 5 # Method used by mostly all the ClassMethods when doing a search
6 def parse_query(query=nil, options={}, models=nil) 6 def parse_query(query=nil, options={}, models=nil)
7 - valid_options = [ :offset, :limit, :facets, :models, :results_format, :order,  
8 - :scores, :operator, :include, :lazy, :joins, :select, :core,  
9 - :around, :relevance, :highlight, :page, :per_page] 7 + valid_options = [ :filter_queries, :offset, :limit, :facets, :models, :results_format, :order,
  8 + :scores, :operator, :include, :lazy, :joins, :select, :core,
  9 + :around, :relevance, :highlight, :page, :per_page]
10 query_options = {} 10 query_options = {}
11 - query = sanitize_query(query) if query  
12 - return nil if (query.nil? || query.strip == '')  
13 - 11 +
  12 + field_list = models.nil? ? solr_configuration[:primary_key_field] : "id"
  13 + query_options[:field_list] = [field_list, 'score']
  14 + query_options[:filter_queries] = []
  15 + #allow empty queries as a type search will also be done
  16 + query = nil if (query.nil? || query == '')
  17 + if query.nil?
  18 + query = solr_type_condition
  19 + else
  20 + query = sanitize_query(query)
  21 + query_options[:filter_queries].push(solr_type_condition)
  22 + end
  23 + query_options[:query] = query
  24 + order = options[:order].split(/\s*,\s*/).collect{|e| e.gsub(/\s+/,'_t ').gsub(/\bscore_t\b/, 'score') }.join(',') if options[:order]
14 25
15 raise "Invalid parameters: #{(options.keys - valid_options).join(',')}" unless (options.keys - valid_options).empty? 26 raise "Invalid parameters: #{(options.keys - valid_options).join(',')}" unless (options.keys - valid_options).empty?
16 begin 27 begin
@@ -23,6 +34,9 @@ module ActsAsSolr #:nodoc: @@ -23,6 +34,9 @@ module ActsAsSolr #:nodoc:
23 34
24 query = add_relevance query, options[:relevance] 35 query = add_relevance query, options[:relevance]
25 36
  37 + raise "Expecting and array of strings for :filter_queries" unless options[:filter_queries].nil? or options[:filter_queries].kind_of?(Array)
  38 + query_options[:filter_queries] = replace_types([*options[:filter_queries]].collect{|k| "#{k.sub!(/ *: */,"_t:")}"}) if options[:filter_queries]
  39 +
26 # first steps on the facet parameter processing 40 # first steps on the facet parameter processing
27 if options[:facets] 41 if options[:facets]
28 query_options[:facets] = {} 42 query_options[:facets] = {}
@@ -33,7 +47,7 @@ module ActsAsSolr #:nodoc: @@ -33,7 +47,7 @@ module ActsAsSolr #:nodoc:
33 # override the :zeros (it's deprecated anyway) if :mincount exists 47 # override the :zeros (it's deprecated anyway) if :mincount exists
34 query_options[:facets][:mincount] = options[:facets][:mincount] if options[:facets][:mincount] 48 query_options[:facets][:mincount] = options[:facets][:mincount] if options[:facets][:mincount]
35 query_options[:facets][:fields] = options[:facets][:fields].collect{|k| "#{k}_facet"} if options[:facets][:fields] 49 query_options[:facets][:fields] = options[:facets][:fields].collect{|k| "#{k}_facet"} if options[:facets][:fields]
36 - query_options[:filter_queries] = replace_types([*options[:facets][:browse]].collect{|k| "#{k.sub!(/ *: */,"_facet:")}"}) if options[:facets][:browse] 50 + query_options[:filter_queries] += replace_types([*options[:facets][:browse]].collect{|k| "#{k.sub!(/ *: */,"_t:")}"}) if options[:facets][:browse]
37 query_options[:facets][:queries] = replace_types(options[:facets][:query].collect{|k| "#{k.sub!(/ *: */,"_t:")}"}) if options[:facets][:query] 51 query_options[:facets][:queries] = replace_types(options[:facets][:query].collect{|k| "#{k.sub!(/ *: */,"_t:")}"}) if options[:facets][:query]
38 52
39 53
@@ -68,19 +82,6 @@ module ActsAsSolr #:nodoc: @@ -68,19 +82,6 @@ module ActsAsSolr #:nodoc:
68 end 82 end
69 end 83 end
70 84
71 - if models.nil?  
72 - # TODO: use a filter query for type, allowing Solr to cache it individually  
73 - models = "AND #{solr_type_condition}"  
74 - field_list = solr_configuration[:primary_key_field]  
75 - else  
76 - field_list = "id"  
77 - end  
78 -  
79 - query_options[:field_list] = [field_list, 'score']  
80 - query = "(#{query.gsub(/ *: */,"_t:")}) #{models}"  
81 - order = options[:order].split(/\s*,\s*/).collect{|e| e.gsub(/\s+/,'_t ').gsub(/\bscore_t\b/, 'score') }.join(',') if options[:order]  
82 - query_options[:query] = replace_types([query])[0] # TODO adjust replace_types to work with String or Array  
83 -  
84 if options[:highlight] 85 if options[:highlight]
85 query_options[:highlighting] = {} 86 query_options[:highlighting] = {}
86 query_options[:highlighting][:field_list] = [] 87 query_options[:highlighting][:field_list] = []
@@ -232,9 +233,8 @@ module ActsAsSolr #:nodoc: @@ -232,9 +233,8 @@ module ActsAsSolr #:nodoc:
232 raise "Invalid option#{'s' if bad_options.size > 1} for faceted date's other param: #{bad_options.join(', ')}. May only be one of :after, :all, :before, :between, :none" if bad_options.size > 0 233 raise "Invalid option#{'s' if bad_options.size > 1} for faceted date's other param: #{bad_options.join(', ')}. May only be one of :after, :all, :before, :between, :none" if bad_options.size > 0
233 end 234 end
234 235
235 - # Remove all leading ?'s and *'s from query  
236 def sanitize_query(query) 236 def sanitize_query(query)
237 - query.gsub(/\A([\?|\*| ]+)/, '') 237 + Solr::Util::query_parser_escape query
238 end 238 end
239 239
240 private 240 private
vendor/plugins/acts_as_solr_reloaded/lib/solr/request/select.rb
@@ -33,7 +33,7 @@ class Solr::Request::Select < Solr::Request::Base @@ -33,7 +33,7 @@ class Solr::Request::Select < Solr::Request::Base
33 end 33 end
34 34
35 def to_hash 35 def to_hash
36 - return {:qt => query_type, :wt => 'ruby'}.merge(@select_params) 36 + return {:qt => query_type, :wt => 'ruby', 'json.nl' => 'arrarr'}.merge(@select_params)
37 end 37 end
38 38
39 def to_s 39 def to_s
vendor/plugins/acts_as_solr_reloaded/lib/solr/response/standard.rb
@@ -36,7 +36,6 @@ class Solr::Response::Standard < Solr::Response::Ruby @@ -36,7 +36,6 @@ class Solr::Response::Standard < Solr::Response::Ruby
36 @response['maxScore'] 36 @response['maxScore']
37 end 37 end
38 38
39 - # TODO: consider the use of json.nl parameter  
40 def field_facets(field) 39 def field_facets(field)
41 facets = [] 40 facets = []
42 values = @data['facet_counts']['facet_fields'][field] 41 values = @data['facet_counts']['facet_fields'][field]
vendor/plugins/acts_as_solr_reloaded/lib/tasks/solr.rake
1 namespace :solr do 1 namespace :solr do
2 2
3 - desc 'Download and install Solr+Jetty 3.3.0.' 3 + SOLR_VERSION = '3.3.0'
  4 + APACHE_MIRROR = "http://ftp.unicamp.br/pub/apache"
  5 + SOLR_FILENAME = "apache-solr-#{SOLR_VERSION}.tgz"
  6 + SOLR_DIR = "apache-solr-#{SOLR_VERSION}"
  7 + SOLR_URL = "#{APACHE_MIRROR}/lucene/solr/#{SOLR_VERSION}/#{SOLR_FILENAME}"
  8 +
  9 + desc "Download and install Solr+Jetty #{SOLR_VERSION}."
4 task :download do 10 task :download do
5 - if (File.exists?(Rails.root + '/vendor/plugins/acts_as_solr_reloaded/solr/start.jar')) 11 + if File.exists?(Rails.root + '/vendor/plugins/acts_as_solr_reloaded/solr/start.jar')
6 puts 'Solr already downloaded.' 12 puts 'Solr already downloaded.'
7 else 13 else
8 cd '/tmp' 14 cd '/tmp'
9 - sh 'wget -c http://ftp.unicamp.br/pub/apache/lucene/solr/3.3.0/apache-solr-3.3.0.tgz'  
10 - sh 'tar xzf apache-solr-3.3.0.tgz'  
11 - cd 'apache-solr-3.3.0/example' 15 + sh "wget -c #{SOLR_URL}"
  16 + if !File.directory?("/tmp/#{SOLR_DIR}")
  17 + sh "tar xzf apache-solr-#{SOLR_VERSION}.tgz"
  18 + end
  19 + cd "apache-solr-#{SOLR_VERSION}/example"
12 cp_r ['../LICENSE.txt', '../NOTICE.txt', 'README.txt', 'etc', 'lib', 'start.jar', 'webapps', 'work'], Rails.root + '/vendor/plugins/acts_as_solr_reloaded/solr', :verbose => true 20 cp_r ['../LICENSE.txt', '../NOTICE.txt', 'README.txt', 'etc', 'lib', 'start.jar', 'webapps', 'work'], Rails.root + '/vendor/plugins/acts_as_solr_reloaded/solr', :verbose => true
13 cd 'solr' 21 cd 'solr'
14 cp_r ['README.txt', 'bin', 'solr.xml'], Rails.root + '/vendor/plugins/acts_as_solr_reloaded/solr/solr', :verbose => true 22 cp_r ['README.txt', 'bin', 'solr.xml'], Rails.root + '/vendor/plugins/acts_as_solr_reloaded/solr/solr', :verbose => true
@@ -27,7 +35,7 @@ namespace :solr do @@ -27,7 +35,7 @@ namespace :solr do
27 end 35 end
28 36
29 desc 'Starts Solr. Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.' 37 desc 'Starts Solr. Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.'
30 - task :start => :environment do 38 + task :start => [:download, :environment] do
31 require File.expand_path("#{File.dirname(__FILE__)}/../../config/solr_environment") 39 require File.expand_path("#{File.dirname(__FILE__)}/../../config/solr_environment")
32 FileUtils.mkdir_p(SOLR_LOGS_PATH) 40 FileUtils.mkdir_p(SOLR_LOGS_PATH)
33 FileUtils.mkdir_p(SOLR_DATA_PATH) 41 FileUtils.mkdir_p(SOLR_DATA_PATH)
@@ -62,7 +70,7 @@ namespace :solr do @@ -62,7 +70,7 @@ namespace :solr do
62 end 70 end
63 71
64 desc 'Stops Solr. Specify the environment by using: RAILS_ENV=your_env. Defaults to development if none.' 72 desc 'Stops Solr. Specify the environment by using: RAILS_ENV=your_env. Defaults to development if none.'
65 - task :stop=> :environment do 73 + task :stop => :environment do
66 require File.expand_path("#{File.dirname(__FILE__)}/../../config/solr_environment") 74 require File.expand_path("#{File.dirname(__FILE__)}/../../config/solr_environment")
67 fork do 75 fork do
68 if File.exists?(SOLR_PID_FILE) 76 if File.exists?(SOLR_PID_FILE)