| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Catalyst::Model::Lucy; |
|
2
|
|
|
|
|
|
|
{ |
|
3
|
|
|
|
|
|
|
$Catalyst::Model::Lucy::VERSION = '0.001'; |
|
4
|
|
|
|
|
|
|
} |
|
5
|
2
|
|
|
2
|
|
4972
|
use Moo; |
|
|
2
|
|
|
|
|
47077
|
|
|
|
2
|
|
|
|
|
22
|
|
|
6
|
2
|
|
|
2
|
|
4654
|
use FindBin; |
|
|
2
|
|
|
|
|
1090
|
|
|
|
2
|
|
|
|
|
96
|
|
|
7
|
2
|
|
|
2
|
|
14
|
use File::Spec; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
48
|
|
|
8
|
2
|
|
|
2
|
|
1390
|
use Lucy::Search::IndexSearcher; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Lucy::Plan::Schema; |
|
10
|
|
|
|
|
|
|
use Lucy::Analysis::PolyAnalyzer; |
|
11
|
|
|
|
|
|
|
use Lucy::Plan::FullTextType; |
|
12
|
|
|
|
|
|
|
use namespace::clean; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
extends 'Catalyst::Model'; |
|
15
|
|
|
|
|
|
|
# ABSTRACT: A model for Lucy |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has create_index => ( |
|
18
|
|
|
|
|
|
|
is => 'rw', |
|
19
|
|
|
|
|
|
|
default => sub { 0 },); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has index_path => ( |
|
22
|
|
|
|
|
|
|
is => 'ro', |
|
23
|
|
|
|
|
|
|
default => sub { return File::Spec->catfile("$FindBin::Bin","index") }, ); |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has index_searcher => ( |
|
26
|
|
|
|
|
|
|
builder => '_index_searcher_builder', |
|
27
|
|
|
|
|
|
|
is => 'ro', |
|
28
|
|
|
|
|
|
|
handles => { hits => 'hits' }, |
|
29
|
|
|
|
|
|
|
lazy => 1,); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has indexer => ( |
|
32
|
|
|
|
|
|
|
builder => '_indexer_builder', |
|
33
|
|
|
|
|
|
|
is => 'ro', |
|
34
|
|
|
|
|
|
|
lazy => 1,); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has language => ( |
|
37
|
|
|
|
|
|
|
is => 'rw', |
|
38
|
|
|
|
|
|
|
required => 1, |
|
39
|
|
|
|
|
|
|
default => sub { return 'en' },); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has num_wanted => ( |
|
42
|
|
|
|
|
|
|
is => 'rw', |
|
43
|
|
|
|
|
|
|
default => sub { return 10 },); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has schema => ( |
|
46
|
|
|
|
|
|
|
builder => '_schema_builder', |
|
47
|
|
|
|
|
|
|
is => 'ro', |
|
48
|
|
|
|
|
|
|
lazy => 1,); |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has schema_params => ( |
|
51
|
|
|
|
|
|
|
is => 'ro',); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has truncate_index => ( |
|
54
|
|
|
|
|
|
|
is => 'rw', |
|
55
|
|
|
|
|
|
|
default => sub { 0 },); |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _indexer_builder { |
|
58
|
|
|
|
|
|
|
my $self = shift; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $indexer = Lucy::Index::Indexer->new( |
|
61
|
|
|
|
|
|
|
index => $self->index_path, |
|
62
|
|
|
|
|
|
|
schema => $self->schema, |
|
63
|
|
|
|
|
|
|
create => $self->create_index, |
|
64
|
|
|
|
|
|
|
truncate => $self->truncate_index, |
|
65
|
|
|
|
|
|
|
); |
|
66
|
|
|
|
|
|
|
return $indexer; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _index_searcher_builder { |
|
70
|
|
|
|
|
|
|
my $self = shift; |
|
71
|
|
|
|
|
|
|
return Lucy::Search::IndexSearcher->new( |
|
72
|
|
|
|
|
|
|
index => $self->index_path, |
|
73
|
|
|
|
|
|
|
); |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _schema_builder { |
|
77
|
|
|
|
|
|
|
my $self = shift; |
|
78
|
|
|
|
|
|
|
my $schema = Lucy::Plan::Schema->new; |
|
79
|
|
|
|
|
|
|
if ( $self->schema_params ) { |
|
80
|
|
|
|
|
|
|
my $polyanalyzer = Lucy::Analysis::PolyAnalyzer->new( |
|
81
|
|
|
|
|
|
|
language => $self->language |
|
82
|
|
|
|
|
|
|
); |
|
83
|
|
|
|
|
|
|
my $default_type = Lucy::Plan::FullTextType->new( |
|
84
|
|
|
|
|
|
|
analyzer => $polyanalyzer, |
|
85
|
|
|
|
|
|
|
); |
|
86
|
|
|
|
|
|
|
for my $param ( @{$self->schema_params} ) { |
|
87
|
|
|
|
|
|
|
$schema->spec_field( |
|
88
|
|
|
|
|
|
|
name => $param->{name}, |
|
89
|
|
|
|
|
|
|
type => $param->{type} || $default_type); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
return $schema; |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 NAME |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Catalyst::Model::Lucy |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# 1. Setup the Model |
|
108
|
|
|
|
|
|
|
package MyCatApplication::Model::Lucy; |
|
109
|
|
|
|
|
|
|
use base qw(Catalyst::Model::Lucy); |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my $other_type = Lucy::Plan::FullTextType->new( |
|
112
|
|
|
|
|
|
|
analyzer => Lucy::Analysis::PolyAnalyzer->new( language => 'en' ) |
|
113
|
|
|
|
|
|
|
); |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__PACKAGE__->config( |
|
116
|
|
|
|
|
|
|
index_path => File::Spec->catfile($FindBin::Bin,'index/path/'), |
|
117
|
|
|
|
|
|
|
num_wanted => 20, |
|
118
|
|
|
|
|
|
|
language => 'en', |
|
119
|
|
|
|
|
|
|
create_index => 1, # Optional |
|
120
|
|
|
|
|
|
|
truncate_index => 1, # Optional |
|
121
|
|
|
|
|
|
|
schema_params => [ # Optional schema params |
|
122
|
|
|
|
|
|
|
{ name => 'title' }, # defaults to Lucy::Plan::FullTextType |
|
123
|
|
|
|
|
|
|
{ name => 'desc', type => $other_type } |
|
124
|
|
|
|
|
|
|
] |
|
125
|
|
|
|
|
|
|
); |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# 2. Use in a controller |
|
129
|
|
|
|
|
|
|
my $results = $c->model('Lucy')->hits( query => 'foo' ); |
|
130
|
|
|
|
|
|
|
while ( my $hit = $results->next ) { |
|
131
|
|
|
|
|
|
|
print $hit->{title},"\n"; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This is a catalyst model for Apache L<Lucy>. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 create_index( 1|0 ) |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Sets the create_index flag to either 1 or 0 when initializing |
|
145
|
|
|
|
|
|
|
L<Lucy::Index::Indexer>. Default value is 0. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 index_path( $path ) |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Specifies the path to the index. The default path is $FindBin::Bin/index. |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 index_searcher |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
This is L<Lucy::Search::IndexSearcher> |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 indexer |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This is L<Lucy::Index::Indexer> |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 language( $lang ) |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This is the index language, the default value is en. |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 num_wanted($num) |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
This is the number of hits the index_searcher will return. This is for |
|
166
|
|
|
|
|
|
|
pagination. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 schema |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Accessor to L<Lucy::Plan::Schema> |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 schema_params( $array_ref ) |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Used when the indexer is initialized. The values of this are used to define |
|
175
|
|
|
|
|
|
|
any custom scheme for index creation. See <Lucy::Plan::Schema> |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 truncate_index( 1|0 ) |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Sets the truncate flag to either 1 or 0 when initializing |
|
180
|
|
|
|
|
|
|
L<Lucy::Index::Indexer>. Default value is 0. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 METHODS |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 AUTHOR |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Logan Bell L<email:logie@cpan.org> |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
L<Lucy>, L<Catalyst::Model> |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Copyright 2012, Logan Bell L<email:logie@cpan.org> |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under |
|
197
|
|
|
|
|
|
|
the same terms as Perl itself. |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
__END__ |