local.ini configuration for couchdb
[angular-mojolicious.git] / couchdb-external-kinosearch.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 # http://wiki.apache.org/couchdb/ExternalProcesses
6
7 use KinoSearch::Search::IndexSearcher;
8 use JSON;
9 use Data::Dump qw(dump);
10
11 $|=1;
12
13 my $searcher = KinoSearch::Search::IndexSearcher->new( 
14         index => '/tmp/index' 
15 );
16
17 open(my $log, '>>', '/tmp/couchdb-external-kinosearch.log');
18
19 while(<STDIN>) {
20         warn "# $_\n";
21         my $request = decode_json($_);
22         print $log "<<< $_\n"; 
23
24         my $response = {
25                 code => 200,
26 #               json => {},
27         };
28
29         if ( my $q = $request->{query}->{q} ) {
30
31                 my $hits = $searcher->hits( query => $q );
32
33                 $response->{json}->{total_hits} = $hits->total_hits;
34
35                 while ( my $hit = $hits->next ) {
36                         push @{ $response->{json}->{hits} }, {
37                                 _id => $hit->{_id},
38                                 _rev => $hit->{_rev},
39                                 score => $hit->get_score,
40                         };
41                 }
42
43         } else {
44                 $response->{json}->{error} = "no query found";
45         }
46
47         my $json = encode_json($response);
48         print $json, $/;
49         print $log ">>> $json\n";
50 }
51
52
53 __END__
54 ; insert following into /etc/couchdb/local.ini:
55
56 [log]
57 level = debug
58
59 [external]
60 kinosearch = /home/dpavlin/klin/angular-mojolicious/couchdb-external-kinosearch.pl
61
62 [httpd_db_handlers]
63 _kinosearch = {couch_httpd_external, handle_external_req, <<"kinosearch">>}
64