/[couchdb]/lib/CouchDB/Estraier.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/CouchDB/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Sat Aug 9 16:10:05 2008 UTC (15 years, 7 months ago) by dpavlin
File size: 3759 byte(s)
first cut at integration with couchdb which partially works
1 package CouchDB::Estraier;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.01';
7
8 use Search::Estraier;
9 use Data::Dump qw/dump/;
10 use Getopt::Long;
11 use JSON;
12 #use IO::Handle;
13 use IO::File;
14
15 =head1 NAME
16
17 CouchDB::Estraier - Hyper Estraier full-text search for CouchDB
18
19 =head1 METHODS
20
21 =cut
22
23 our $c = {
24 node_url => 'http://localhost:1978/node/',
25 estuser => 'admin',
26 estpasswd => 'admin',
27 debug => 0,
28 };
29
30 my $log = $0;
31 $log =~ s{^.*/([^/]+)(\.\w+)$}{/tmp/$1.log} or die "can't generate log name from $0";
32
33 warn "$0 log $log\n";
34
35 GetOptions($c, qw/node_url=s debug+ estuser=s estpasswd=s dbuser=s dbpasswd=s/) or die $!;
36
37 #my $log = IO::Handle->new;
38 #my $log = IO::Handle->new_from_fd(\*STDERR, 'w');
39
40 $log = IO::File->new( "> $log" ) || die "can't open $log: $!";
41
42 $log->autoflush( 1 );
43 $log->print("c: ", dump($c), "\n" ); # if $c->{debug};
44
45 my $in = IO::Handle->new_from_fd(\*STDIN, 'r');
46 my $out = IO::Handle->new_from_fd(\*STDOUT, 'w');
47 $out->autoflush( 1 );
48
49 =head2 run_search
50
51 Process command line options and start helper
52
53 CouchDB::Estraier->run;
54
55 =cut
56
57 sub run_search {
58 while ( 1 ) {
59 $log->print("search ready\n");
60 my $json = $in->getline;
61 die unless defined $json;
62 $log->print( "run_search $json\n" );
63 $out->print(
64 encode_json(
65 search( decode_json( $json ) )
66 ) ,"\n"
67 );
68 }
69 }
70
71 sub run_update {
72 while ( 1 ) {
73 $log->print("update ready\n");
74 my $json = $in->getline;
75 die unless defined $json;
76 $log->print( "run_update $json\n" );
77 update( decode_json( $json ) )
78 }
79 }
80
81
82 =head2 update
83
84 CouchDB::Estraier::update( { db => $database, type => $type } );
85
86 =cut
87
88 sub update {
89 my $args = shift or die "no args";
90 $log->print( "add ",dump( $args ),"\n" );
91
92 my $ret = {
93 code => 200,
94 json => {
95 args => $args,
96 },
97 };
98
99 return $ret;
100
101 my $database = $args->{db} or die "no db in ",dump( $args );
102 my $data = $args->{data} or die "no data in ",dump( $args );
103
104 # create and configure node
105 my $node = new Search::Estraier::Node(
106 url => $c->{node_url} . $database,
107 user => $c->{estuser},
108 passwd => $c->{estpasswd},
109 croak_on_error => 1,
110 create => 1,
111 debug => $c->{debug} >= 4 ? 1 : 0,
112 );
113
114 # create document
115 my $doc = new Search::Estraier::Document;
116
117 if (my $id = $data->{_id}) {
118 $doc->add_attr('@uri', $id);
119 } else {
120 die "can't find pk_col column _id in results\n";
121 }
122
123 while (my ($col,$val) = each %{$data}) {
124
125 if ( defined $val ) {
126 # add attributes (make column usable from attribute search)
127 $doc->add_attr($col, $val);
128
129 # add body text to document (make it searchable using full-text index)
130 $doc->add_text($val);
131 }
132
133 }
134
135 $log->print("doc draft: ",$doc->dump_draft ) if ($c->{debug} >= 2);
136
137 die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) });
138
139 return $ret;
140 }
141
142 =head2 search
143
144 Implementation specification: L<http://wiki.apache.org/couchdb/FullTextSearch>
145
146 CouchDB::Estraier::search( $database, $query );
147
148 =cut
149
150 sub search {
151 my $args = shift or die "no args";
152 $log->print( "search ",dump( $args ),"\n" );
153 my $database = $args->{db} or die "no db in ",dump( $args );
154 my $query = $args->{query} or die "no query in ",dump( $args );
155
156 # create and configure node
157 my $node = new Search::Estraier::Node(
158 url => $c->{node_url} . $database,
159 user => $c->{estuser},
160 passwd => $c->{estpasswd},
161 croak_on_error => 1,
162 # create => 1,
163 debug => $c->{debug} >= 4 ? 1 : 0,
164 );
165
166 my $cond = new Search::Estraier::Condition;
167 $cond->set_phrase( $query );
168 my $nres = $node->search($cond, 0);
169
170 if ( defined($nres) ) {
171
172 $out->print( "ok\n" );
173 for my $i ( 0 ... $nres->doc_num - 1 ) {
174 my $rdoc = $nres->get_doc($i);
175 print $rdoc->attr('@uri'),"\n",$i,"\n";
176 }
177 $out->print( "\n\n" );
178
179 } else {
180 $out->print( "error\n", $node->status, "\n" );
181 }
182
183 }
184
185
186 1;

  ViewVC Help
Powered by ViewVC 1.1.26