| 1 | package BenchmarkAnything::Storage::Backend::SQL::Search; | ||
| 2 | our $AUTHORITY = 'cpan:TAPPER'; | ||
| 3 | # ABSTRACT: searchengine support functions | ||
| 4 | $BenchmarkAnything::Storage::Backend::SQL::Search::VERSION = '0.029'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | |||
| 10 | sub json_true { bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ) } | ||
| 11 | sub json_false { bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ) } | ||
| 12 | |||
| 13 | sub _sync_search_engine_process_chunk | ||
| 14 | { | ||
| 15 | my ( $orig_sql, $b_force, $i_start, $i_end) = @_; | ||
| 16 | |||
| 17 | # === use own connection so we can run this function in parallel === | ||
| 18 | |||
| 19 | my $dbh = DBI->connect($orig_sql->{dbh_config}{dsn}, | ||
| 20 | $orig_sql->{dbh_config}{user}, | ||
| 21 | $orig_sql->{dbh_config}{password}, {'RaiseError' => 1}) | ||
| 22 | or die "benchmarkanything: can not connect: ".$DBI::errstr; | ||
| 23 | my $or_sql = BenchmarkAnything::Storage::Backend::SQL->new({dbh => $dbh, | ||
| 24 | dbh_config => $orig_sql->{dbh_config}, | ||
| 25 | debug => $orig_sql->{debug}, | ||
| 26 | force => $orig_sql->{force}, | ||
| 27 | verbose => $orig_sql->{verbose}, | ||
| 28 | searchengine => $orig_sql->{searchengine}, | ||
| 29 | }); | ||
| 30 | |||
| 31 | # === elasticsearch client === | ||
| 32 | |||
| 33 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 34 | my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client | ||
| 35 | ( | ||
| 36 | {searchengine => $or_sql->{searchengine}, ownjson => 1} | ||
| 37 | ); | ||
| 38 | my $bulk = $or_es->bulk_helper(index => $s_index, type => $s_type); | ||
| 39 | |||
| 40 | # === bulk index === | ||
| 41 | |||
| 42 | my $i_count = $i_end - $i_start + 1; | ||
| 43 | print STDERR "search-sync - process chunk: $i_start..$i_end ($i_count elements, force=$b_force)\n" if $or_sql->{verbose} || $or_sql->{debug}; | ||
| 44 | |||
| 45 | if ($b_force | ||
| 46 | # If the beginning or the end of a range do not exist we sync that window. | ||
| 47 | # Careful! This will not sync that window if there is a hole in-between! So | ||
| 48 | # if in doubt you better force a full sync. | ||
| 49 | or not $or_es->exists(index => $s_index, type => $s_type, id => $i_start) | ||
| 50 | or not $or_es->exists(index => $s_index, type => $s_type, id => $i_end) | ||
| 51 | ) | ||
| 52 | { | ||
| 53 | # Make sure to query the ::Backend::SQL store! | ||
| 54 | my $bmks = $or_sql->get_full_benchmark_points($i_start, $i_count); | ||
| 55 | $bulk->index({ id => $_->{VALUE_ID}, source => $_}) foreach @$bmks; | ||
| 56 | $bulk->flush; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub sync_search_engine_classic | ||
| 62 | { | ||
| 63 | my ( $or_self, $b_force, $i_start, $i_count) = @_; | ||
| 64 | |||
| 65 | my $i_count_datapoints = $or_self->{query}->select_count_datapoints->fetch->[0]; | ||
| 66 | |||
| 67 | for (my $i = $i_start; $i <= $i_count_datapoints; $i += $i_count) | ||
| 68 | { | ||
| 69 | my $i_end = $i + $i_count-1; | ||
| 70 | _sync_search_engine_process_chunk ($or_self, $b_force, $i, $i_end); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub init_search_engine | ||
| 76 | { | ||
| 77 | my ( $or_sql, $b_force) = @_; | ||
| 78 | |||
| 79 | my $debug = $or_sql->{debug} || $or_sql->{searchengine}{elasticsearch}{debug}; | ||
| 80 | |||
| 81 | if ( $or_sql->{searchengine}{elasticsearch}{index} ) | ||
| 82 | { | ||
| 83 | require BenchmarkAnything::Storage::Search::Elasticsearch; | ||
| 84 | my ($or_es, $s_index, $s_type) = BenchmarkAnything::Storage::Search::Elasticsearch::get_elasticsearch_client | ||
| 85 | ( | ||
| 86 | {searchengine => $or_sql->{searchengine}} | ||
| 87 | ); | ||
| 88 | |||
| 89 | # exists? | ||
| 90 | if ($or_es->indices->exists(index => $s_index) and not $b_force) | ||
| 91 | { | ||
| 92 | print STDERR "init_search_engine: index '$s_index' already exists, use force to delete and recreate.\n"; | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | # delete | ||
| 97 | if ($or_es->indices->exists(index => $s_index)) | ||
| 98 | { | ||
| 99 | my $response = $or_es->indices->delete(index => $s_index); | ||
| 100 | } | ||
| 101 | |||
| 102 | my $dynamic_templates = [ | ||
| 103 | #{"typed_field_VALUE_(KEY FROM QUERY OPTIONS)" => { "match" => "VALUE", "mapping" => { "type" => "long", } } }, | ||
| 104 | { "typed_field_VALUE_numeric" => { "match" => "VALUE", "mapping" => { "type" => "long", } } }, | ||
| 105 | { "typed_field_VALUE_text" => { "match" => "VALUE", "mapping" => { "type" => "text", } } }, | ||
| 106 | { "core_field_NAME" => { "match" => "NAME", "mapping" => { "type" => "keyword", "store" => json_true } } }, | ||
| 107 | { "core_field_VALUE" => { "match" => "VALUE", "mapping" => { "type" => "text", } } }, | ||
| 108 | { "core_field_UNIT" => { "match" => "UNIT", "mapping" => { "type" => "text", } } }, | ||
| 109 | { "core_field_VALUE_ID" => { "match" => "VALUE_ID", "mapping" => { "type" => "long", } } }, | ||
| 110 | { "core_field_CREATED" => { "match" => "CREATED", "mapping" => { "type" => "date", format => 'yyyy-MM-dd||yyyy-MM-dd HH:mm:ss', } } }, | ||
| 111 | { "non_core_fields" => { "match" => "*", "mapping" => { "type" => "keyword", } } }, | ||
| 112 | ]; | ||
| 113 | |||
| 114 | # mappings | ||
| 115 | my $mappings = { | ||
| 116 | ($or_sql->{searchengine}{elasticsearch}{client} eq '5_0::Direct') | ||
| 117 | ? ( $s_type => { dynamic_templates => $dynamic_templates } ) | ||
| 118 | : ( dynamic_templates => $dynamic_templates ) | ||
| 119 | }; | ||
| 120 | require JSON::XS; | ||
| 121 | require Hash::Merge; | ||
| 122 | require Data::Dumper; | ||
| 123 | local $Types::Serialiser::true; | ||
| 124 | my $merge = Hash::Merge->new( 'RIGHT_PRECEDENT' ); | ||
| 125 | my $additional_mappings = $or_sql->{searchengine}{elasticsearch}{additional_mappings} || {}; | ||
| 126 | $mappings = $merge->merge($mappings, $additional_mappings); | ||
| 127 | print STDERR "create.mappings:\n".JSON::XS->new->convert_blessed->pretty->encode($mappings) if $debug; | ||
| 128 | |||
| 129 | # create | ||
| 130 | my $answer = $or_es->indices->create | ||
| 131 | ( | ||
| 132 | index => $s_index, | ||
| 133 | body => { mappings => $mappings }, | ||
| 134 | ); | ||
| 135 | print STDERR "create.answer:\n".Data::Dumper::Dumper($answer) if $debug; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sub sync_search_engine | ||
| 141 | { | ||
| 142 | my ( $or_sql, $b_force, $i_start, $i_bulkcount) = @_; | ||
| 143 | |||
| 144 | $i_start ||= 1; | ||
| 145 | $i_bulkcount ||= 10_000; | ||
| 146 | |||
| 147 | if ($or_sql->{searchengine}{elasticsearch}) | ||
| 148 | { | ||
| 149 | eval { require BenchmarkAnything::Storage::Backend::SQL::Search::MCE }; | ||
| 150 | |||
| 151 | if (!$@) { | ||
| 152 | # parallel with MCE | ||
| 153 | print STDERR "search-sync - parallel processing (using MCE)\n" if $or_sql->{verbose} || $or_sql->{debug}; | ||
| 154 | BenchmarkAnything::Storage::Backend::SQL::Search::MCE::sync_search_engine_mce( $or_sql, $b_force, $i_start, $i_bulkcount); | ||
| 155 | } else { | ||
| 156 | # non-parallel fallback | ||
| 157 | print STDERR "search-sync - serial processing\n" if $or_sql->{verbose} || $or_sql->{debug}; | ||
| 158 | sync_search_engine_classic($or_sql, $b_force, $i_start, $i_bulkcount); | ||
| 159 | } | ||
| 160 | } else { | ||
| 161 | # Unsupported search engine | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | |||
| 167 | __END__ |