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 |
|
13 |
=head1 NAME |
14 |
|
15 |
CouchDB::Estraier - Hyper Estraier full-text search for CouchDB |
16 |
|
17 |
=head1 METHODS |
18 |
|
19 |
=cut |
20 |
|
21 |
our $c = { |
22 |
node_url => 'http://localhost:1978/node/', |
23 |
debug => 0, |
24 |
estuser => 'admin', |
25 |
estpasswd => 'admin', |
26 |
quiet => 0, |
27 |
}; |
28 |
|
29 |
|
30 |
=head2 run_search |
31 |
|
32 |
Process command line options and start helper |
33 |
|
34 |
CouchDB::Estraier->run; |
35 |
|
36 |
=cut |
37 |
|
38 |
GetOptions($c, qw/node_url=s debug+ quiet+ estuser=s estpasswd=s dbuser=s dbpasswd=s/) or die $!; |
39 |
warn "# c: ", dump($c) if ($c->{debug}); |
40 |
|
41 |
open(my $log, '>', '/tmp/couchdb-estraier.log'); |
42 |
|
43 |
sub run_search { |
44 |
while ( 1 ) { |
45 |
my $database = <STDIN>; |
46 |
die unless defined $database; |
47 |
my $query_string = <STDIN>; |
48 |
chomp $database; |
49 |
chomp $query_string; |
50 |
print $log "run_search $database\t$query_string\n"; |
51 |
search( $database, $query_string ); |
52 |
} |
53 |
} |
54 |
|
55 |
sub run_update { |
56 |
while ( 1 ) { |
57 |
my $database = <STDIN>; |
58 |
die unless defined $database; |
59 |
my $json = <STDIN>; |
60 |
chomp $database; |
61 |
chomp $json; |
62 |
print $log "run_update $database\t$json\n"; |
63 |
add( $database, from_json( $json ) ); |
64 |
} |
65 |
} |
66 |
|
67 |
|
68 |
=head2 add |
69 |
|
70 |
CouchDB::Estraier::add( $database, $data ); |
71 |
|
72 |
=cut |
73 |
|
74 |
sub add { |
75 |
my ( $database, $data ) = @_; |
76 |
|
77 |
print $log "add $database ",dump( $data ),"\n"; |
78 |
return; |
79 |
|
80 |
# create and configure node |
81 |
my $node = new Search::Estraier::Node( |
82 |
url => $c->{node_url} . $database, |
83 |
user => $c->{estuser}, |
84 |
passwd => $c->{estpasswd}, |
85 |
croak_on_error => 1, |
86 |
create => 1, |
87 |
debug => $c->{debug} >= 4 ? 1 : 0, |
88 |
); |
89 |
|
90 |
# create document |
91 |
my $doc = new Search::Estraier::Document; |
92 |
|
93 |
if (my $id = $data->{_id}) { |
94 |
$doc->add_attr('@uri', $id); |
95 |
} else { |
96 |
die "can't find pk_col column _id in results\n"; |
97 |
} |
98 |
|
99 |
while (my ($col,$val) = each %{$data}) { |
100 |
|
101 |
if ($val) { |
102 |
# add attributes (make column usable from attribute search) |
103 |
$doc->add_attr($col, $val); |
104 |
|
105 |
# add body text to document (make it searchable using full-text index) |
106 |
$doc->add_text($val); |
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
warn "# doc draft: ",$doc->dump_draft, "\n" if ($c->{debug} >= 2); |
112 |
|
113 |
die "error: ", $node->status,"\n" unless (eval { $node->put_doc($doc) }); |
114 |
|
115 |
} |
116 |
|
117 |
=head2 search |
118 |
|
119 |
Implementation specification: L<http://wiki.apache.org/couchdb/FullTextSearch> |
120 |
|
121 |
CouchDB::Estraier::search( $database, $query ); |
122 |
|
123 |
=cut |
124 |
|
125 |
sub search { |
126 |
my ( $database, $query ) = @_; |
127 |
|
128 |
# create and configure node |
129 |
my $node = new Search::Estraier::Node( |
130 |
url => $c->{node_url} . $database, |
131 |
user => $c->{estuser}, |
132 |
passwd => $c->{estpasswd}, |
133 |
croak_on_error => 1, |
134 |
create => 1, |
135 |
debug => $c->{debug} >= 4 ? 1 : 0, |
136 |
); |
137 |
|
138 |
my $cond = new Search::Estraier::Condition; |
139 |
$cond->set_phrase( $query ); |
140 |
my $nres = $node->search($cond, 0); |
141 |
|
142 |
if ( defined($nres) ) { |
143 |
|
144 |
print "ok\n"; |
145 |
for my $i ( 0 ... $nres->doc_num - 1 ) { |
146 |
my $rdoc = $nres->get_doc($i); |
147 |
print $rdoc->attr('@uri'),"\n",$i,"\n"; |
148 |
} |
149 |
print "\n\n"; |
150 |
|
151 |
} else { |
152 |
print "error\n", $node->status, "\n"; |
153 |
} |
154 |
|
155 |
|
156 |
} |
157 |
|
158 |
|
159 |
1; |