4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
|
7 |
our $VERSION = '0.01'; |
our $VERSION = '0.04_1'; |
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
12 |
|
|
13 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
14 |
|
|
15 |
use Search::Estraier; |
=head2 Simple indexer |
16 |
my $est = new Search::Estraier(); |
|
17 |
|
use Search::Estraier; |
18 |
|
|
19 |
|
# create and configure node |
20 |
|
my $node = new Search::Estraier::Node; |
21 |
|
$node->set_url("http://localhost:1978/node/test"); |
22 |
|
$node->set_auth("admin","admin"); |
23 |
|
|
24 |
|
# create document |
25 |
|
my $doc = new Search::Estraier::Document; |
26 |
|
|
27 |
|
# add attributes |
28 |
|
$doc->add_attr('@uri', "http://estraier.gov/example.txt"); |
29 |
|
$doc->add_attr('@title', "Over the Rainbow"); |
30 |
|
|
31 |
|
# add body text to document |
32 |
|
$doc->add_text("Somewhere over the rainbow. Way up high."); |
33 |
|
$doc->add_text("There's a land that I heard of once in a lullaby."); |
34 |
|
|
35 |
|
die "error: ", $node->status,"\n" unless ($node->put_doc($doc)); |
36 |
|
|
37 |
|
=head2 Simple searcher |
38 |
|
|
39 |
|
use Search::Estraier; |
40 |
|
|
41 |
|
# create and configure node |
42 |
|
my $node = new Search::Estraier::Node; |
43 |
|
$node->set_url("http://localhost:1978/node/test"); |
44 |
|
$node->set_auth("admin","admin"); |
45 |
|
|
46 |
|
# create condition |
47 |
|
my $cond = new Search::Estraier::Condition; |
48 |
|
|
49 |
|
# set search phrase |
50 |
|
$cond->set_phrase("rainbow AND lullaby"); |
51 |
|
|
52 |
|
my $nres = $node->search($cond, 0); |
53 |
|
if (defined($nres)) { |
54 |
|
# for each document in results |
55 |
|
for my $i ( 0 ... $nres->doc_num - 1 ) { |
56 |
|
# get result document |
57 |
|
my $rdoc = $nres->get_doc($i); |
58 |
|
# display attribte |
59 |
|
print "URI: ", $rdoc->attr('@uri'),"\n"; |
60 |
|
print "Title: ", $rdoc->attr('@title'),"\n"; |
61 |
|
print $rdoc->snippet,"\n"; |
62 |
|
} |
63 |
|
} else { |
64 |
|
die "error: ", $node->status,"\n"; |
65 |
|
} |
66 |
|
|
67 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
68 |
|
|
74 |
It is implemented as multiple packages which closly resamble Ruby |
It is implemented as multiple packages which closly resamble Ruby |
75 |
implementation. It also includes methods to manage nodes. |
implementation. It also includes methods to manage nodes. |
76 |
|
|
77 |
|
There are few examples in C<scripts> directory of this distribution. |
78 |
|
|
79 |
=cut |
=cut |
80 |
|
|
81 |
=head1 Inheritable common methods |
=head1 Inheritable common methods |
157 |
} elsif ($line =~ m/^$/) { |
} elsif ($line =~ m/^$/) { |
158 |
$in_text = 1; |
$in_text = 1; |
159 |
next; |
next; |
160 |
} elsif ($line =~ m/^(.+)=(.+)$/) { |
} elsif ($line =~ m/^(.+)=(.*)$/) { |
161 |
$self->{attrs}->{ $1 } = $2; |
$self->{attrs}->{ $1 } = $2; |
162 |
next; |
next; |
163 |
} |
} |
164 |
|
|
165 |
warn "draft ignored: $line\n"; |
warn "draft ignored: '$line'\n"; |
166 |
} |
} |
167 |
} |
} |
168 |
|
|
735 |
return $self->{hints}->{$key}; |
return $self->{hints}->{$key}; |
736 |
} |
} |
737 |
|
|
738 |
|
=head2 hints |
739 |
|
|
740 |
|
More perlish version of C<hint>. This one returns hash. |
741 |
|
|
742 |
|
my %hints = $rec->hints; |
743 |
|
|
744 |
|
=cut |
745 |
|
|
746 |
|
sub hints { |
747 |
|
my $self = shift; |
748 |
|
return $self->{hints}; |
749 |
|
} |
750 |
|
|
751 |
package Search::Estraier::Node; |
package Search::Estraier::Node; |
752 |
|
|
762 |
|
|
763 |
my $node = new Search::HyperEstraier::Node; |
my $node = new Search::HyperEstraier::Node; |
764 |
|
|
765 |
|
or optionally with C<url> as parametar |
766 |
|
|
767 |
|
my $node = new Search::HyperEstraier::Node( 'http://localhost:1978/node/test' ); |
768 |
|
|
769 |
|
or in more verbose form |
770 |
|
|
771 |
|
my $node = new Search::HyperEstraier::Node( |
772 |
|
url => 'http://localhost:1978/node/test', |
773 |
|
debug => 1, |
774 |
|
croak_on_error => 1 |
775 |
|
); |
776 |
|
|
777 |
|
with following arguments: |
778 |
|
|
779 |
|
=over 4 |
780 |
|
|
781 |
|
=item url |
782 |
|
|
783 |
|
URL to node |
784 |
|
|
785 |
|
=item debug |
786 |
|
|
787 |
|
dumps a B<lot> of debugging output |
788 |
|
|
789 |
|
=item croak_on_error |
790 |
|
|
791 |
|
very helpful during development. It will croak on all errors instead of |
792 |
|
silently returning C<-1> (which is convention of Hyper Estraier API in other |
793 |
|
languages). |
794 |
|
|
795 |
|
=back |
796 |
|
|
797 |
=cut |
=cut |
798 |
|
|
799 |
sub new { |
sub new { |
811 |
}; |
}; |
812 |
bless($self, $class); |
bless($self, $class); |
813 |
|
|
814 |
my $args = {@_}; |
if ($#_ == 0) { |
815 |
|
$self->{url} = shift; |
816 |
|
} else { |
817 |
|
my $args = {@_}; |
818 |
|
|
819 |
|
%$self = ( %$self, @_ ); |
820 |
|
|
821 |
$self->{debug} = $args->{debug}; |
warn "## Node debug on\n" if ($self->{debug}); |
822 |
warn "## Node debug on\n" if ($self->{debug}); |
} |
823 |
|
|
824 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
825 |
} |
} |
1417 |
|
|
1418 |
=head2 shuttle_url |
=head2 shuttle_url |
1419 |
|
|
1420 |
This is method which uses C<IO::Socket::INET> to communicate with Hyper Estraier node |
This is method which uses C<LWP::UserAgent> to communicate with Hyper Estraier node |
1421 |
master. |
master. |
1422 |
|
|
1423 |
my $rv = shuttle_url( $url, $content_type, $req_body, \$resbody ); |
my $rv = shuttle_url( $url, $content_type, $req_body, \$resbody ); |
1459 |
|
|
1460 |
$req->headers->header( 'Host' => $url->host . ":" . $url->port ); |
$req->headers->header( 'Host' => $url->host . ":" . $url->port ); |
1461 |
$req->headers->header( 'Connection', 'close' ); |
$req->headers->header( 'Connection', 'close' ); |
1462 |
$req->headers->header( 'Authorization', 'Basic ' . $self->{auth} ); |
$req->headers->header( 'Authorization', 'Basic ' . $self->{auth} ) if ($self->{auth}); |
1463 |
$req->content_type( $content_type ); |
$req->content_type( $content_type ); |
1464 |
|
|
1465 |
warn $req->headers->as_string,"\n" if ($self->{debug}); |
warn $req->headers->as_string,"\n" if ($self->{debug}); |
1473 |
|
|
1474 |
warn "## response status: ",$res->status_line,"\n" if ($self->{debug}); |
warn "## response status: ",$res->status_line,"\n" if ($self->{debug}); |
1475 |
|
|
|
return -1 if (! $res->is_success); |
|
|
|
|
1476 |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
($self->{status}, $self->{status_message}) = split(/\s+/, $res->status_line, 2); |
1477 |
|
|
1478 |
|
if (! $res->is_success) { |
1479 |
|
if ($self->{croak_on_error}) { |
1480 |
|
croak("can't get $url: ",$res->status_line); |
1481 |
|
} else { |
1482 |
|
return -1; |
1483 |
|
} |
1484 |
|
} |
1485 |
|
|
1486 |
$$resbody .= $res->content; |
$$resbody .= $res->content; |
1487 |
|
|
1488 |
warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug}); |
warn "## response body:\n$$resbody\n" if ($resbody && $self->{debug}); |
1582 |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
$reqbody .= '&credit=' . $credit if ($credit > 0); |
1583 |
|
|
1584 |
$self->shuttle_url( $self->{url} . '/_set_link', |
$self->shuttle_url( $self->{url} . '/_set_link', |
1585 |
'text/plain', |
'application/x-www-form-urlencoded', |
1586 |
$reqbody, |
$reqbody, |
1587 |
undef |
undef |
1588 |
) == 200; |
) == 200; |