mirror of https://github.com/apache/lucene.git
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:
parent
38e272fe23
commit
33d170b6bc
|
@ -13,7 +13,7 @@
|
||||||
class Solr::Request::Standard < Solr::Request::Select
|
class Solr::Request::Standard < Solr::Request::Select
|
||||||
|
|
||||||
VALID_PARAMS = [:query, :sort, :default_field, :operator, :start, :rows,
|
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)
|
def initialize(params)
|
||||||
super('standard')
|
super('standard')
|
||||||
|
@ -38,8 +38,6 @@ class Solr::Request::Standard < Solr::Request::Select
|
||||||
@params[:rows] = params[:rows].to_i if params[:rows]
|
@params[:rows] = params[:rows].to_i if params[:rows]
|
||||||
|
|
||||||
@params[:field_list] ||= ["*","score"]
|
@params[:field_list] ||= ["*","score"]
|
||||||
|
|
||||||
#TODO model highlighting parameters: http://wiki.apache.org/solr/HighlightingParameters
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
|
@ -89,6 +87,18 @@ class Solr::Request::Standard < Solr::Request::Select
|
||||||
end
|
end
|
||||||
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)
|
hash.merge(super.to_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,9 @@ class Solr::Response::Standard < Solr::Response::Ruby
|
||||||
facets
|
facets
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def highlighted(id, field)
|
||||||
|
@data['highlighting'][id.to_s][field.to_s]
|
||||||
|
end
|
||||||
|
|
||||||
# supports enumeration of hits
|
# supports enumeration of hits
|
||||||
# TODO revisit - should this iterate through *all* hits by re-requesting more?
|
# TODO revisit - should this iterate through *all* hits by re-requesting more?
|
||||||
|
|
|
@ -150,6 +150,22 @@ class ServerTest < Test::Unit::TestCase
|
||||||
assert_equal 1, info.num_docs
|
assert_equal 1, info.num_docs
|
||||||
end
|
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
|
# wipe the index clean
|
||||||
def clean
|
def clean
|
||||||
@connection.delete_by_query('[* TO *]')
|
@connection.delete_by_query('[* TO *]')
|
||||||
|
|
|
@ -86,4 +86,23 @@ class StandardRequestTest < Test::Unit::TestCase
|
||||||
assert_equal 'query;title desc', request.to_hash[:q]
|
assert_equal 'query;title desc', request.to_hash[:q]
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue