Elasticsearch.pm

filter: Statementstmt 15.7% 14 / 89 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 10.7% 20 / 186 CC 38 SCAR 66.8
CC
38
Cov
9%
get_elasticsearch_query
1056.0
get_elasticsearch_client
56.0
BEGIN
1.0
CRAP 795.1
↑ Summary Elasticsearch/Serializer/JSON/DontTouchMyUTF8.pm »
1 package BenchmarkAnything::Storage::Search::Elasticsearch;
2 # git description: v0.004-2-gcd52c72
3  
4 1 our $AUTHORITY = 'cpan:SCHWIGON';
5 # ABSTRACT: Utility functions to use Elasticsearch with BenchmarkAnything storage
6 1 $BenchmarkAnything::Storage::Search::Elasticsearch::VERSION = '0.005';
7 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Encode 'encode_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 0 ▶sub get_elasticsearch_client {
Subroutine get_elasticsearch_client: not called CC=7 SCAR=40.3
Pod documented
14  
15 0 my ( $opt ) = @_;
16  
17 0 require Search::Elasticsearch;
18  
19 0 my $cfg = $opt->{searchengine};
20 0 my $s_index = $cfg->{elasticsearch}{index};
21 0 my $s_type = $cfg->{elasticsearch}{type};
22 0 ▶ die "benchmarkanything-storage-search-elasticsearch: missing config 'elasticsearch.index'" unless $s_index;
Branch: 1 branch
BranchTrueFalseTotal
unless $s_index 0 0 0
23 0 ▶ die "benchmarkanything-storage-search-elasticsearch: missing config 'elasticsearch.type'" unless $s_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $s_type 0 0 0
24  
25 # Elasticsearch
26 my %es_cfg =
27 (client => ($cfg->{elasticsearch}{client} || "5_0::Direct"),
28 nodes => ($cfg->{elasticsearch}{nodes} || ["localhost:9200"]),
29 0 ▶ $opt->{ownjson} ? (serializer => "+BenchmarkAnything::Storage::Search::Elasticsearch::Serializer::JSON::DontTouchMyUTF8") : (),
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'ownjson'} ? : 0 0 0
Condition0%0/4
$cfg->{'elasticsearch'}{'client'} || '5_0::Direct'
l!l
00
$cfg->{'elasticsearch'}{'nodes'} || ['localhost:9200']
l!l
00
MC/DC0%0/1
$cfg->{'elasticsearch'}{'client'} || '5_0::Direct'
$cfg->{'elasticsearch'}{'client'}
MC/DC0%0/1
$cfg->{'elasticsearch'}{'nodes'} || ['localhost:9200']
$cfg->{'elasticsearch'}{'nodes'}
Truth table0%0/2
A
A= $cfg->{'elasticsearch'}{'client'}
Aresult
11
00
Truth table0%0/2
A
A= $cfg->{'elasticsearch'}{'nodes'}
Aresult
11
00
30 );
31 0 my $or_es = Search::Elasticsearch->new(%es_cfg);
32  
33 0 ▶ return wantarray ? ($or_es, $s_index, $s_type) : $or_es;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
34 }
35  
36  
37 sub get_elasticsearch_query
38 0 ▶{
Subroutine get_elasticsearch_query: not called CC=32 SCAR=69.6
Pod documented
39  
40 0 my ( $hr_ba_query ) = @_;
41  
42 0 my $hr_es_query;
43 0 my $default_max_size = 10_000;
44  
45 0 my $ar_ba_select = $hr_ba_query->{select}; # select:done; aggregators!
46 0 my $ar_ba_where = $hr_ba_query->{where}; # done
47 0 my $i_ba_limit = $hr_ba_query->{limit}; # done
48 0 my $i_ba_offset = $hr_ba_query->{offset}; # done
49 0 my $ar_ba_order_by = $hr_ba_query->{order_by}; # done
50  
51 0 my @must_ranges;
52 my @must_matches;
53 0 my @must_not_matches;
54 0 my @should_matches;
55 0 my %source_fields = (
56 NAME => 1,
57 UNIT => 1,
58 VALUE => 1,
59 VALUE_ID => 1,
60 CREATED => 1,
61 );
62  
63 # select fields
64 0 $source_fields{$_} = 1 for @$ar_ba_select;
65 0 ▶ my %source = @$ar_ba_select ? ( _source => [ keys %source_fields ] ) : ();
Branch: 1 branch
BranchTrueFalseTotal
@$ar_ba_select ? : 0 0 0
66  
67 # The 'sort' entry should get an always-the-same canonical
68 # structure because get_mapping/properties below relies on that.
69 my %sort =
70 !$ar_ba_order_by
71 ? ( sort => [ { VALUE_ID => { order => "asc" }} ] ) # default
72 0 : ( sort => [ map { my @e = ();
73 # No error handling for bogus input here, hm...
74 0 ▶ if (ref and ref eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ and ref $_ eq 'ARRAY') { } 0 0 0
elsif (defined $_) { } 0 0 0
Condition0%0/3
ref $_ and ref $_ eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and ref $_ eq "ARRAY"
ref $_ ref $_ eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_B= ref $_ eq "ARRAY"
ABresult
0X0
100
111
75 {
76 0 my $k = $_->[0];
77 0 ▶ my $direction = lc($_->[1]) || 'asc';
Condition0%0/2
lc $_->[1] || 'asc'
l!l
00
MC/DC0%0/1
lc $_->[1] || 'asc'
lc $_->[1]
Truth table0%0/2
A
A= lc $_->[1]
Aresult
11
00
78 0 my $options = $_->[2]; # (eg. numeric=>1) - IGNORED! We let Elasticsearch figure out. XXX: use different field: typed_field_VALUE_long if {numeric} == 1
79 0 @e = ({ $k => { order => $direction } });
80 }
81 elsif (defined($_)) # STRING
82 {
83 0 @e = ({ $_ => { order => "asc" } });
84 }
85 else
86 {
87 0 require Data::Dumper;
88 0 warn "_get_elasticsearch_query: unknown order_by clause: ".Data::Dumper::Dumper($_)."\n";
89 0 return;
90 }
91 @e
92 0 ▶ } @{ $ar_ba_order_by || [] }
Branch: 1 branch
BranchTrueFalseTotal
!$ar_ba_order_by ? : 0 0 0
Condition0%0/2
$ar_ba_order_by || []
l!l
00
MC/DC0%0/1
$ar_ba_order_by || []
$ar_ba_order_by
Truth table0%0/2
A
A= $ar_ba_order_by
Aresult
11
00
93 ]);
94 0 ▶ my %from = !$i_ba_offset ? () : ( from => $i_ba_offset );
Branch: 1 branch
BranchTrueFalseTotal
!$i_ba_offset ? : 0 0 0
95 0 ▶ my %size = ( size => ($i_ba_limit || $default_max_size) );
Condition0%0/3
$i_ba_limit || $default_max_size
l!l&&r!l&&!r
000
MC/DC0%0/2
$i_ba_limit || $default_max_size
$i_ba_limit $default_max_size
Truth table0%0/3
A || B
A= $i_ba_limitB= $default_max_size
ABresult
1X1
011
000
96  
97 0 my %range_operator =
98 (
99 '<' => 'lt',
100 '>' => 'gt',
101 '<=' => 'lte',
102 '>=' => 'gte',
103 );
104 0 my %match_operator =
105 (
106 '=' => 1,
107 );
108 0 my %not_match_operator =
109 (
110 '!=' => 1,
111 );
112 0 my %empty_match_operator =
113 (
114 'is_empty' => 1,
115 );
116 0 my %wildcard_match_operator =
117 (
118 'like' => 1,
119 );
120 0 my %wildcard_not_match_operator =
121 (
122 'not like' => 1, # deprecated
123 'not_like' => 1,
124 );
125 0 ▶ foreach my $w (@{ $ar_ba_where || [] })
Condition0%0/2
$ar_ba_where || []
l!l
00
MC/DC0%0/1
$ar_ba_where || []
$ar_ba_where
Truth table0%0/2
A
A= $ar_ba_where
Aresult
11
00
126 {
127 0 my $op = $w->[0]; # operator
128 0 my $k = $w->[1]; # key
129 0 my @v = map { encode_utf8($_) } @$w[2..@$w-1]; # value(s)
130  
131 0 my $es_op;
132 0 ▶ if ($es_op = $range_operator{$op})
Branch: 6 branches
BranchTrueFalseTotal
if ($es_op = $range_operator{$op}) { } 0 0 0
elsif ($es_op = $match_operator{$op}) { } 0 0 0
elsif ($es_op = $not_match_operator{$op}) { } 0 0 0
elsif ($es_op = $wildcard_match_operator{$op}) { } 0 0 0
elsif ($es_op = $empty_match_operator{$op}) { } 0 0 0
elsif ($es_op = $wildcard_not_match_operator{$op}) { } 0 0 0
133 {
134 0 push @must_ranges, { range => { $k => { $es_op => $v[0] } } };
135 }
136 elsif ($es_op = $match_operator{$op})
137 {
138 0 ▶ if (@v > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v > 1) { } 0 0 0
139 0 push @should_matches, { match => { $k => $_ } } foreach @v;
140 } else {
141 0 push @must_matches, { match => { $k => $v[0] } };
142 }
143 }
144 elsif ($es_op = $not_match_operator{$op})
145 {
146 0 push @must_not_matches, { match => { $k => $_ } } foreach @v;
147 }
148 elsif ($es_op = $wildcard_match_operator{$op})
149 {
150 0 my $es_v = $v[0];
151 0 $es_v =~ s/%/*/g;
152 0 push @must_matches, { wildcard => { $k => $es_v } };
153 }
154 elsif ($es_op = $empty_match_operator{$op})
155 {
156 0 ▶ if (@v and $v[0] == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (@v and $v[0] == 0) { } 0 0 0
elsif (@v and $v[0] == 2) { } 0 0 0
elsif (not @v or $v[0] == 1) { } 0 0 0
Condition0%0/3
@v and $v[0] == 0
!ll&&!rl&&r
000
MC/DC0%0/2
@v and $v[0] == 0
@v $v[0] == 0
Truth table0%0/3
A and B
A= @vB= $v[0] == 0
ABresult
0X0
100
111
157 # field is NOT EMPTY: [ "is_empty", "some_field_name", 0 ]
158 0 push @must_matches, { wildcard => { $k => '*' } };
159  
160 ▶ } elsif (@v and $v[0] == 2) {
Condition0%0/3
@v and $v[0] == 2
!ll&&!rl&&r
000
MC/DC0%0/2
@v and $v[0] == 2
@v $v[0] == 2
Truth table0%0/3
A and B
A= @vB= $v[0] == 2
ABresult
0X0
100
111
161 # field is EMPTY but exists (ie. not undefined): [ "is_empty", "some_field_name", 2 ]
162 0 push @must_not_matches, { wildcard => { $k => '*' } };
163 0 push @must_matches, { exists => { field => $k } };
164  
165 ▶ } elsif (not @v or $v[0] == 1) {
Condition0%0/3
not @v or $v[0] == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not @v or $v[0] == 1
not @v $v[0] == 1
Truth table0%0/3
not(A) or B
A= not @vB= $v[0] == 1
ABresult
1X1
011
000
166 # field is EMPTY or UNDEFINED/NULL: [ "is_empty", "some_field_name", 1 ] or [ "is_empty", "some_field_name" ]
167 0 push @must_not_matches, { wildcard => { $k => '*' } };
168  
169 } else {
170 # we might invent other semantics so we better warn about
171 # what could once become meaningful.
172 0 warn "unclear 'is_empty' condition (".$v[0]."). Interpreting as 'is_empty' condition (1).";
173 0 push @must_not_matches, { wildcard => { $k => '*' } };
174 }
175 }
176 elsif ($es_op = $wildcard_not_match_operator{$op})
177 {
178 0 my $es_v = $v[0];
179 0 $es_v =~ s/%/*/g;
180 0 push @must_not_matches, { wildcard => { $k => $es_v } };
181 }
182 else
183 {
184 0 warn "_get_elasticsearch_query: Unsupported operator: $op\n";
185 0 return;
186 }
187 }
188  
189 0 ▶ $hr_es_query = { query => { bool => {
Branch: 3 branches
BranchTrueFalseTotal
@must_matches || @must_ranges ? : 0 0 0
@must_not_matches ? : 0 0 0
@should_matches ? : 0 0 0
Condition0%0/3
@must_matches or @must_ranges
l!l&&r!l&&!r
000
MC/DC0%0/2
@must_matches or @must_ranges
@must_matches @must_ranges
Truth table0%0/3
A or B
A= @must_matchesB= @must_ranges
ABresult
1X1
011
000
190 (@must_matches||@must_ranges ? (must => [ @must_ranges, @must_matches ]) : ()),
191 (@must_not_matches ? (must_not => [ @must_not_matches ]) : ()),
192 (@should_matches ? (should => [ @should_matches ],minimum_should_match => 1) : ()),
193 },
194 },
195 %from,
196 %size,
197 %sort,
198 %source,
199 };
200  
201 0 return $hr_es_query;
202 }
203  
204 1 1;
205  
206 __END__
↑ Summary Elasticsearch/Serializer/JSON/DontTouchMyUTF8.pm »