ElasticSync/ResultSet.pm

filter: Statementstmt 14.5% 25 / 172 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 18.1% 6 / 33 Podpod 77.2% 17 / 22 totaltotal 16.1% 48 / 297 CC 35 SCAR 30.4
CC
35
Cov
11%
all
72.0
hits
56.0
range
20.0
CRAP 20.9
« ElasticSync/Role/ElasticResult.pm ↑ Summary ElasticSync/Role/ElasticSchema.pm »
1 package DBIx::Class::ElasticSync::ResultSet;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use DBIx::Class::ResultClass::HashRefInflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Search::Elasticsearch::Compat::QueryParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has body => (
13 is => 'rw',
14 isa => 'HashRef',
15 required => 0,
16 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 1 );
18  
19 has queries => (
20 is => 'rw',
21 isa => 'ArrayRef',
22 required => 0,
23 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 1 );
25  
26 has filters => (
27 is => 'rw',
28 isa => 'ArrayRef',
29 required => 0,
30 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 1 );
32  
33 has response => (
34 is => 'rw',
35 isa => 'HashRef',
36 required => 0,
37 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 1 );
39  
40 has aggs => (
41 is => 'rw',
42 isa => 'ArrayRef',
43 required => 0,
44 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 1 );
46  
47 =head2 size
48  
49 Set maximum hit size for the Query
50  
51 $elastic_rs->size(10)
52  
53 =cut
54  
55 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
56  
57 0 my ( $self, $size ) = @_;
58 0 $self->body->{size} = $size;
59 0 return $self;
60  
61 }
62  
63 =head2 from
64  
65 Offset for Result
66  
67 =cut
68  
69 0 ▶sub from {
Subroutine from: not called CC=1 SCAR=6.9
Pod documented
70  
71 0 my ( $self, $from ) = @_;
72 0 $self->body->{from} = $from;
73 0 return $self;
74 }
75  
76 =head2 order_by
77  
78 Order the Hits in the Elastic response
79  
80 $elastic_rs->order_by( { "fieldname" => "desc" } )
81  
82 =cut
83  
84 0 ▶sub order_by {
Subroutine order_by: not called CC=2 SCAR=17.9
Pod documented
85  
86 0 my ( $self, $order ) = @_;
87  
88 0 ▶ $self->body->{sort} = $order || [ { "_score" => 'desc' } ];
Condition0%0/2
$order || [{'_score', 'desc'}]
l!l
00
MC/DC0%0/1
$order || [{'_score', 'desc'}]
$order
Truth table0%0/2
A
A= $order
Aresult
11
00
89 0 return $self;
90 }
91  
92 =head2 query_string
93  
94 Query String for Elastic Query String Query
95  
96 $elastic_rs->query_string('foo*')
97  
98 =cut
99  
100 0 ▶sub query_string {
Subroutine query_string: not called CC=1 SCAR=6.9
Pod documented
101  
102 0 my ( $self, $searchstring ) = @_;
103  
104 0 my $qp = Search::Elasticsearch::Compat::QueryParser->new();
105 0 my $filtered_query_string = $qp->filter($searchstring);
106 0 $self->body->{query}{query_string}{query} = $filtered_query_string;
107  
108 0 return $self;
109 }
110  
111 =head2 query
112  
113 add query to Elastic Request
114  
115 $elastic_rs->query( { match_all => {} } )
116  
117 =cut
118  
119 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
120  
121 0 my ( $self, $query ) = @_;
122 0 push @{ $self->queries }, $query;
123 0 return $self;
124 }
125  
126 =head2 agg
127  
128 add aggregation to Elastic Request
129  
130 =cut
131  
132 0 ▶sub agg {
Subroutine agg: not called CC=3 SCAR=24.8
Pod documented
133  
134 0 my ( $self, $params ) = @_;
135  
136 0 ▶ return unless $params && ref $params eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $params and ref $params eq "HASH" 0 0 0
Condition0%0/3
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params eq "HASH"
$params ref $params eq "HASH"
Truth table0%0/3
A and B
A= $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
137  
138 0 push @{ $self->aggs }, $params;
139  
140 0 return $self;
141 }
142  
143 =head2 aggregation
144  
145 proxy function for "agg"
146  
147 =cut
148  
149 0 ▶sub aggregation {
Subroutine aggregation: not called CC=1 SCAR=6.9
Pod documented
150  
151 0 my ( $self, $params ) = @_;
152  
153 0 return $self->agg($params);
154 }
155  
156 =head2 filter
157  
158 Adds filter to Elastic Request. Deprecated since Elasticsearch 2.x
159  
160 =cut
161  
162 0 ▶sub filter {
Subroutine filter: not called CC=1 SCAR=6.9
Pod documented
163  
164 0 my ( $self, $filter ) = @_;
165 0 push @{ $self->filters }, $filter;
166 0 return $self;
167  
168 }
169  
170 0 ▶sub filter_rs {
Subroutine filter_rs: not called CC=3 SCAR=24.8
Pod undocumented
171  
172 0 my ( $self, $params ) = @_;
173  
174 0 ▶ return unless ref $params eq 'HASH' && %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" and %$params 0 0 0
Condition0%0/3
ref $params eq "HASH" and %$params
!ll&&!rl&&r
000
MC/DC0%0/2
ref $params eq "HASH" and %$params
ref $params eq "HASH" %$params
Truth table0%0/3
A and B
A= ref $params eq "HASH"B= %$params
ABresult
0X0
100
111
175  
176 0 return $self->filter( { term => $params } );
177 }
178  
179 0 ▶sub exists {
Subroutine exists: not called CC=2 SCAR=17.9
Pod undocumented
180  
181 0 my ( $self, $column ) = @_;
182  
183 0 ▶ return unless $column;
Branch: 1 branch
BranchTrueFalseTotal
unless $column 0 0 0
184  
185 0 return $self->filter( { exists => { field => $column } } );
186 }
187  
188 0 ▶sub range {
Subroutine range: not called CC=4 SCAR=30.0
Pod undocumented
189  
190 0 my ( $self, $column, $params ) = @_;
191  
192 0 ▶ return unless $column;
Branch: 1 branch
BranchTrueFalseTotal
unless $column 0 0 0
193 0 ▶ return unless ref $params eq 'HASH' && %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" and %$params 0 0 0
Condition0%0/3
ref $params eq "HASH" and %$params
!ll&&!rl&&r
000
MC/DC0%0/2
ref $params eq "HASH" and %$params
ref $params eq "HASH" %$params
Truth table0%0/3
A and B
A= ref $params eq "HASH"B= %$params
ABresult
0X0
100
111
194  
195 0 return $self->filter( { range => { $column => $params } } );
196 }
197  
198 =head2 highlighter
199  
200 Add the highlighter on $field, must be mapped with term_vector => "with_positions_offsets"
201  
202 "text" => { type => "string", index => "analyzed", "store" => "yes", "term_vector" => "with_positions_offsets" },
203  
204  
205 =cut
206  
207 0 ▶sub highlighter {
Subroutine highlighter: not called CC=2 SCAR=17.9
Pod documented
208  
209 0 my ( $self, $field ) = @_;
210  
211 0 ▶ die "Missing field for highlighter" unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 0 0
212  
213 $self->body->{highlight} = {
214 0 "number_of_fragments" => 3,
215 "fragment_size" => 150,
216 "tags_schema" => "styled",
217 "pre_tags" => [ '<span class="marking1">', '<span class="marking2">', '<span class="marking3">' ],
218 "post_tags" => [ "</span>", "</span>", "</span>" ],
219 "fields" => { $field => { "number_of_fragments" => 5 }, }
220 };
221  
222 0 return $self;
223 }
224  
225 =head2 all
226  
227 Collects all items, based on query / aggreagtions setted
228  
229 =cut
230  
231 0 ▶sub all {
Subroutine all: not called CC=8 SCAR=42.8
Pod documented
232  
233 0 my ( $self, $search_params ) = @_;
234 0 $self->body->{track_scores} = 1;
235  
236 0 ▶ $search_params = {} unless ref $search_params eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $search_params eq "HASH" 0 0 0
237  
238 0 my $queries = $self->queries;
239 0 my $filters = $self->filters;
240 0 my $aggs = $self->aggs;
241  
242 0 ▶ if ( @$queries == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$queries == 1) { } 0 0 0
elsif (@$queries > 1) { } 0 0 0
243  
244 0 $self->body->{query} = $queries->[0];
245  
246 } elsif ( @$queries > 1 ) {
247  
248 0 $self->body->{query}{and} = $queries;
249  
250 }
251  
252 0 ▶ if ( @$aggs == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$aggs == 1) { } 0 0 0
elsif (@$aggs > 1) { } 0 0 0
253  
254 0 $self->body->{aggs} = $aggs->[0];
255 } elsif ( @$aggs > 1 ) {
256  
257 0 $self->body->{aggs} = $aggs;
258 }
259  
260 0 ▶ if ( @$filters == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$filters == 1) { } 0 0 0
elsif (@$filters > 1) { } 0 0 0
261  
262 0 $self->body->{filter} = $filters->[0];
263  
264 } elsif ( @$filters > 1 ) {
265  
266 0 $self->body->{filter}{and} = $filters;
267  
268 }
269  
270 0 my $response = $self->schema->es->search(
271 index => $self->index_name,
272 type => $self->type,
273 body => $self->body,
274 %$search_params,
275 );
276  
277 0 $self->response($response);
278  
279 0 return $self;
280  
281 }
282  
283 =head2 hits
284  
285 returns the collected hits from "all" as ArrayRef
286  
287 =cut
288  
289 0 ▶sub hits {
Subroutine hits: not called CC=7 SCAR=40.3
Pod documented
290  
291 0 my ($self) = @_;
292  
293 0 my $response = $self->response;
294  
295 0 my $result = [];
296  
297 0 foreach my $match ( @{ $response->{hits}{hits} } ) {
298  
299 0 my $doc = $match->{_source};
300 0 my $inner_hits = $match->{inner_hits};
301  
302 0 ▶ if ($inner_hits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner_hits) 0 0 0
303  
304 0 foreach my $key ( keys %$inner_hits ) {
305  
306 0 my $inner_results = [];
307  
308 0 foreach my $inner_result ( @{ $match->{inner_hits}{$key}{hits}{hits} } ) {
309  
310 0 push @$inner_results, $inner_result->{_source};
311 }
312  
313 0 $doc->{inner_hits}->{$key} = $inner_results;
314 }
315 }
316  
317 0 ▶ if ( $self->body->{highlight} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->body->{"highlight"}) 0 0 0
318  
319 # TODO fix fix text-assign
320 0 ▶ $doc->{highlight} = join( " … ", @{ $match->{highlight}{text} || [] } );
Condition0%0/2
$match->{'highlight'}{'text'} || []
l!l
00
MC/DC0%0/1
$match->{'highlight'}{'text'} || []
$match->{'highlight'}{'text'}
Truth table0%0/2
A
A= $match->{'highlight'}{'text'}
Aresult
11
00
321  
322 }
323  
324 0 push @$result, $doc;
325 }
326  
327 0 return $result;
328 }
329  
330 =head2 buckets
331  
332 returns Aggregation buckets, by Aggregation name
333  
334 $elastic_rs->buckets('agg_name')
335  
336 =cut
337  
338 0 ▶sub buckets {
Subroutine buckets: not called CC=3 SCAR=24.8
Pod documented
339  
340 0 my ( $self, $agg_name ) = @_;
341  
342 0 my $response = $self->response;
343  
344 0 ▶ return unless $response->{aggregations}{$agg_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"aggregations"}{$agg_name} 0 0 0
345  
346 0 my $buckets = [];
347  
348 0 for my $match ( @{ $response->{aggregations}{$agg_name}{buckets} } ) {
349  
350 0 push @$buckets, $match;
351 }
352  
353 0 return $buckets;
354 }
355  
356 =head2 es_index
357  
358 adds documents from a DBIC Resultset to the Elastic index. It calls the Elastic
359 Resultset es_transform method for denormalizing Data
360  
361 $elastic_rs->es_index( $dbic_rs->search_rs( { foo => bar } ) )
362  
363 =cut
364  
365 #TODO add example
366  
367 0 ▶sub es_index {
Subroutine es_index: not called CC=2 SCAR=17.9
Pod documented
368  
369 0 my $self = shift;
370 0 my $dbic_rs = shift;
371  
372 0 my $results = $dbic_rs;
373 0 $results->result_class('DBIx::Class::ResultClass::HashRefInflator');
374  
375 0 ▶ while ( my $row = $results->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $results->next) 0 0 0
376  
377 0 $row = $self->es_transform( $row, $dbic_rs );
378  
379 0 $row->{es_id} = $self->es_id( $row, $dbic_rs );
380  
381 $self->es->index(
382 {
383 index => $self->index_name($row),
384 id => $row->{es_id},
385 0 type => $self->type,
386 body => $row,
387 }
388 );
389 }
390  
391 }
392  
393 0 ▶sub es_create {
Subroutine es_create: not called CC=3 SCAR=24.8
Pod undocumented
394  
395 0 my $self = shift;
396 0 my $dbic_rs = shift;
397  
398 0 my $results = $dbic_rs;
399 0 $results->result_class('DBIx::Class::ResultClass::HashRefInflator');
400  
401 0 ▶ while ( my $row = $results->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $results->next) 0 0 0
402  
403 0 $row = $self->es_transform( $row, $dbic_rs );
404  
405 0 $row->{es_id} = $self->es_id( $row, $dbic_rs );
406  
407 0 ▶ my $id = $row->{es_id} ? { id => $row->{es_id} } : {};
Branch: 1 branch
BranchTrueFalseTotal
$row->{'es_id'} ? : 0 0 0
408  
409 0 $self->es->create(
410 {
411 index => $self->index_name($row),
412 type => $self->type,
413 body => $row,
414 %$id,
415 }
416 );
417 }
418 }
419  
420 =head2 es_delete
421  
422 Deletes Object from Elasticstorage based on a DBIC Resultset
423  
424 =cut
425  
426 0 ▶sub es_delete {
Subroutine es_delete: not called CC=2 SCAR=17.9
Pod documented
427  
428 0 my $self = shift;
429 0 my $dbic_rs = shift;
430  
431 0 $dbic_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
432  
433 0 ▶ while ( my $row = $dbic_rs->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $dbic_rs->next) 0 0 0
434  
435 0 my $id = $self->es_id( $row, $dbic_rs );
436  
437 0 $self->es->delete(
438 id => $id,
439 type => $self->type,
440 index => $self->index_name,
441 );
442 }
443 }
444  
445 =head2 es_transform
446  
447 Helper Function to denormalize Data. Override it in your ElasticResultSet to transform ResultObjects.
448  
449 sub es_tranform {
450  
451 my ( $self, $obj ) = @_;
452  
453 $obj->{another_document} = $schema->resultset('MyRS')->find( { id => 5 } )->get_columns
454  
455 }
456  
457 =cut
458  
459 0 ▶sub es_transform {
Subroutine es_transform: not called CC=1 SCAR=6.9
Pod documented
460  
461 0 my ( $self, $obj ) = @_;
462  
463 # overwrite transform in Elasticsearch::ResultSet's to do actions on it
464 0 return $obj;
465 }
466  
467 =head2 es_batch_index
468  
469 Indexing huge Resultsets with the Search::Elasticsearch Bulkhelper.
470  
471 Use this method for bigger Resultlists to speed up Indexing
472  
473 =cut
474  
475 0 ▶sub es_batch_index {
Subroutine es_batch_index: not called CC=4 SCAR=30.0
Pod documented
476 0 warn "Batch Indexing...\n";
477  
478 0 my $self = shift;
479 0 my $rs = shift;
480  
481 0 ▶ my $batch_size = shift || 1000;
Condition0%0/2
shift() || 1000
l!l
00
MC/DC0%0/1
shift() || 1000
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
482 0 my $data = [];
483  
484 0 my $results = $self->index_rs; # add prefetches
485 0 my $dbic_rs = $self->index_rs;
486  
487 0 $results->result_class('DBIx::Class::ResultClass::HashRefInflator');
488  
489 0 my $counter = 0;
490  
491 0 my $bulk = $self->es->bulk_helper;
492  
493 0 ▶ while ( my $row = $results->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $results->next) 0 0 0
494  
495 0 my $additional = {};
496  
497 0 $row = $self->es_transform( $row, $dbic_rs );
498 0 $row->{es_id} = $self->es_id( $row, $results );
499  
500 0 ▶ if ( $row->{_parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"_parent"}) 0 0 0
501 0 $additional->{parent} = delete $row->{_parent};
502 }
503  
504 my $params = {
505 index => $self->index_name($row),
506 id => $row->{es_id},
507 0 type => $self->type,
508 source => $row,
509 %$additional,
510 };
511  
512 0 $bulk->index($params);
513 }
514  
515 0 $bulk->flush;
516  
517 0 1;
518 }
519  
520 =head2 es_id
521  
522 Builds the document ID for the Elasticstorage based on the columns setted up in the specific ElasticResultSet
523  
524 =cut
525  
526 0 ▶sub es_id {
Subroutine es_id: not called CC=3 SCAR=24.8
Pod documented
527  
528 0 my $self = shift;
529 0 my $row = shift;
530  
531 0 my $pks = $self->es_id_columns;
532  
533 0 ▶ return unless scalar @$pks;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$pks 0 0 0
534  
535 0 my $ids = [];
536  
537 0 for my $pk (@$pks) {
538  
539 0 push @$ids, $row->{$pk};
540 }
541  
542 0 return join '_', @$ids;
543 }
544  
545 =head2
546  
547 returns Search::Elasticsearch Object from Schemastorage
548  
549 =cut
550  
551 0 ▶sub es {
Subroutine es: not called CC=1 SCAR=6.9
Pod undocumented
552  
553 0 return shift->schema->es;
554 }
555  
556 1 __PACKAGE__->meta->make_immutable;
557  
558 1 1;
« ElasticSync/Role/ElasticResult.pm ↑ Summary ElasticSync/Role/ElasticSchema.pm »