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
This commit is contained in:
Erik Hatcher 2007-03-07 03:06:00 +00:00
parent ccadb5498c
commit dc7a3984f1
4 changed files with 22 additions and 4 deletions

View File

@ -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

View File

@ -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 <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 @@ 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

View File

@ -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(/<doc boost=['"]300.28['"]>[\s]+<field name=['"]name['"]>McGrump<\/field>[\s]+<\/doc>/, doc.to_xml.to_s)
end
end

View File

@ -39,4 +39,10 @@ class FieldTest < Test::Unit::TestCase
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