Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/BenchmarkAnything/Storage/Search/Elasticsearch.pm |
Statements | Executed 9020 statements in 22.5ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1001 | 2 | 1 | 23.6ms | 1.88s | get_elasticsearch_client | BenchmarkAnything::Storage::Search::Elasticsearch::
1 | 1 | 1 | 2.03ms | 4.34ms | BEGIN@10 | BenchmarkAnything::Storage::Search::Elasticsearch::
1 | 1 | 1 | 17µs | 17µs | BEGIN@7 | BenchmarkAnything::Storage::Search::Elasticsearch::
1 | 1 | 1 | 8µs | 11µs | BEGIN@8 | BenchmarkAnything::Storage::Search::Elasticsearch::
1 | 1 | 1 | 6µs | 13µs | BEGIN@9 | BenchmarkAnything::Storage::Search::Elasticsearch::
0 | 0 | 0 | 0s | 0s | get_elasticsearch_query | BenchmarkAnything::Storage::Search::Elasticsearch::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package BenchmarkAnything::Storage::Search::Elasticsearch; | ||||
2 | # git description: v0.002-0-gf3d2be9 | ||||
3 | |||||
4 | 1 | 300ns | our $AUTHORITY = 'cpan:SCHWIGON'; | ||
5 | # ABSTRACT: Utility functions to use Elasticsearch with BenchmarkAnything storage | ||||
6 | 1 | 200ns | $BenchmarkAnything::Storage::Search::Elasticsearch::VERSION = '0.003'; | ||
7 | 2 | 44µs | 1 | 17µs | # spent 17µs within BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@7 which was called:
# once (17µs+0s) by BenchmarkAnything::Storage::Backend::SQL::add_single_benchmark at line 7 # spent 17µs making 1 call to BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@7 |
8 | 2 | 20µs | 2 | 14µs | # spent 11µs (8+3) within BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@8 which was called:
# once (8µs+3µs) by BenchmarkAnything::Storage::Backend::SQL::add_single_benchmark at line 8 # spent 11µs making 1 call to BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@8
# spent 3µs making 1 call to strict::import |
9 | 2 | 23µs | 2 | 19µs | # spent 13µs (6+7) within BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@9 which was called:
# once (6µs+7µs) by BenchmarkAnything::Storage::Backend::SQL::add_single_benchmark at line 9 # spent 13µs making 1 call to BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@9
# spent 6µs making 1 call to warnings::import |
10 | 2 | 521µs | 2 | 4.41ms | # spent 4.34ms (2.03+2.31) within BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@10 which was called:
# once (2.03ms+2.31ms) by BenchmarkAnything::Storage::Backend::SQL::add_single_benchmark at line 10 # spent 4.34ms making 1 call to BenchmarkAnything::Storage::Search::Elasticsearch::BEGIN@10
# spent 74µs making 1 call to Exporter::import |
11 | |||||
12 | |||||
13 | # spent 1.88s (23.6ms+1.85) within BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client which was called 1001 times, avg 1.87ms/call:
# 1000 times (23.6ms+1.85s) by BenchmarkAnything::Storage::Backend::SQL::add_single_benchmark at line 368 of BenchmarkAnything/Storage/Backend/SQL.pm, avg 1.87ms/call
# once (18µs+3.74ms) by BenchmarkAnything::Storage::Backend::SQL::gc at line 461 of BenchmarkAnything/Storage/Backend/SQL.pm | ||||
14 | |||||
15 | 1001 | 558µs | my ( $opt ) = @_; | ||
16 | |||||
17 | 1001 | 801µs | require Search::Elasticsearch; | ||
18 | |||||
19 | 1001 | 966µs | my $cfg = $opt->{searchengine}; | ||
20 | 1001 | 1.15ms | my $s_index = $cfg->{elasticsearch}{index}; | ||
21 | 1001 | 875µs | my $s_type = $cfg->{elasticsearch}{type}; | ||
22 | 1001 | 381µs | die "benchmarkanything-storage-search-elasticsearch: missing config 'elasticsearch.index'" unless $s_index; | ||
23 | 1001 | 288µs | die "benchmarkanything-storage-search-elasticsearch: missing config 'elasticsearch.type'" unless $s_type; | ||
24 | |||||
25 | # Elasticsearch | ||||
26 | my $or_es = Search::Elasticsearch->new | ||||
27 | (client => ($cfg->{client} || "5_0::Direct"), | ||||
28 | 1001 | 7.28ms | 1001 | 1.85s | $opt->{ownjson} ? (serializer => "+BenchmarkAnything::Storage::Search::Elasticsearch::Serializer::JSON::DontTouchMyUTF8") : (), # spent 1.85s making 1001 calls to Search::Elasticsearch::new, avg 1.84ms/call |
29 | ); | ||||
30 | |||||
31 | 1001 | 9.56ms | return wantarray ? ($or_es, $s_index, $s_type) : $or_es; | ||
32 | } | ||||
33 | |||||
34 | |||||
35 | sub get_elasticsearch_query | ||||
36 | { | ||||
37 | |||||
38 | my ( $hr_ba_query ) = @_; | ||||
39 | |||||
40 | my $hr_es_query; | ||||
41 | my $default_max_size = 10_000; | ||||
42 | |||||
43 | my $ar_ba_select = $hr_ba_query->{select}; # select:done; aggregators! | ||||
44 | my $ar_ba_where = $hr_ba_query->{where}; # done | ||||
45 | my $i_ba_limit = $hr_ba_query->{limit}; # done | ||||
46 | my $i_ba_offset = $hr_ba_query->{offset}; # done | ||||
47 | my $ar_ba_order_by = $hr_ba_query->{order_by}; # done | ||||
48 | |||||
49 | my @must_ranges; | ||||
50 | my @must_matches; | ||||
51 | my @must_not_matches; | ||||
52 | my @should_matches; | ||||
53 | 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 | $source_fields{$_} = 1 for @$ar_ba_select; | ||||
63 | my %source = @$ar_ba_select ? ( _source => [ keys %source_fields ] ) : (); | ||||
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 | : ( sort => [ map { my @e = (); | ||||
71 | # No error handling for bogus input here, hm... | ||||
72 | if (ref and ref eq 'ARRAY') | ||||
73 | { | ||||
74 | my $k = $_->[0]; | ||||
75 | my $direction = lc($_->[1]) || 'asc'; | ||||
76 | my $options = $_->[2]; # (eg. numeric=>1) - IGNORED! We let Elasticsearch figure out. | ||||
77 | @e = ({ $k => { order => $direction } }); | ||||
78 | } | ||||
79 | elsif (defined($_)) # STRING | ||||
80 | { | ||||
81 | @e = ({ $_ => { order => "asc" } }); | ||||
82 | } | ||||
83 | else | ||||
84 | { | ||||
85 | require Data::Dumper; | ||||
86 | print STDERR "_get_elasticsearch_query: unknown order_by clause: ".Data::Dumper::Dumper($_)."\n"; | ||||
87 | return; | ||||
88 | } | ||||
89 | @e | ||||
90 | } @{ $ar_ba_order_by || [] } | ||||
91 | ]); | ||||
92 | my %from = !$i_ba_offset ? () : ( from => $i_ba_offset+1 ); | ||||
93 | my %size = ( size => ($i_ba_limit || $default_max_size) ); | ||||
94 | |||||
95 | my %range_operator = | ||||
96 | ( | ||||
97 | '<' => 'lt', | ||||
98 | '>' => 'gt', | ||||
99 | '<=' => 'lte', | ||||
100 | '>=' => 'gte', | ||||
101 | ); | ||||
102 | my %match_operator = | ||||
103 | ( | ||||
104 | '=' => 1, | ||||
105 | ); | ||||
106 | my %not_match_operator = | ||||
107 | ( | ||||
108 | '!=' => 1, | ||||
109 | ); | ||||
110 | my %wildcard_match_operator = | ||||
111 | ( | ||||
112 | 'like' => 1, | ||||
113 | ); | ||||
114 | my %wildcard_not_match_operator = | ||||
115 | ( | ||||
116 | 'not like' => 1, | ||||
117 | ); | ||||
118 | foreach my $w (@{ $ar_ba_where || [] }) | ||||
119 | { | ||||
120 | my $op = $w->[0]; # operator | ||||
121 | my $k = $w->[1]; # key | ||||
122 | my @v = map { encode_utf8($_) } @$w[2..@$w-1]; # value(s) | ||||
123 | |||||
124 | my $es_op; | ||||
125 | if ($es_op = $range_operator{$op}) | ||||
126 | { | ||||
127 | push @must_ranges, { range => { $k => { $es_op => $v[0] } } }; | ||||
128 | } | ||||
129 | elsif ($es_op = $match_operator{$op}) | ||||
130 | { | ||||
131 | if (@v > 1) { | ||||
132 | push @should_matches, { match => { $k => $_ } } foreach @v; | ||||
133 | } else { | ||||
134 | push @must_matches, { match => { $k => $v[0] } }; | ||||
135 | } | ||||
136 | } | ||||
137 | elsif ($es_op = $not_match_operator{$op}) | ||||
138 | { | ||||
139 | push @must_not_matches, { match => { $k => $_ } } foreach @v; | ||||
140 | } | ||||
141 | elsif ($es_op = $wildcard_match_operator{$op}) | ||||
142 | { | ||||
143 | my $es_v = $v[0]; | ||||
144 | $es_v =~ s/%/*/g; | ||||
145 | push @must_matches, { wildcard => { $k => $es_v } }; | ||||
146 | } | ||||
147 | elsif ($es_op = $wildcard_not_match_operator{$op}) | ||||
148 | { | ||||
149 | my $es_v = $v[0]; | ||||
150 | $es_v =~ s/%/*/g; | ||||
151 | push @must_not_matches, { wildcard => { $k => $es_v } }; | ||||
152 | } | ||||
153 | else | ||||
154 | { | ||||
155 | print STDERR "_get_elasticsearch_query: Unsupported operator: $op\n"; | ||||
156 | return; | ||||
157 | } | ||||
158 | } | ||||
159 | |||||
160 | $hr_es_query = { query => { bool => { | ||||
161 | (@must_matches||@must_ranges ? (must => [ @must_ranges, @must_matches ]) : ()), | ||||
162 | (@must_not_matches ? (must_not => [ @must_not_matches ]) : ()), | ||||
163 | (@should_matches ? (should => [ @should_matches ],minimum_should_match => 1) : ()), | ||||
164 | }, | ||||
165 | }, | ||||
166 | %from, | ||||
167 | %size, | ||||
168 | %sort, | ||||
169 | %source, | ||||
170 | }; | ||||
171 | |||||
172 | return $hr_es_query; | ||||
173 | } | ||||
174 | |||||
175 | 1 | 3µs | 1; | ||
176 | |||||
177 | __END__ |