Mailing List Archive

Search in one field, yet highlights across all fields
I've got a boolean field in my index, I mark the field as unanalyzed,
then store the string '1' (without the quotes) to mean true. I search
for a string + bool_field=1 and I get the hits I want, but the
highlighter doesn't care which field I was looking for a term in. All
the 1s in my body field get highlighted. He is a short code sample,
let me know if you want more, or a more concrete example:

my $query_string = 'this is a query string';

my $query = KinoSearch::Search::BooleanQuery->new;
$query->add_clause( query => $query_string, occur => 'MUST' );

my $bool = KinoSearch::Search::TermQuery->new(
term => KinoSearch::Index::Term->new( bool_field => '1' )
);
$query->add_clause( query => $bool, occur => 'MUST' );

my $hits = $searcher->search( query => $query );

my $highlighter = BigMunch::Highlighter->new( fields => [ 'body' ] );
$hits->create_excerpts( highlighter => $highlighter );

--
Edward Betts
Search in one field, yet highlights across all fields [ In reply to ]
On Apr 24, 2007, at 3:31 PM, Edward Betts wrote:

> I search
> for a string + bool_field=1 and I get the hits I want, but the
> highlighter doesn't care which field I was looking for a term in. All
> the 1s in my body field get highlighted. He is a short code sample,

Thank you for the report and the sample. Code-wise, this was easy to
fix: only highlight terms whose fields match. I'm pretty sure that's
the behavior we'll want generally.

The change has gone in as revision 2361, which should be safe for use.

svn co -r 2361 http://www.rectangular.com/svn/kinosearch/trunk ks

Please note that the Highlighter API changed as of this morning (docs
at <http://xrl.us/vv4v>). The constructor no longer takes any
arguments; you now control highlighting via the new method add_spec
(), which allows you to apply different settings to different fields,
or even generate multiple excerpts from the same field.

my $highlighter = KinoSearch::Highlight::Highlighter->new;
$highlighter->add_spec(
field => 'content', # required
excerpt_length => 150, # default: 200
formatter => $formatter, # default: SimpleHTMLFormatter
encoder => $encoder, # default: SimpleHTMLEncoder
name => 'blurb' # default: value of field param
);
$hits->create_excerpts( highlighter => $highlighter );

Cheers,

Marvin Humphrey
Rectangular Research
http://www.rectangular.com/