diff --git a/vendor/plugins/acts_as_solr_reloaded/.gitignore b/vendor/plugins/acts_as_solr_reloaded/.gitignore index c855325..95c7b4a 100644 --- a/vendor/plugins/acts_as_solr_reloaded/.gitignore +++ b/vendor/plugins/acts_as_solr_reloaded/.gitignore @@ -1,17 +1,29 @@ +# inside solr +solr/LICENSE.txt +solr/NOTICE.txt +solr/README.txt +solr/etc/ +solr/work/ +solr/lib/ +solr/solr/README.txt +solr/solr/solr.xml +solr/start.jar +solr/webapps/ +solr/solr/data/* + +acts_as_solr_reloaded-*.gem *.log *.log *_pid + +coverage coverage/* coverage.data -solr/solr/data/* -.svn test/db/*.db test/log/*.log pkg/* *.sw? -.DS_Store -coverage rdoc pkg -acts_as_solr_reloaded-*.gem tags +.svn diff --git a/vendor/plugins/acts_as_solr_reloaded/README.markdown b/vendor/plugins/acts_as_solr_reloaded/README.markdown index 9d2f240..2bdd8f9 100644 --- a/vendor/plugins/acts_as_solr_reloaded/README.markdown +++ b/vendor/plugins/acts_as_solr_reloaded/README.markdown @@ -13,19 +13,20 @@ Install as a plugin script/plugin install git://github.com/brauliobo/acts_as_solr_reloaded.git -Download Solr 3.1 +Download Solr 3.5 rake solr:download Requirements ------ -* Java Runtime Environment(JRE) 1.6 aka 6.0 or newer [http://www.java.com/en/download/index.jsp](http://www.java.com/en/download/index.jsp) (use default-jre for Debian like distribution) -* (Recommended) If you have libxml-ruby installed, make sure it's at least version 0.7 +* Java Runtime Environment(JRE) 6.0 (or newer) from Oracle or OpenJDK Configuration ====== See config/solr.yml file. +For solr configuration the important files are solrconfig.xml and schema.xml. + Basic Usage ======
@@ -42,13 +43,21 @@ Basic Usage
+Pagination
+======
+ActsAsSolr implements in SearchResults class an interface compatible with will_paginate and maybe others.
-`acts_as_solr` in your tests
+In your tests
======
-To test code that uses `acts_as_solr` you must start a Solr server for the test environment. You can do that with `rake solr:start RAILS_ENV=test`
+To test code that uses `acts_as_solr` you must start a Solr server for the test environment.
+You can add to the beggining of your test/test_helper.rb the code:
+
+ENV["RAILS_ENV"] = "test"
+abort unless system 'rake solr:start'
+at_exit { system 'rake solr:stop' }
+
However, if you would like to mock out Solr calls so that a Solr server is not needed (and your tests will run much faster), just add this to your `test_helper.rb` or similar:
-
class ActsAsSolr::Post
def self.execute(request)
@@ -57,8 +66,6 @@ class ActsAsSolr::Post
end
-([via](http://www.subelsky.com/2007/10/actsassolr-capistranhttpwwwbloggercomim.html#c1646308013209805416))
-
Release Information
======
Released under the MIT license.
diff --git a/vendor/plugins/acts_as_solr_reloaded/README.rdoc b/vendor/plugins/acts_as_solr_reloaded/README.rdoc
deleted file mode 100644
index 08d77e0..0000000
--- a/vendor/plugins/acts_as_solr_reloaded/README.rdoc
+++ /dev/null
@@ -1,96 +0,0 @@
-= DESCRIPTION
-
-This plugin adds full text search capabilities and many other nifty features from Apache's Solr[http://lucene.apache.org/solr/] to any Rails model, like:
-
-* faceting
-* dynamic attributes
-* integration with acts_as_taggable_on
-* integration with will_paginate
-* highlighting
-* geolocation
-* relevance
-* suggest
-
-Watch this screencast for a short demo of the latests features:
-
-http://www.vimeo.com/8728276
-
-== INSTALLATION
-
- script/plugin install git://github.com/brauliobo/acts_as_solr_reloaded.git
- rake solr:download
-
-== REQUIREMENTS
-
-* Java Runtime Environment(JRE) 1.5 aka 5.0 [http://www.java.com/en/download/index.jsp](http://www.java.com/en/download/index.jsp) (use default-jre for Debian like distribution)
-* (Recommended) If you have libxml-ruby installed, make sure it's at least version 0.7
-
-== CONFIGURATION
-
-See config/solr.yml file.
-
-== USAGE
-
-Just include the line below to any of your ActiveRecord models:
- acts_as_solr
-
-Or if you want, you can specify only the fields that should be indexed:
- acts_as_solr :fields => [:name, :author]
-
-Then to find instances of your model, just do:
- Model.search(query) #query is a string representing your query
-
-Case you want to use dynamic attributes or geolocalization, you can use this generators that setup the database:
-
- script/generate dynamic_attributes_migration
- script/generate local_migration
-
-and then configure your model like this:
-
- acts_as_solr :dynamic_attributes => true,
- :spatial => true
-
-If you want to integrate the model with acts_as_taggable_on, just add the option :taggable => true :
-
- acts_as_solr :taggable => true
-
-Please see ActsAsSolr::ActsMethods for a complete info
-
-== PAGINATION
-
-In your controller:
-
- @search = Product.search "beer", :page => 2, :per_page => 20
-
-And in your view:
-
- will_paginate @search
-
-== TESTING
-
-To test code that uses acts_as_solr_reloaded you must start a Solr server for the test environment and also start MongoDB.
-
-== LICENSE
-
-(The MIT License)
-
-Copyright © 2010
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-‘Software’), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/plugins/acts_as_solr_reloaded/config/solr.yml b/vendor/plugins/acts_as_solr_reloaded/config/solr.yml
index 79779cf..b0c88c5 100644
--- a/vendor/plugins/acts_as_solr_reloaded/config/solr.yml
+++ b/vendor/plugins/acts_as_solr_reloaded/config/solr.yml
@@ -1,14 +1,22 @@
-# Config file for the acts_as_solr_reloaded plugin.
+# Config file for the acts_as_solr plugin.
#
# If you change the host or port number here, make sure you update
# them in your Solr config file
development:
- url: http://127.0.0.1:8982/solr
-
-test:
- url: http://127.0.0.1:8981/solr
+ url: http://0.0.0.0:8982/solr
+ jvm_options: -server -Xmx128M -Xms16M
+ timeout: 0
production:
url: http://127.0.0.1:8983/solr
- jvm_options: -server -d64 -Xmx1024M -Xms64M
\ No newline at end of file
+ jvm_options: -server -Xmx192M -Xms64M
+ timeout: 0
+
+test: &TEST
+ url: http://0.0.0.0:8981/solr
+ jvm_options: -server -Xmx128M -Xms16M
+ timeout: 0
+
+cucumber:
+ <<: *TEST
diff --git a/vendor/plugins/acts_as_solr_reloaded/config/solr_environment.rb b/vendor/plugins/acts_as_solr_reloaded/config/solr_environment.rb
index b5d729b..1c9dbbc 100644
--- a/vendor/plugins/acts_as_solr_reloaded/config/solr_environment.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/config/solr_environment.rb
@@ -27,7 +27,7 @@ end
unless defined? SOLR_PORT
config = YAML::load_file(RAILS_ROOT+'/config/solr.yml')
- raise("No solr environment defined for RAILS_ENV the #{ENV['RAILS_ENV'].inspect}") unless config[ENV['RAILS_ENV']]
+ raise("No solr environment defined for RAILS_ENV = #{ENV['RAILS_ENV'].inspect}") unless config[ENV['RAILS_ENV']]
SOLR_HOST = ENV['HOST'] || URI.parse(config[ENV['RAILS_ENV']]['url']).host
SOLR_PORT = ENV['PORT'] || URI.parse(config[ENV['RAILS_ENV']]['url']).port
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr.rb
index 3a75756..468a47f 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr.rb
@@ -19,36 +19,7 @@ require File.dirname(__FILE__) + '/acts_as_solr/deprecation'
require File.dirname(__FILE__) + '/acts_as_solr/search_results'
require File.dirname(__FILE__) + '/acts_as_solr/lazy_document'
require File.dirname(__FILE__) + '/acts_as_solr/mongo_mapper'
-
-module ActsAsSolr
- class Post
- class << self
- def config
- return @config if @config
- @config = {}
- YAML::load_file("#{Rails.root}/config/solr.yml")[Rails.env].each{ |k,v| @config[k.to_sym] = v }
- @config
- end
-
- def options
- @options ||= credentials.merge config
- end
-
- def credentials
- @credentials ||= {:username => config[:username], :password => config[:password]}
- end
-
- def url(core)
- core.nil? ? config[:url] : "#{config[:url]}/#{core}"
- end
-
- def execute(request, core = nil)
- connection = Solr::Connection.new(url(core), options)
- connection.send request
- end
- end
- end
-end
+require File.dirname(__FILE__) + '/acts_as_solr/post'
# reopen ActiveRecord and include the acts_as_solr method
ActiveRecord::Base.extend ActsAsSolr::ActsMethods
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/acts_methods.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/acts_methods.rb
index 6a6e406..7bf1763 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/acts_methods.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/acts_methods.rb
@@ -164,8 +164,11 @@ module ActsAsSolr #:nodoc:
# dynamic_attributes: Default false. When true, requires a has_many relationship to a DynamicAttribute
# (:name, :value) model. Then, all dynamic attributes will be mapped as normal attributes
# in Solr, so you can filter like this: Model.find_by_solr "#{dynamic_attribute.name}:Lorem"
+ #
# taggable: Default false. When true, indexes tags with field name tag. Tags are taken from taggings.tag
+ #
# spatial: Default false. When true, indexes model.local.latitude and model.local.longitude as coordinates.
+ #
def acts_as_solr(options={}, solr_options={}, &deferred_solr_configuration)
$solr_indexed_models << self
@@ -262,20 +265,23 @@ module ActsAsSolr #:nodoc:
:additional_fields => nil,
:dynamic_attributes => false,
:exclude_fields => [],
- :auto_commit => true,
+ :auto_commit => ['production'].include?(Rails.env) ? false : true,
:include => nil,
:facets => nil,
:boost => nil,
:if => "true",
:offline => false,
- :spatial => false
+ :spatial => false,
}
self.solr_configuration = {
:type_field => "type_s",
:primary_key_field => "pk_s",
- :default_boost => 1.0
+ :default_boost => 1.0,
}
+ raise "Invalid options: #{(options.keys-configuration.keys).join(',')}" unless (options.keys-configuration.keys).empty?
+ raise "Invalid solr options: #{(solr_options.keys-solr_configuration.keys).join(',')}" unless (solr_options.keys-solr_configuration.keys).empty?
+
configuration.update(options) if options.is_a?(Hash)
solr_configuration.update(solr_options) if solr_options.is_a?(Hash)
Deprecation.validate_index(configuration)
@@ -290,8 +296,18 @@ module ActsAsSolr #:nodoc:
process_fields(configuration[:additional_fields])
end
- if configuration[:include].respond_to?(:each)
- process_includes(configuration[:include])
+ process_includes(configuration[:include]) if configuration[:include]
+ end
+
+ def after_save_reindex(associations, options = {})
+ Array(associations).each do |association|
+ after_save do |ar|
+ if options[:with] == :delayed_job
+ delay(:run_at => (Date.today+1.day).to_time+4.hours).solr_batch_add_association ar, association
+ else
+ solr_batch_add_association ar, association
+ end
+ end
end
end
@@ -301,19 +317,13 @@ module ActsAsSolr #:nodoc:
field_name, options = determine_field_name_and_options(field)
configuration[:solr_fields][field_name] = options
- define_method("#{field_name}_for_solr".to_sym) do
- begin
- value = self[field_name] || self.instance_variable_get("@#{field_name.to_s}".to_sym) || self.send(field_name.to_sym)
- case options[:type]
- # format dates properly; return nil for nil dates
- when :date
- value ? (value.respond_to?(:utc) ? value.utc : value).strftime("%Y-%m-%dT%H:%M:%SZ") : nil
- else value
- end
- rescue
- puts $!
- logger.debug "There was a problem getting the value for the field '#{field_name}': #{$!}"
- value = ''
+ define_method "#{field_name}_for_solr" do
+ value = self.send field_name
+ case options[:type]
+ when :date # format dates properly; return nil for nil dates
+ value ? (value.respond_to?(:utc) ? value.utc : value).strftime("%Y-%m-%dT%H:%M:%SZ") : nil
+ else
+ value
end
end
end
@@ -328,11 +338,9 @@ module ActsAsSolr #:nodoc:
end
def process_includes(includes)
- if includes.respond_to?(:each)
- includes.each do |assoc|
- field_name, options = determine_field_name_and_options(assoc)
- configuration[:solr_includes][field_name] = options
- end
+ Array(includes).each do |assoc|
+ field_name, options = determine_field_name_and_options(assoc)
+ configuration[:solr_includes][field_name] = options
end
end
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/class_methods.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/class_methods.rb
index dc74227..c0eb4c3 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/class_methods.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/class_methods.rb
@@ -197,7 +197,7 @@ module ActsAsSolr #:nodoc:
data = parse_query(query, options)
data.total_hits
end
-
+
# It's used to rebuild the Solr index for a specific model.
# Book.rebuild_solr_index
#
@@ -209,39 +209,75 @@ module ActsAsSolr #:nodoc:
# If a finder block is given, it will be called to retrieve the items to index.
# This can be very useful for things such as updating based on conditions or
# using eager loading for indexed associations.
- def rebuild_solr_index(batch_size=100, options = {}, &finder)
- finder ||= lambda { |ar, options| ar.find(:all, options.merge({:order => self.primary_key})) }
+ def rebuild_solr_index(batch_size=300, options = {}, &finder)
+ finder ||= lambda do |ar, sql_options|
+ ar.all sql_options.merge!({:order => self.primary_key, :include => configuration[:solr_includes].keys})
+ end
start_time = Time.now
options[:offset] ||= 0
+ options[:threads] ||= 2
+ options[:delayed_job] &= defined?(Delayed::Job)
if batch_size > 0
items_processed = 0
- limit = batch_size
offset = options[:offset]
- begin
- iteration_start = Time.now
- items = finder.call(self, {:limit => limit, :offset => offset})
+ end_reached = false
+ threads = []
+ mutex = Mutex.new
+ queue = Queue.new
+ loop do
+ items = finder.call(self, {:limit => batch_size, :offset => offset})
add_batch = items.collect { |content| content.to_solr_doc }
-
- if items.size > 0
- solr_add add_batch
- solr_commit
- end
-
- items_processed += items.size
- last_id = items.last.id if items.last
- time_so_far = Time.now - start_time
- iteration_time = Time.now - iteration_start
- logger.info "#{Process.pid}: #{items_processed} items for #{self.name} have been batch added to index in #{'%.3f' % time_so_far}s at #{'%.3f' % (items_processed / time_so_far)} items/sec (#{'%.3f' % (items.size / iteration_time)} items/sec for the last batch). Last id: #{last_id}"
offset += items.size
- end while items.nil? || items.size > 0
+ end_reached = items.size == 0
+ break if end_reached
+
+ if options[:threads] == threads.size
+ threads.first.join
+ threads.shift
+ end
+
+ queue << [items, add_batch]
+ threads << Thread.new do
+ iteration_start = Time.now
+
+ iteration_items, iteration_add_batch = queue.pop(true)
+ if options[:delayed_job]
+ delay.solr_add iteration_add_batch
+ else
+ solr_add iteration_add_batch
+ solr_commit
+ end
+
+ last_id = iteration_items.last.id
+ time_so_far = Time.now - start_time
+ iteration_time = Time.now - iteration_start
+ mutex.synchronize do
+ items_processed += iteration_items.size
+ if options[:delayed_job]
+ logger.info "#{Process.pid}: #{items_processed} items for #{self.name} have been sent to Delayed::Job in #{'%.3f' % time_so_far}s at #{'%.3f' % (items_processed / time_so_far)} items/sec. Last id: #{last_id}"
+ else
+ logger.info "#{Process.pid}: #{items_processed} items for #{self.name} have been batch added to index in #{'%.3f' % time_so_far}s at #{'%.3f' % (items_processed / time_so_far)} items/sec. Last id: #{last_id}"
+ end
+ end
+ end
+ end
+
+ solr_commit if options[:delayed_job]
+ threads.each{ |t| t.join }
else
items = finder.call(self, {})
items.each { |content| content.solr_save }
items_processed = items.size
end
- solr_optimize
- logger.info items_processed > 0 ? "Index for #{self.name} has been rebuilt" : "Nothing to index for #{self.name}"
+
+ if items_processed > 0
+ solr_optimize
+ time_elapsed = Time.now - start_time
+ logger.info "Index for #{self.name} has been rebuilt (took #{'%.3f' % time_elapsed}s)"
+ else
+ "Nothing to index for #{self.name}"
+ end
end
alias :rebuild_index :rebuild_solr_index
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/common_methods.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/common_methods.rb
index 4ad7022..5c3a4f3 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/common_methods.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/common_methods.rb
@@ -1,23 +1,25 @@
module ActsAsSolr #:nodoc:
module CommonMethods
-
+
+ TypeMapping = {
+ :double => "do",
+ :float => "f",
+ :decimal => "f",
+ :integer => "i",
+ :boolean => "b",
+ :string => "s",
+ :date => "d",
+ :range_float => "rf",
+ :range_integer => "ri",
+ :facet => "facet",
+ :text => "t",
+ }
+
# Converts field types into Solr types
def get_solr_field_type(field_type)
if field_type.is_a?(Symbol)
- h = {
- :float => "f",
- :decimal => "f",
- :integer => "i",
- :boolean => "b",
- :string => "s",
- :date => "d",
- :range_float => "rf",
- :range_integer => "ri",
- :facet => "facet",
- :text => "t",
- }
- t = h[field_type]
+ t = TypeMapping[field_type]
raise "Unknown field_type symbol: #{field_type}" if t.nil?
t
elsif field_type.is_a?(String)
@@ -34,7 +36,7 @@ module ActsAsSolr #:nodoc:
return "false"
when "s", "t", "d", :date, :string, :text
return ""
- when "f", "rf", :float, :range_float
+ when "f", "rf", :float, :range_float, :double, :decimal
return 0.00
when "i", "ri", :integer, :range_integer
return 0
@@ -42,6 +44,14 @@ module ActsAsSolr #:nodoc:
return ""
end
end
+
+ def solr_batch_add(objects)
+ solr_add Array(objects).map{ |a| a.to_solr_doc }
+ end
+
+ def solr_batch_add_association(ar, association)
+ solr_batch_add ar.send(association)
+ end
# Sends an add command to Solr
def solr_add(add_xml)
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/deprecation.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/deprecation.rb
index 94d2194..a6343f0 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/deprecation.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/deprecation.rb
@@ -30,7 +30,7 @@ module ActsAsSolr #:nodoc:
end
if options[:sort_by]
plog "The option :sort_by is deprecated, use :order instead!"
- options[:order] ||= options[:sort_by]
+ options[:sort] ||= options[:sort_by]
end
if options[:start]
plog "The option :start is deprecated, use :offset instead!"
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/instance_methods.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/instance_methods.rb
index 31031e8..53f8cbb 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/instance_methods.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/instance_methods.rb
@@ -45,8 +45,6 @@ module ActsAsSolr #:nodoc:
# iterate through the fields and add them to the document,
configuration[:solr_fields].each do |field_name, options|
- #field_type = configuration[:facets] && configuration[:facets].include?(field) ? :facet : :text
-
field_boost = options[:boost] || solr_configuration[:default_boost]
field_type = get_solr_field_type(options[:type])
solr_name = options[:as] || field_name
@@ -66,8 +64,10 @@ module ActsAsSolr #:nodoc:
next if value.nil? || value.to_s.strip.empty?
[value].flatten.each do |v|
v = set_value_if_nil(suffix) if value.to_s == ""
- field = Solr::Field.new("#{solr_name}_#{suffix}" => ERB::Util.html_escape(v.to_s))
- field.boost = validate_boost(field_boost)
+
+ field = Solr::Field.new(:name => "#{solr_name}_#{suffix}", :value => ERB::Util.html_escape(v.to_s))
+ processed_boost = validate_boost(field_boost)
+ field.boost = processed_boost if processed_boost != solr_configuration[:default_boost]
doc << field
end
end
@@ -78,38 +78,38 @@ module ActsAsSolr #:nodoc:
add_tags(doc)
add_space(doc)
- debug doc.to_xml
+ debug doc.to_json
doc
end
-
+
private
def debug(text)
logger.debug text rescue nil
end
-
+
def add_space(doc)
if configuration[:spatial] and local
- doc << Solr::Field.new("lat" => local.latitude)
- doc << Solr::Field.new("lng" => local.longitude)
+ doc << Solr::Field.new(:name => "lat", :value => local.latitude)
+ doc << Solr::Field.new(:name => "lng", :value => local.longitude)
end
end
def add_tags(doc)
taggings.each do |tagging|
- doc << Solr::Field.new("tag_facet" => tagging.tag.name)
- doc << Solr::Field.new("tag_t" => tagging.tag.name)
+ doc << Solr::Field.new(:name => "tag_facet", :value => tagging.tag.name)
+ doc << Solr::Field.new(:name => "tag_t", :value => tagging.tag.name)
end if configuration[:taggable]
end
def add_dynamic_attributes(doc)
dynamic_attributes.each do |attribute|
value = ERB::Util.html_escape(attribute.value)
- doc << Solr::Field.new("#{attribute.name}_t" => value)
- doc << Solr::Field.new("#{attribute.name}_facet" => value)
+ doc << Solr::Field.new(:name => "#{attribute.name}_t", :value => value)
+ doc << Solr::Field.new(:name => "#{attribute.name}_facet", :value => value)
end if configuration[:dynamic_attributes]
end
-
+
def add_includes(doc)
if configuration[:solr_includes].respond_to?(:each)
configuration[:solr_includes].each do |association, options|
@@ -120,12 +120,13 @@ module ActsAsSolr #:nodoc:
suffix = get_solr_field_type(field_type)
case self.class.reflect_on_association(association).macro
when :has_many, :has_and_belongs_to_many
- records = self.send(association).to_a
+ records = self.send(association).compact
unless records.empty?
records.each {|r| data << include_value(r, options)}
- [data].flatten.each do |value|
- field = Solr::Field.new("#{field_name}_#{suffix}" => value)
- field.boost = validate_boost(field_boost)
+ Array(data).each do |value|
+ field = Solr::Field.new(:name => "#{field_name}_#{suffix}", :value => value)
+ processed_boost = validate_boost(field_boost)
+ field.boost = processed_boost if processed_boost != solr_configuration[:default_boost]
doc << field
end
end
@@ -145,7 +146,13 @@ module ActsAsSolr #:nodoc:
elsif options[:using].is_a? Symbol
record.send(options[:using])
else
- record.attributes.inject([]){|k,v| k << "#{v.first}=#{ERB::Util.html_escape(v.last)}"}.join(" ")
+ if options[:fields]
+ fields = {}
+ options[:fields].each{ |f| fields[f] = record.send(f) }
+ else
+ fields = record.attributes
+ end
+ fields.map{ |k,v| ERB::Util.html_escape(v) }.join(" ")
end
end
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/parser_methods.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/parser_methods.rb
index 98a2a4d..ea9306c 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/parser_methods.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/parser_methods.rb
@@ -4,9 +4,10 @@ module ActsAsSolr #:nodoc:
# Method used by mostly all the ClassMethods when doing a search
def parse_query(query=nil, options={}, models=nil)
- valid_options = [ :filter_queries, :offset, :limit, :facets, :models, :results_format, :order,
- :scores, :operator, :include, :lazy, :joins, :select, :core,
- :around, :relevance, :highlight, :page, :per_page]
+ valid_options = [ :alternate_query, :boost_functions, :filter_queries, :facets, :models, :sort,
+ :scores, :operator, :lazy, :core, :latitude, :longitude, :radius, :relevance, :highlight,
+ :offset, :per_page, :limit, :page,
+ :results_format, :sql_options]
query_options = {}
field_list = models.nil? ? solr_configuration[:primary_key_field] : "id"
@@ -20,8 +21,7 @@ module ActsAsSolr #:nodoc:
query = sanitize_query(query)
query_options[:filter_queries].push(solr_type_condition)
end
- query_options[:query] = query
- order = options[:order].split(/\s*,\s*/).collect{|e| e.gsub(/\s+/,'_t ').gsub(/\bscore_t\b/, 'score') }.join(',') if options[:order]
+ query_options[:query] = "#{options[:alternate_query]} #{query}"
raise "Invalid parameters: #{(options.keys - valid_options).join(',')}" unless (options.keys - valid_options).empty?
begin
@@ -34,8 +34,8 @@ module ActsAsSolr #:nodoc:
query = add_relevance query, options[:relevance]
- raise "Expecting and array of strings for :filter_queries" unless options[:filter_queries].nil? or options[:filter_queries].kind_of?(Array)
- query_options[:filter_queries] += replace_types([*options[:filter_queries]].collect{|k| "#{k.dup.sub!(/ *: */,"_t:")}"}) if options[:filter_queries]
+ query_options[:filter_queries] += replace_types([*options[:filter_queries]], '') if options[:filter_queries]
+ query_options[:boost_functions] = replace_types([*options[:boost_functions]], '').join(' ') if options[:boost_functions]
# first steps on the facet parameter processing
if options[:facets]
@@ -47,9 +47,8 @@ module ActsAsSolr #:nodoc:
# override the :zeros (it's deprecated anyway) if :mincount exists
query_options[:facets][:mincount] = options[:facets][:mincount] if options[:facets][:mincount]
query_options[:facets][:fields] = options[:facets][:fields].collect{|k| "#{k}_facet"} if options[:facets][:fields]
- query_options[:filter_queries] += replace_types([*options[:facets][:browse]].collect{|k| "#{k.dup.sub!(/ *: */,"_t:")}"}) if options[:facets][:browse]
- query_options[:facets][:queries] = replace_types(options[:facets][:query].collect{|k| "#{k.dup.sub!(/ *: */,"_t:")}"}) if options[:facets][:query]
-
+ query_options[:filter_queries] += replace_types([*options[:facets][:browse]]) if options[:facets][:browse]
+ query_options[:facets][:queries] = replace_types(options[:facets][:query]) if options[:facets][:query]
if options[:facets][:dates]
query_options[:date_facets] = {}
@@ -82,28 +81,25 @@ module ActsAsSolr #:nodoc:
end
end
- if options[:highlight]
- query_options[:highlighting] = {}
- query_options[:highlighting][:field_list] = []
- query_options[:highlighting][:field_list] << options[:highlight][:fields].collect {|k| "#{k}_t"} if options[:highlight][:fields]
- query_options[:highlighting][:require_field_match] = options[:highlight][:require_field_match] if options[:highlight][:require_field_match]
- query_options[:highlighting][:max_snippets] = options[:highlight][:max_snippets] if options[:highlight][:max_snippets]
- query_options[:highlighting][:prefix] = options[:highlight][:prefix] if options[:highlight][:prefix]
- query_options[:highlighting][:suffix] = options[:highlight][:suffix] if options[:highlight][:suffix]
- end
-
- # TODO: set the sort parameter instead of the old ;order. style.
- query_options[:sort] = replace_types([order], false)[0] if options[:order]
-
- if options[:around]
- query_options[:radius] = options[:around][:radius]
- query_options[:latitude] = options[:around][:latitude]
- query_options[:longitude] = options[:around][:longitude]
+ if options[:highlight]
+ query_options[:highlighting] = {}
+ query_options[:highlighting][:field_list] = []
+ query_options[:highlighting][:field_list] << options[:highlight][:fields].collect {|k| "#{k}_t"} if options[:highlight][:fields]
+ query_options[:highlighting][:require_field_match] = options[:highlight][:require_field_match] if options[:highlight][:require_field_match]
+ query_options[:highlighting][:max_snippets] = options[:highlight][:max_snippets] if options[:highlight][:max_snippets]
+ query_options[:highlighting][:prefix] = options[:highlight][:prefix] if options[:highlight][:prefix]
+ query_options[:highlighting][:suffix] = options[:highlight][:suffix] if options[:highlight][:suffix]
end
- ActsAsSolr::Post.execute(Solr::Request::Standard.new(query_options), options[:core])
+ query_options[:sort] = replace_types([options[:sort]], '')[0] if options[:sort]
+
+ query_options[:radius] = options[:radius]
+ query_options[:latitude] = options[:latitude]
+ query_options[:longitude] = options[:longitude]
+
+ ActsAsSolr::Post.execute(Solr::Request::Dismax.new(query_options), options[:core])
rescue
- raise "There was a problem executing your search\n#{query_options.inspect}\n: #{$!} in #{$!.backtrace.first}"
+ raise "#{$query} There was a problem executing your search\n#{query_options.inspect}\n: #{$!} in #{$!.backtrace.first}"
end
end
@@ -158,11 +154,11 @@ module ActsAsSolr #:nodoc:
result = if configuration[:lazy] && configuration[:format] != :ids
ids.collect {|id| ActsAsSolr::LazyDocument.new(id, self)}
elsif configuration[:format] == :objects
- find_options = {}
- find_options[:include] = options[:include] if options[:include]
- find_options[:select] = options[:select] if options[:select]
- find_options[:joins] = options[:joins] if options[:joins]
- result = reorder(self.find(ids, find_options), ids)
+ find_options = options[:sql_options] || {}
+ find_options[:conditions] = self.send :merge_conditions, {:id => ids}, (find_options[:conditions] || [])
+ result = self.all(find_options)
+ result = reorder(result, ids) unless find_options[:order]
+ result
else
ids
end
@@ -182,14 +178,13 @@ module ActsAsSolr #:nodoc:
# Replaces the field types based on the types (if any) specified
# on the acts_as_solr call
- def replace_types(strings, include_colon=true)
- suffix = include_colon ? ":" : ""
+ def replace_types(strings, suffix=':')
if configuration[:solr_fields]
configuration[:solr_fields].each do |name, options|
- solr_name = options[:as] || name.to_s
+ solr_name = (options[:as] || name).to_s
solr_type = get_solr_field_type(options[:type])
field = "#{solr_name}_#{solr_type}#{suffix}"
- strings.each_with_index {|s,i| strings[i] = s.gsub(/#{solr_name.to_s}_t#{suffix}/,field) }
+ strings.each_with_index {|s,i| strings[i] = s.gsub(/\b#{solr_name}\b#{suffix}/,field) }
end
end
if configuration[:solr_includes]
@@ -197,7 +192,7 @@ module ActsAsSolr #:nodoc:
solr_name = options[:as] || association.to_s.singularize
solr_type = get_solr_field_type(options[:type])
field = "#{solr_name}_#{solr_type}#{suffix}"
- strings.each_with_index {|s,i| strings[i] = s.gsub(/#{solr_name.to_s}_t#{suffix}/,field) }
+ strings.each_with_index {|s,i| strings[i] = s.gsub(/\b#{solr_name}\b#{suffix}/,field) }
end
end
strings
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/post.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/post.rb
new file mode 100644
index 0000000..bd8250f
--- /dev/null
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/post.rb
@@ -0,0 +1,29 @@
+module ActsAsSolr
+ class Post
+ class << self
+ def config
+ return @config if @config
+ @config = {}
+ YAML::load_file("#{Rails.root}/config/solr.yml")[Rails.env].each{ |k,v| @config[k.to_sym] = v }
+ @config
+ end
+
+ def options
+ @options ||= credentials.merge config
+ end
+
+ def credentials
+ @credentials ||= {:username => config[:username], :password => config[:password]}
+ end
+
+ def url(core)
+ core.nil? ? config[:url] : "#{config[:url]}/#{core}"
+ end
+
+ def execute(request, core = nil)
+ connection = Solr::Connection.new(url(core), options)
+ connection.send request
+ end
+ end
+ end
+end
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/search_results.rb b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/search_results.rb
index c8220bf..c1221aa 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/search_results.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/acts_as_solr/search_results.rb
@@ -27,10 +27,17 @@ module ActsAsSolr #:nodoc:
#
#
class SearchResults
+
+ include Enumerable
+
def initialize(solr_data={})
@solr_data = solr_data
end
+ def each(&block)
+ self.results.each &block
+ end
+
# Returns an array with the instances. This method
# is also aliased as docs and records
def results
@@ -60,7 +67,7 @@ module ActsAsSolr #:nodoc:
# Returns the highlighted fields which one has asked for..
def highlights
- @solr_data[:highlights]
+ @solr_data[:highlights]
end
# Returns a suggested query
@@ -83,10 +90,45 @@ module ActsAsSolr #:nodoc:
(@solr_data[:start].to_i / per_page) + 1
end
+ def blank?
+ total_entries == 0
+ end
+
+ def size
+ total_entries
+ end
+
+ def offset
+ (current_page - 1) * per_page
+ end
+
+ def previous_page
+ if current_page > 1
+ current_page - 1
+ else
+ false
+ end
+ end
+
+ def next_page
+ if current_page < total_pages
+ current_page + 1
+ else
+ false
+ end
+ end
+
+ def method_missing(symbol, *args, &block)
+ self.results.send(symbol, *args, &block)
+ rescue NoMethodError
+ raise NoMethodError, "There is no method called #{symbol} at #{self.class.name} - #{self.inspect}"
+ end
+
alias docs results
alias records results
alias num_found total
alias total_hits total
+ alias total_entries total
alias highest_score max_score
end
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/solr/connection.rb b/vendor/plugins/acts_as_solr_reloaded/lib/solr/connection.rb
index 1adb92b..65c1ee3 100755
--- a/vendor/plugins/acts_as_solr_reloaded/lib/solr/connection.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/solr/connection.rb
@@ -40,8 +40,8 @@ class Solr::Connection
# Not actually opening the connection yet, just setting up the persistent connection.
@connection = Net::HTTP.new(@url.host, @url.port)
-
- @connection.read_timeout = opts[:timeout] if opts[:timeout]
+
+ @connection.read_timeout = opts[:timeout].to_i || 0
@username = opts[:username] if opts[:username]
@password = opts[:password] if opts[:password]
end
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/solr/document.rb b/vendor/plugins/acts_as_solr_reloaded/lib/solr/document.rb
index 3c91f4e..7d69631 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/solr/document.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/solr/document.rb
@@ -18,7 +18,7 @@ class Solr::Document
# Create a new Solr::Document, optionally passing in a hash of
# key/value pairs for the fields
#
- # doc = Solr::Document.new(:creator => 'Jorge Luis Borges')
+ # doc = Solr::Document.new(:name => :creator, :value => 'Jorge Luis Borges')
def initialize(hash={})
@fields = []
self << hash
@@ -26,19 +26,19 @@ class Solr::Document
# Append a Solr::Field
#
- # doc << Solr::Field.new(:creator => 'Jorge Luis Borges')
+ # doc << Solr::Field.new(:name => :creator, :value => 'Jorge Luis Borges')
#
# If you are truly lazy you can simply pass in a hash:
#
- # doc << {:creator => 'Jorge Luis Borges'}
+ # doc << {:name => :creator, :value => 'Jorge Luis Borges'}
def <<(fields)
case fields
when Hash
fields.each_pair do |name,value|
if value.respond_to?(:each) && !value.is_a?(String)
- value.each {|v| @fields << Solr::Field.new(name => v)}
+ value.each {|v| @fields << Solr::Field.new(:name => name, :value => v)}
else
- @fields << Solr::Field.new(name => value)
+ @fields << Solr::Field.new(:name => name, :value => value)
end
end
when Solr::Field
@@ -58,10 +58,20 @@ class Solr::Document
# shorthand to assign as a hash
def []=(name,value)
- @fields << Solr::Field.new(name => value)
+ @fields << Solr::Field.new(:name => name, :value => value)
+ end
+
+ def to_jsonhash
+ hash = {'doc' => {}}
+ hash['boost'] = @boost if @boost
+ @fields.each{ |f| hash['doc'][f.name] = f.value_to_jsonhash }
+ hash
+ end
+
+ def to_json
+ to_jsonhash.to_json
end
- # convert the Document to a REXML::Element
def to_xml
e = Solr::XML::Element.new 'doc'
e.attributes['boost'] = @boost.to_s if @boost
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/solr/field.rb b/vendor/plugins/acts_as_solr_reloaded/lib/solr/field.rb
index f7bc8dd..2601a5a 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/solr/field.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/solr/field.rb
@@ -18,18 +18,22 @@ class Solr::Field
# Accepts an optional :boost parameter, used to boost the relevance of a particular field.
def initialize(params)
- @boost = params[:boost]
- name_key = (params.keys - VALID_PARAMS).first
- @name, @value = name_key.to_s, params[name_key]
+ @boost = params[:boost] == 1.0 ? nil : params[:boost]
+ @name = params[:name].to_s
+ @value = params[:value]
# Convert any Time values into UTC/XML schema format (which Solr requires).
- @value = @value.respond_to?(:utc) ? @value.utc.xmlschema : @value.to_str
+ @value = @value.respond_to?(:utc) ? @value.utc.xmlschema : @value
end
+ def value_to_jsonhash
+ @boost.nil? ? @value : {'boost' => @boost, 'value' => @value}
+ end
+
def to_xml
e = Solr::XML::Element.new 'field'
e.attributes['name'] = @name
e.attributes['boost'] = @boost.to_s if @boost
- e.text = @value
+ e.text = @value.to_str
return e
end
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/add_document.rb b/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/add_document.rb
index 699d2dd..6e0b9af 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/add_document.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/add_document.rb
@@ -10,19 +10,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-class Solr::Request::AddDocument < Solr::Request::Update
+class Solr::Request::AddDocument < Solr::Request::JsonUpdate
# create the request, optionally passing in a Solr::Document
#
- # request = Solr::Request::AddDocument.new doc
+ # request = Solr::Request::AddDocument.new(doc)
#
# as a short cut you can pass in a Hash instead:
#
- # request = Solr::Request::AddDocument.new :creator => 'Jorge Luis Borges'
+ # request = Solr::Request::AddDocument.new(:creator => 'Jorge Luis Borges')
#
# or an array, to add multiple documents at the same time:
#
- # request = Solr::Request::AddDocument.new([doc1, doc2, doc3])
+ # request = Solr::Request::AddDocument.new([doc1, doc2, doc3]))
def initialize(doc={})
@docs = []
@@ -33,15 +33,23 @@ class Solr::Request::AddDocument < Solr::Request::Update
end
end
- # returns the request as a string suitable for posting
-
- def to_s
+ def to_json
+ '{' +
+ @docs.map{ |doc| "\"add\": #{doc.to_jsonhash.to_json}" }.join(',') +
+ '}'
+ end
+
+ def to_xml
e = Solr::XML::Element.new 'add'
for doc in @docs
e.add_element doc.to_xml
end
return e.to_s
end
+
+ def to_s
+ to_json
+ end
private
def add_doc(doc)
diff --git a/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/dismax.rb b/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/dismax.rb
index 13d1977..031a0fb 100644
--- a/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/dismax.rb
+++ b/vendor/plugins/acts_as_solr_reloaded/lib/solr/request/dismax.rb
@@ -13,34 +13,25 @@
class Solr::Request::Dismax < Solr::Request::Standard
VALID_PARAMS.replace(VALID_PARAMS + [:tie_breaker, :query_fields, :minimum_match, :phrase_fields, :phrase_slop,
- :boost_query, :boost_functions])
+ :alternate_query, :boost_query, :boost_functions])
def initialize(params)
- @alternate_query = params.delete(:alternate_query)
- @sort_values = params.delete(:sort)
-
- super
-
- @query_type = "dismax"
+ super("search")
end
def to_hash
hash = super
+ hash[:defType] = 'edismax'
hash[:tie] = @params[:tie_breaker]
hash[:mm] = @params[:minimum_match]
hash[:qf] = @params[:query_fields]
hash[:pf] = @params[:phrase_fields]
hash[:ps] = @params[:phrase_slop]
hash[:bq] = @params[:boost_query]
- hash[:bf] = @params[:boost_functions]
- hash["q.alt"] = @alternate_query
- # FIXME: 2007-02-13