Apache Solr open-source search software
SOLR-171: Add per-doc and per-field boosts. (contributed by Coda Hale)
Erik Hatcherdc7a3984f1e0
diff --git a/client/ruby/solr-ruby/lib/solr/document.rb b/client/ruby/solr-ruby/lib/solr/document.rbindex 4f037d65ea7..f714e769f3e 100644--- a/client/ruby/solr-ruby/lib/solr/document.rb+++ b/client/ruby/solr-ruby/lib/solr/document.rb@@ -15,6 +15,7 @@ require 'solr/field'class Solr::Documentinclude Enumerable+ attr_accessor :boost# Create a new Solr::Document, optionally passing in a hash of# key/value pairs for the fields@@ -65,6 +66,7 @@ class Solr::Document# convert the Document to a REXML::Elementdef to_xmle = Solr::XML::Element.new 'doc'+ e.attributes['boost'] = @boost.to_s if @boost@fields.each {|f| e.add_element(f.to_xml)}return eenddiff --git a/client/ruby/solr-ruby/lib/solr/field.rb b/client/ruby/solr-ruby/lib/solr/field.rbindex b252ad7ebb9..0731d0e0afd 100644--- a/client/ruby/solr-ruby/lib/solr/field.rb+++ b/client/ruby/solr-ruby/lib/solr/field.rb@@ -14,13 +14,16 @@ require 'solr/xml'require 'time'class Solr::Field+ VALID_PARAMS = [:boost]attr_accessor :nameattr_accessor :value+ attr_accessor :boost- def initialize(key_val, opts={})- raise "first argument must be a hash" unless key_val.kind_of? Hash- @name = key_val.keys[0].to_s- @value = key_val.values[0]+ # Accepts an optional <tt>:boost</tt> 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]# Convert any Time values into UTC/XML schema format (which Solr requires).@value = @value.respond_to?(:utc) ? @value.utc.xmlschema : @value.to_send@@ -28,6 +31,7 @@ class Solr::Fielddef to_xmle = Solr::XML::Element.new 'field'e.attributes['name'] = @name+ e.attributes['boost'] = @boost.to_s if @booste.text = @valuereturn eenddiff --git a/client/ruby/solr-ruby/test/unit/document_test.rb b/client/ruby/solr-ruby/test/unit/document_test.rbindex 09a751155bc..758834c479f 100644--- a/client/ruby/solr-ruby/test/unit/document_test.rb+++ b/client/ruby/solr-ruby/test/unit/document_test.rb@@ -55,5 +55,11 @@ class DocumentTest < Test::Unit::TestCaseassert_equal 'Lucene in Action', doc[:title]assert_equal 'Search', doc[:subject]end++ def test_boost+ doc = Solr::Document.new :name => "McGrump"+ doc.boost = 300.28+ assert_match(/<doc boost=['"]300.28['"]>[\s]+<field name=['"]name['"]>McGrump<\/field>[\s]+<\/doc>/, doc.to_xml.to_s)+ endenddiff --git a/client/ruby/solr-ruby/test/unit/field_test.rb b/client/ruby/solr-ruby/test/unit/field_test.rbindex ca8d3630654..44e6d5c4c27 100644--- a/client/ruby/solr-ruby/test/unit/field_test.rb+++ b/client/ruby/solr-ruby/test/unit/field_test.rb@@ -39,4 +39,10 @@ class FieldTest < Test::Unit::TestCaseassert_match(/<field name=["']i18nstring["']>Äêâîôû Öëäïöü<\/field>/m, field.to_xml.to_s)end+ def test_boost_values+ field = Solr::Field.new(:blah => "squee", :boost => 3.0)+ assert_kind_of Solr::XML::Element, field.to_xml+ assert_match(/<field name=["']blah["'] boost=["']3.0["']>squee<\/field>/, field.to_xml.to_s)+ end+end