add highlighting support to Standard request

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@505102 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2007-02-09 00:39:50 +00:00
parent 38e272fe23
commit 33d170b6bc
4 changed files with 52 additions and 4 deletions

View File

@ -13,7 +13,7 @@
class Solr::Request::Standard < Solr::Request::Select
VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows,
:filter_queries, :field_list, :debug_query, :explain_other, :facets]
:filter_queries, :field_list, :debug_query, :explain_other, :facets, :highlighting]
def initialize(params)
super('standard')
@ -38,8 +38,6 @@ class Solr::Request::Standard < Solr::Request::Select
@params[:rows] = params[:rows].to_i if params[:rows]
@params[:field_list] ||= ["*","score"]
#TODO model highlighting parameters: http://wiki.apache.org/solr/HighlightingParameters
end
def to_hash
@ -89,6 +87,18 @@ class Solr::Request::Standard < Solr::Request::Select
end
end
# highlighting parameter processing - http://wiki.apache.org/solr/HighlightingParameters
#TODO need to add per-field overriding to snippets, fragsize, requiredFieldMatch, formatting, and simple.pre/post
if @params[:highlighting]
hash[:hl] = true
hash["hl.fl"] = @params[:highlighting][:field_list].join(',') if @params[:highlighting][:field_list]
hash["hl.snippets"] = @params[:highlighting][:max_snippets]
hash["hl.requireFieldMatch"] = @params[:highlighting][:require_field_match]
hash["hl.simple.pre"] = @params[:highlighting][:prefix]
hash["hl.simple.post"] = @params[:highlighting][:suffix]
end
hash.merge(super.to_hash)
end

View File

@ -47,7 +47,10 @@ class Solr::Response::Standard < Solr::Response::Ruby
facets
end
def highlighted(id, field)
@data['highlighting'][id.to_s][field.to_s]
end
# supports enumeration of hits
# TODO revisit - should this iterate through *all* hits by re-requesting more?
def each

View File

@ -149,6 +149,22 @@ class ServerTest < Test::Unit::TestCase
assert info.field_names.include?("id") && info.field_names.include?("test_index_facet")
assert_equal 1, info.num_docs
end
def test_highlighting
@connection.add(:id => 1, :title_text => "Apache Solr")
request = Solr::Request::Standard.new(:query => 'solr',
:highlighting => {
:field_list => ['title_text'],
:max_snippets => 3,
:prefix => ">>",
:suffix => "<<"
}
)
response = @connection.send(request)
assert_equal ["Apache >>Solr<<"], response.highlighted(1, :title_text)
end
# wipe the index clean
def clean

View File

@ -85,5 +85,24 @@ class StandardRequestTest < Test::Unit::TestCase
request = Solr::Request::Standard.new(:query => 'query', :sort => [{:title => :descending}])
assert_equal 'query;title desc', request.to_hash[:q]
end
def test_highlighting
request = Solr::Request::Standard.new(:query => 'query',
:highlighting => {
:field_list => ['title', 'author'],
:max_snippets => 3,
:require_field_match => true,
:prefix => "<blink>",
:suffix => "</blink>"
}
)
hash = request.to_hash
assert_equal true, hash[:hl]
assert_equal "title,author", hash["hl.fl"]
assert_equal true, hash["hl.requireFieldMatch"]
assert_equal "<blink>", hash["hl.simple.pre"]
assert_equal "</blink>", hash["hl.simple.post"]
end
end