From 33d170b6bc432abab6c978085f977264e7c11461 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Fri, 9 Feb 2007 00:39:50 +0000 Subject: [PATCH] add highlighting support to Standard request git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@505102 13f79535-47bb-0310-9956-ffa450edef68 --- .../ruby/solrb/lib/solr/request/standard.rb | 16 +++++++++++++--- .../ruby/solrb/lib/solr/response/standard.rb | 5 ++++- .../ruby/solrb/test/functional/server_test.rb | 16 ++++++++++++++++ .../solrb/test/unit/standard_request_test.rb | 19 +++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/client/ruby/solrb/lib/solr/request/standard.rb b/client/ruby/solrb/lib/solr/request/standard.rb index 759d5e425d8..e990cf3ef50 100755 --- a/client/ruby/solrb/lib/solr/request/standard.rb +++ b/client/ruby/solrb/lib/solr/request/standard.rb @@ -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 diff --git a/client/ruby/solrb/lib/solr/response/standard.rb b/client/ruby/solrb/lib/solr/response/standard.rb index f64e04fda36..5f1cb7e436e 100644 --- a/client/ruby/solrb/lib/solr/response/standard.rb +++ b/client/ruby/solrb/lib/solr/response/standard.rb @@ -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 diff --git a/client/ruby/solrb/test/functional/server_test.rb b/client/ruby/solrb/test/functional/server_test.rb index f1055375a43..a841b787ee3 100644 --- a/client/ruby/solrb/test/functional/server_test.rb +++ b/client/ruby/solrb/test/functional/server_test.rb @@ -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 diff --git a/client/ruby/solrb/test/unit/standard_request_test.rb b/client/ruby/solrb/test/unit/standard_request_test.rb index 86072a452eb..85dc7821640 100755 --- a/client/ruby/solrb/test/unit/standard_request_test.rb +++ b/client/ruby/solrb/test/unit/standard_request_test.rb @@ -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 => "", + :suffix => "" + } + ) + + hash = request.to_hash + assert_equal true, hash[:hl] + assert_equal "title,author", hash["hl.fl"] + assert_equal true, hash["hl.requireFieldMatch"] + assert_equal "", hash["hl.simple.pre"] + assert_equal "", hash["hl.simple.post"] + end end