1 |
dpavlin |
181 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
# kinosearch.pl |
4 |
|
|
# |
5 |
|
|
# 06/10/07 11:58:27 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
|
|
7 |
|
|
use strict; |
8 |
|
|
use blib './lib'; |
9 |
dpavlin |
190 |
use Grep::Search::KinoSearch; |
10 |
dpavlin |
181 |
use KinoSearch::Highlight::Highlighter; |
11 |
|
|
use Data::Dump qw/dump/; |
12 |
|
|
|
13 |
|
|
my ( $index_path, $query_string ) = @ARGV; |
14 |
|
|
|
15 |
|
|
die "usage: $0 /path/to/index 'search query'\n" unless ( $index_path && $query_string ); |
16 |
|
|
|
17 |
|
|
my $searcher = KinoSearch::Searcher->new( |
18 |
dpavlin |
190 |
invindex => Grep::Search::KinoSearch::Schema->open( $index_path ) |
19 |
dpavlin |
181 |
); |
20 |
|
|
|
21 |
|
|
my @fields = ( qw/ title link content summary category author / ); |
22 |
|
|
my @excerpt_fields = ( qw/ content summary / ); |
23 |
|
|
|
24 |
|
|
my $query_parser = KinoSearch::QueryParser->new( |
25 |
dpavlin |
190 |
schema => Grep::Search::KinoSearch::Schema->new, |
26 |
dpavlin |
181 |
fields => \@fields, |
27 |
|
|
); |
28 |
|
|
$query_parser->set_heed_colons(1); # enable field:value AND/OR/NOT syntax |
29 |
|
|
my $query = $query_parser->parse( $query_string ); |
30 |
|
|
my $hits = $searcher->search( |
31 |
|
|
query => $query, |
32 |
|
|
# offset => $offset, |
33 |
|
|
# num_wanted => $hits_per_page, |
34 |
|
|
); |
35 |
|
|
|
36 |
|
|
my $highlighter = KinoSearch::Highlight::Highlighter->new( |
37 |
|
|
searchable => $searcher, |
38 |
|
|
query => $query, |
39 |
|
|
field => 'content', |
40 |
|
|
); |
41 |
|
|
|
42 |
|
|
print "## found ", $hits->total_hits, " results for $query_string\n"; |
43 |
|
|
|
44 |
|
|
sub snippet { |
45 |
|
|
my $t = shift; |
46 |
|
|
$t =~ s/\s\s+/ /gs; |
47 |
|
|
return substr($t,0,1024); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
while ( my $hit = $hits->fetch_hit ) { |
51 |
|
|
print $hit->get_score,$/; |
52 |
|
|
foreach my $f ( @fields ) { |
53 |
|
|
my $v = $hit->{$f}; |
54 |
|
|
next unless defined $v; |
55 |
|
|
# $v = $highlighter->create_excerpt( $hit ) if grep(/^\Q$f\E^/, @excerpt_fields); |
56 |
|
|
printf("%15s: %s\n", $f, snippet($v)); |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
|