-
+my $v = {
+ search => '',
+ hits => 0,
+ page => 0,
+ max_page => 0,
+ time => '',
+ id => time() . rand(99),
};
-my $node = new Search::Estraier::Node(
- url => 'http://localhost:1978/node/cpan',
- user => 'admin',
- passwd => 'admin',
- croak_on_error => 1,
-);
-
-my $o = $q->param('index') || 0;
-my $search = $q->param('q');
-
-my $on_page = 30;
-my $skip = $o * $on_page;
-
-my $cond = new Search::Estraier::Condition;
-$cond->set_phrase( $search );
-$cond->set_max( $on_page );
-$cond->set_skip( $skip );
-
-my $nres = $node->search($cond, 0);
-
-my $max = 0;
-
-if (defined($nres)) {
- $max = $nres->hits;
- print "Got ", $nres->hits, " results for $search\n";
-
- sub html_snippet {
- my $text = shift || return;
- my $out = '';
- foreach my $s (split(/[\n\r]{2}/, $text)) {
- $out .= ' ... ' if ($out);
- my ($pre,$hit,$post) = split(/\n/,$s,3);
- $hit =~ s/\t.*$//;
- $out .=
- $q->escapeHTML( $pre || '' ) . '' .
- $q->escapeHTML( $hit || '' ) . '' .
- $q->escapeHTML( $post || '');
- }
- return $out;
+my $json;
+
+#warn "config = ", dump($config);
+
+sub json {
+ return
+ '';
+}
+
+sub get_results {
+ my $p = {@_};
+
+ my ($search,$page) = ( $p->{search} || '', $p->{page} || 0);
+
+ warn "get_results( $search , $page )\n";
+
+ sub next_page {
+ return '
' .
+ join("\n", @_) . json() . '
';
+ }
+
+ if (! $search || $search =~ m/^\s*$/) {
+ $v->{status} = 'Enter search query';
+ return next_page();
}
+ if (! $page) {
+ $v->{status} = 'Error: no page number?';
+ return next_page();
+ }
+
+ $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
+ $v->{search} = $search;
+
+ $v->{page} = $page;
- # for each document in results
- for my $i ( 0 ... $nres->doc_num - 1 ) {
+ my $node = new Search::Estraier::Node(%{ $config->{estraier} });
- my $rdoc = $nres->get_doc($i);
+ my $on_page = 30;
+ my $skip = ( $page - 1 ) * $on_page;
+
+ my $cond = new Search::Estraier::Condition;
+ $cond->set_phrase( $search );
+ $cond->set_max( $on_page );
+ $cond->set_skip( $skip );
+
+ my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
+
+ my $out;
+
+ if (defined($nres)) {
+ $v->{hits} = $nres->hits;
+ $v->{time} = $nres->hint('TIME');
+ $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
+
+ $v->{status} = qq{
+ Got $v->{hits} results for $v->{search}
+ in $v->{time} s
+ };
+
+ sub html_snippet {
+ my $text = shift || return;
+ my $out = '';
+ foreach my $s (split(/[\n\r]{2}/, $text)) {
+ $out .= ' ... ' if ($out);
+ my ($pre,$hit,$post) = split(/\n/,$s,3);
+ $hit =~ s/\t.*$//;
+ $out .=
+ $q->escapeHTML( $pre || '' ) . '' .
+ $q->escapeHTML( $hit || '' ) . '' .
+ $q->escapeHTML( $post || '');
+ }
+ return $out;
+ }
+
+ sub attr_regex {
+ my ($rdoc,$attr) = @_;
+ my $text = $rdoc->attr( $attr );
+ return unless defined($text);
+
+ if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
+ my $do = '$text =~ ' . $r . ';';
+ eval $do;
+ if ($@) {
+ warn "eval $do failed: $@\n";
+ }
+ }
+ return $text;
+ }
+
+ # for each document in results
+ for my $i ( 0 ... $nres->doc_num - 1 ) {
- print "- ";
+ my $rdoc = $nres->get_doc($i);
- print "
", $rdoc->attr('@title'),"
\n"; - print "", $rdoc->attr('source'),"
\n"; - print "", html_snippet( $rdoc->snippet ),"\n"; - print "[", $skip + $i, "] "; - print "", $rdoc->attr('@uri'),""; - print "
' .
+ '
', + 'If you are using the scroll bar, release the mouse to see more results.' + ); + } -print qq{ - -
+ return $out;
-' . $rdoc->attr('@title') . '
' . + '' . html_snippet( $rdoc->snippet ) . '
' . + '' . attr_regex( $rdoc, 'source' ) . '
'; + my $uri = attr_regex( $rdoc, '@uri' ); + $out .= + qq{$uri } . + attr_regex( $rdoc, '@mdate' ) . + ' [' . ( $skip + $i + 1 ) . ']'; + } + + } else { + $out .= 'error: ' . $node->status; } -} else { - die "error: ", $node->status,"\n"; -} + if ($v->{page} == $v->{max_page}) { + $out .= next_page('All results shown'); + } else { + $out .= next_page( + 'Loading results...', + 'If you are using the scroll bar, release the mouse to see more results.' + ); + } -print qq{ - -