forked from gphat/data-searchengine-elasticsearch
/
ElasticSearch.pm
319 lines (243 loc) · 7.14 KB
/
ElasticSearch.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package Data::SearchEngine::ElasticSearch;
use Moose;
# ABSTRACT: ElasticSearch support for Data::SearchEngine
use Clone qw(clone);
use ElasticSearch;
use Try::Tiny;
with (
'Data::SearchEngine',
'Data::SearchEngine::Modifiable'
);
use Data::SearchEngine::Item;
use Data::SearchEngine::Paginator;
use Data::SearchEngine::ElasticSearch::Results;
=head1 SYNOPSIS
use Data::SearchEngine::Query;
use Data::SearchEngine::ElasticSearch;
my $dse = Data::SearchEngine::ElasticSearch->new(
url => '127.0.0.1:9200'
);
my $query = Data::SearchEngine::Query->new(
index => 'tweets',
page => 1,
count => 10,
order => { _score => { order => 'asc' } },
type => 'query_string',
facets => {
etype => { terms => { field => 'etype' } },
author_organization_literal => { terms => { field => 'author_organization_literal' } },
author_literal => { terms => { field => 'author_literal' } },
source_literal => { terms => { field => 'source_literal' } },
}
);
my $results = $dse->search($query);
=head1 DESCRIPTION
Data::SearchEngine::ElasticSearch is a backend for Data::SearchEngine. It
aims to generalize the features of L<ElasticSearch> so that application
authors are insulated from I<some> of the differences betwene search modules.
=begin :prelude
=head1 IMPLEMENTATION NOTES
=head2 Incomplete
ElasticSearch's query DSL is large and complex. It is not well suited to
abstraction by a library like this one. As such you will almost likely find
this abstraction lacking. Expect it to improve as the author uses more of
ElasticSearch's features in applications.
=head2 Queries
It is expected that if your L<Data::SearchEngine::Query> object has B<any>
C<query> set then it must also have a C<type>.
The query is then passed on to L<ElasticSearch> thusly:
$es->search(
# ...
query => { $query->type => $query->query }
# ...
);
So if you want to do a query_string query, you would set up your query like
this:
my $query = Data::SearchEngine::Query->new(
# ...
type => 'query_string',
query => { query => 'some query text' }
# ...
);
See the documents for
L<ElasticSearch's DLS|http://www.elasticsearch.org/guide/reference/query-dsl/>
for more details.
=head2 Indexing
ElasticSearch wants an C<index> and C<type> for each Item that is indexed. It
is expected that you will populate these values in the item thusly:
my $item = Data::SearchEngine::Item->new(
id => $something,
values => {
index => 'twitter',
type => 'tweet',
# and whatever else
}
);
$dse->add($item);
=end :prelude
=cut
has '_es' => (
is => 'ro',
isa => 'ElasticSearch',
lazy => 1,
default => sub {
my $self = shift;
return ElasticSearch->new(
servers => $self->servers,
transport => $self->transport,
trace_calls => $self->debug
)
}
);
=cut
=attr servers
The servers to which we'll be connecting.
=cut
has 'servers' => (
is => 'ro',
isa => 'Str|ArrayRef',
default => '127.0.0.1:9200'
);
=attr transport
The transport to use. Refer to L<ElasticSearch> for more information.
=cut
has 'transport' => (
is => 'ro',
isa => 'Str',
default => 'http'
);
=method add ([ $items ])
Add items to the index. Keep in mind that the L<Data::SearchEngine::Item>
should have values set for L<index> and L<type>.
=cut
sub add {
my ($self, $items, $options) = @_;
my @docs;
foreach my $item (@{ $items }) {
my $data = $item->values;
push(@docs, {
index => delete($data->{index}),
type => delete($data->{type}),
id => $item->id,
data => $data
})
}
$self->_es->bulk_index(\@docs);
}
=method present ($item)
Returns true if the L<Data::SearchEngine::Item> is present. Uses the item's
C<id>.
=cut
sub present {
my ($self, $item) = @_;
my $data = $item->values;
try {
my $result = $self->_es->get(
index => delete($data->{index}),
type => delete($data->{type}),
id => $item->id
);
} catch {
# ElasticSearch throws an exception if the document isn't there.
return 0;
}
return 1;
}
sub remove {
die("not implemented");
}
=method remove_by_id ($item)
Remove the specified item from the index. Uses the item's C<id>.
=cut
sub remove_by_id {
my ($self, $item) = @_;
my $data = $item->values;
try {
$self->_es->delete(
index => delete($data->{index}),
type => delete($data->{type}),
id => $item->id
);
} catch {
return 0;
}
return 1;
}
sub update {
die("not implemented");
}
=method search ($query)
Search!
=cut
sub search {
my ($self, $query) = @_;
my $options;
if($query->has_query) {
die "Queries must have a type." unless $query->has_type;
$options->{query} = { $query->type => $query->query };
}
$options->{index} = $query->index;
if($query->has_filters) {
$options->{filter} = {};
foreach my $filter ($query->filter_names) {
$options->{filter} = $query->get_filter($filter);
}
}
if($query->has_facets) {
$options->{facets} = $query->facets;
}
if($query->has_order) {
$options->{sort} = $query->order;
}
$options->{from} = ($query->page - 1) * $query->count;
$options->{size} = $query->count;
my $start = time;
my $resp = $self->_es->search($options);
my $page = $query->page;
my $count = $query->count;
my $hit_count = $resp->{hits}->{total};
my $max_page = $hit_count / $count;
if($max_page != int($max_page)) {
# If trying to calculate how many pages we _could_ have gives us a
# non integer, add one to the page after inting it so we get the right
# integer.
$max_page = int($max_page) + 1;
}
if($page > $max_page) {
$page = $max_page;
}
my $pager = Data::SearchEngine::Paginator->new(
current_page => $page,
entries_per_page => $count,
total_entries => $hit_count
);
my $result = Data::SearchEngine::ElasticSearch::Results->new(
query => $query,
pager => $pager,
elapsed => time - $start
);
if(exists($resp->{facets})) {
foreach my $facet (keys %{ $resp->{facets} }) {
my $href = $resp->{facets}->{$facet};
if(exists($href->{terms})) {
my @vals = ();
foreach my $term (@{ $href->{terms} }) {
push(@vals, { count => $term->{count}, value => $term->{term} });
}
$result->set_facet($facet, \@vals);
}
}
}
foreach my $doc (@{ $resp->{hits}->{hits} }) {
my $values = $doc->{_source};
$values->{_index} = $doc->{_index};
$result->add(Data::SearchEngine::Item->new(
id => $doc->{_id},
values => $values,
));
}
return $result;
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;