##
#!/usr/bin/perl
use strict;
use warnings;
my $path_to_index = '/path/to/indexdir';
use CGI;
use List::Util qw( max min );
use POSIX qw( ceil );
use Encode qw( decode );
use Lucy::Search::IndexSearcher;
use Lucy::Highlight::Highlighter;
use Lucy::Search::QueryParser;
use Lucy::Search::TermQuery;
use Lucy::Search::ANDQuery;
my $cgi = CGI->new;
my $q = decode( "UTF-8", $cgi->param('q') || '' );
my $offset = decode( "UTF-8", $cgi->param('offset') || 0 );
my $category = decode( "UTF-8", $cgi->param('category') || '' );
my $page_size = 10;
# Create an IndexSearcher and a QueryParser.
my $searcher = Lucy::Search::IndexSearcher->new(
index => $path_to_index,
);
my $qparser = Lucy::Search::QueryParser->new(
schema => $searcher->get_schema,
);
# Build up a Query.
my $query = $qparser->parse($q);
if ($category) {
my $category_query = Lucy::Search::TermQuery->new(
field => 'category',
term => $category,
);
$query = Lucy::Search::ANDQuery->new(
children => [ $query, $category_query ]
);
}
# Execute the Query and get a Hits object.
my $hits = $searcher->hits(
query => $query,
offset => $offset,
num_wanted => $page_size,
);
my $hit_count = $hits->total_hits;
# Arrange for highlighted excerpts to be created.
my $highlighter = Lucy::Highlight::Highlighter->new(
searcher => $searcher,
query => $q,
field => 'content'
);
# Create result list.
my $report = '';
while ( my $hit = $hits->next ) {
my $score = sprintf( "%0.3f", $hit->get_score );
my $excerpt = $highlighter->create_excerpt($hit);my @exi=split (/\n/,$excerpt);
$report .= qq|
$hit->{title}
$score ;
foreach my $v(@exi){
if ($v=~ /$q/){
$report .= qq|
$v
|;
}
}
$report .= qq|
$hit->{url}
|;
}
blast_out_content( $q, $report );
sub blast_out_content {
my ( $query_string, $hit_list, $paging_info, $category_select ) = @_;
my $escaped_q = CGI::escapeHTML($query_string);
binmode( STDOUT, ":encoding(UTF-8)" );
print qq|Content-type: text/html; charset=UTF-8\n\n|;
print qq|
Lucy: $escaped_q
$hit_list
$paging_info
|;
}
####
ID Name Dept Addr
1 Chella IT cbe
2 Sara CSE cbe
3 aaaaa EEE CBE
4 chella CSE thi
5 sara ECE che
6 dddd MECH che