#! /usr/bin/perl
###############################################################################
#
# $Id: showfile,v 1.1.1.1 2003/04/21 15:30:49 bcwhite Exp $
#
# Example on how to highlight words found by a query.
#
# This will run an arbitrary query and display the highest ranked match with
# all words & phrases highlighted for easier spotting.
#
# Written by Brian C. White <bcwhite@pobox.com>.
# This example code has been placed in the public domain.
#
###############################################################################



# Our friend...
use Ferret;


# Get script arguments
die "Use: $0 'query' [prefix]\n" if @ARGV < 1 || @ARGV > 2;
$query = $ARGV[0];
$prefix= $ARGV[1];


# Create search engine and open the index file
$search = new Ferret;
$search->Open("ferret.index");


# Run the query and get the highest ranked result
my @docs = $search->Query($query);
my $doc  = $docs[0];


# Die if no matches were found
die "Sorry, no documents matched that query\n" unless $doc;


# Remove the score from the beginning and load the document
$doc =~ s/^\d+ //;
my $file  = Ferret::LoadFile($doc);


# Change the query into an array of words and highlight them
# When highlighting HTML documents, use MarkHTMLWords so it
# doesn't highlight a word inside a tag.
my @words = $search->QueryWords($query);
Ferret::MarkWords(\$file,"<b>","</b>",@words);


# Display the final result
print $file;


# Close down the search engine
$search->Close();
