From dc7a3984f1e0c8ab208e918a3b7989c60cfdd2e9 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Wed, 7 Mar 2007 03:06:00 +0000 Subject: [PATCH] SOLR-171: Add per-doc and per-field boosts. (contributed by Coda Hale) git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@515436 13f79535-47bb-0310-9956-ffa450edef68 --- client/ruby/solr-ruby/lib/solr/document.rb | 2 ++ client/ruby/solr-ruby/lib/solr/field.rb | 12 ++++++++---- client/ruby/solr-ruby/test/unit/document_test.rb | 6 ++++++ client/ruby/solr-ruby/test/unit/field_test.rb | 6 ++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/client/ruby/solr-ruby/lib/solr/document.rb b/client/ruby/solr-ruby/lib/solr/document.rb index 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::Document include 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::Element def to_xml e = Solr::XML::Element.new 'doc' + e.attributes['boost'] = @boost.to_s if @boost @fields.each {|f| e.add_element(f.to_xml)} return e end diff --git a/client/ruby/solr-ruby/lib/solr/field.rb b/client/ruby/solr-ruby/lib/solr/field.rb index 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 :name attr_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 :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] # Convert any Time values into UTC/XML schema format (which Solr requires). @value = @value.respond_to?(:utc) ? @value.utc.xmlschema : @value.to_s end @@ -28,6 +31,7 @@ class Solr::Field def to_xml e = Solr::XML::Element.new 'field' e.attributes['name'] = @name + e.attributes['boost'] = @boost.to_s if @boost e.text = @value return e end diff --git a/client/ruby/solr-ruby/test/unit/document_test.rb b/client/ruby/solr-ruby/test/unit/document_test.rb index 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::TestCase assert_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(/[\s]+McGrump<\/field>[\s]+<\/doc>/, doc.to_xml.to_s) + end end diff --git a/client/ruby/solr-ruby/test/unit/field_test.rb b/client/ruby/solr-ruby/test/unit/field_test.rb index 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::TestCase assert_match(/Äêâîôû Öëäïöü<\/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(/squee<\/field>/, field.to_xml.to_s) + end + end