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