X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=blobdiff_plain;f=couchdb-external-kinosearch.pl;h=b455bfd96619d2d7cfb6b399173f061db6af4f16;hp=7b95d8395ad74303a21500a426efca6074a21fb2;hb=9e68c62631a45ffaa21a023ced9103df07ffae84;hpb=50eae40f791d88599453db0f02453bc48a60ba98 diff --git a/couchdb-external-kinosearch.pl b/couchdb-external-kinosearch.pl index 7b95d83..b455bfd 100755 --- a/couchdb-external-kinosearch.pl +++ b/couchdb-external-kinosearch.pl @@ -3,22 +3,22 @@ use warnings; use strict; # http://wiki.apache.org/couchdb/ExternalProcesses +# +# curl 'http://localhost:5984/drzb2011/_kinosearch?q=a&include_docs=true' use KinoSearch::Search::IndexSearcher; -use JSON; +use Mojo::JSON; use Data::Dump qw(dump); $|=1; -my $searcher = KinoSearch::Search::IndexSearcher->new( - index => '/tmp/index' -); +our $json = Mojo::JSON->new; open(my $log, '>>', '/tmp/couchdb-external-kinosearch.log'); while() { warn "# $_\n"; - my $request = decode_json($_); + my $request = $json->decode($_); print $log "<<< $_\n"; my $response = { @@ -28,25 +28,31 @@ while() { if ( my $q = $request->{query}->{q} ) { + my $searcher = KinoSearch::Search::IndexSearcher->new( + index => '/tmp/kinosearch.' . $request->{info}->{db_name}, + ); + my $hits = $searcher->hits( query => $q ); $response->{json}->{total_hits} = $hits->total_hits; while ( my $hit = $hits->next ) { - push @{ $response->{json}->{hits} }, { + my $r = { _id => $hit->{_id}, _rev => $hit->{_rev}, score => $hit->get_score, }; + $r->{doc} = $json->decode( $hit->{doc} ) if exists $request->{query}->{include_docs}; + push @{ $response->{json}->{hits} }, $r; } } else { $response->{json}->{error} = "no query found"; } - my $json = encode_json($response); - print $json, $/; - print $log ">>> $json\n"; + my $send = $json->encode($response); + print $send, $/; + print $log ">>> $send\n"; }