/[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

Annotation of /lib/CouchDB/Estraier.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (hide annotations)
Tue Aug 5 13:34:06 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 2973 byte(s)
cleanup API, added CouchDB::Estraier version [0.01]
1 dpavlin 1 package CouchDB::Estraier;
2    
3     use strict;
4     use warnings;
5    
6 dpavlin 3 our $VERSION = '0.01';
7    
8 dpavlin 1 use Search::Estraier;
9     use Data::Dump qw/dump/;
10 dpavlin 3 use Getopt::Long;
11     use JSON;
12 dpavlin 1
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 dpavlin 3 =head2 run_search
31 dpavlin 1
32     Process command line options and start helper
33    
34     CouchDB::Estraier->run;
35    
36     =cut
37    
38 dpavlin 3 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 dpavlin 1 }
53     }
54    
55 dpavlin 3 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 dpavlin 1
67 dpavlin 3
68 dpavlin 1 =head2 add
69    
70 dpavlin 3 CouchDB::Estraier::add( $database, $data );
71 dpavlin 1
72     =cut
73    
74     sub add {
75 dpavlin 3 my ( $database, $data ) = @_;
76 dpavlin 1
77 dpavlin 3 print $log "add $database ",dump( $data ),"\n";
78     return;
79    
80 dpavlin 1 # 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 dpavlin 3 CouchDB::Estraier::search( $database, $query );
122 dpavlin 1
123     =cut
124    
125     sub search {
126 dpavlin 3 my ( $database, $query ) = @_;
127 dpavlin 1
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;

  ViewVC Help
Powered by ViewVC 1.1.26