You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by eh...@apache.org on 2007/03/07 04:06:01 UTC
svn commit: r515436 - in /lucene/solr/trunk/client/ruby/solr-ruby:
lib/solr/document.rb lib/solr/field.rb test/unit/document_test.rb
test/unit/field_test.rb
Author: ehatcher
Date: Tue Mar 6 19:06:00 2007
New Revision: 515436
URL: http://svn.apache.org/viewvc?view=rev&rev=515436
Log:
SOLR-171: Add per-doc and per-field boosts. (contributed by Coda Hale)
Modified:
lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/document.rb
lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/field.rb
lucene/solr/trunk/client/ruby/solr-ruby/test/unit/document_test.rb
lucene/solr/trunk/client/ruby/solr-ruby/test/unit/field_test.rb
Modified: lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/document.rb
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/document.rb?view=diff&rev=515436&r1=515435&r2=515436
==============================================================================
--- lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/document.rb (original)
+++ lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/document.rb Tue Mar 6 19:06:00 2007
@@ -15,6 +15,7 @@
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 @@
# 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
Modified: lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/field.rb
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/field.rb?view=diff&rev=515436&r1=515435&r2=515436
==============================================================================
--- lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/field.rb (original)
+++ lucene/solr/trunk/client/ruby/solr-ruby/lib/solr/field.rb Tue Mar 6 19:06:00 2007
@@ -14,13 +14,16 @@
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 <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_s
end
@@ -28,6 +31,7 @@
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
Modified: lucene/solr/trunk/client/ruby/solr-ruby/test/unit/document_test.rb
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/ruby/solr-ruby/test/unit/document_test.rb?view=diff&rev=515436&r1=515435&r2=515436
==============================================================================
--- lucene/solr/trunk/client/ruby/solr-ruby/test/unit/document_test.rb (original)
+++ lucene/solr/trunk/client/ruby/solr-ruby/test/unit/document_test.rb Tue Mar 6 19:06:00 2007
@@ -55,5 +55,11 @@
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(/<doc boost=['"]300.28['"]>[\s]+<field name=['"]name['"]>McGrump<\/field>[\s]+<\/doc>/, doc.to_xml.to_s)
+ end
end
Modified: lucene/solr/trunk/client/ruby/solr-ruby/test/unit/field_test.rb
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/ruby/solr-ruby/test/unit/field_test.rb?view=diff&rev=515436&r1=515435&r2=515436
==============================================================================
--- lucene/solr/trunk/client/ruby/solr-ruby/test/unit/field_test.rb (original)
+++ lucene/solr/trunk/client/ruby/solr-ruby/test/unit/field_test.rb Tue Mar 6 19:06:00 2007
@@ -39,4 +39,10 @@
assert_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