Bug 22831: (follow-up) Sort results, group by db
[koha.git] / misc / maintenance / compare_es_to_db.pl
1 #! /usr/bin/perl
2 #
3 # This compares record counts from a Koha database to Elasticsearch
4
5 # Copyright 2019 ByWater Solutions
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 =head1 NAME
23
24 compare_es_to_db.pl - compares record counts from a Koha database to Elasticsearch
25
26 =head1 SYNOPSIS
27
28 B<compare_es_to_db.pl>
29
30 =cut
31
32 use Modern::Perl;
33 use Array::Utils qw( array_diff );
34
35 use C4::Context;
36
37 use Koha::Authorities;
38 use Koha::Biblios;
39 use Koha::Items;
40 use Koha::SearchEngine::Elasticsearch;
41
42 foreach my $index ( ('biblios','authorities') ){
43     print "=================\n";
44     print "Checking $index\n";
45     my @db_records = $index eq 'biblios' ? Koha::Biblios->search()->get_column('biblionumber') : Koha::Authorities->search()->get_column('authid');
46
47     my $searcher = Koha::SearchEngine::Elasticsearch->new({ index => $index });
48     my $es = $searcher->get_elasticsearch();
49     my $count = $es->indices->stats( index => $searcher->get_elasticsearch_params->{index_name} )
50         ->{_all}{primaries}{docs}{count};
51     print "Count in db for $index is " . scalar @db_records . ", count in Elasticsearch is $count\n";
52
53     # Now we get all the ids from Elasticsearch
54     # The scroll lets us iterate through, it fetches chunks of 'size' as we move through
55     my $scroll = $es->scroll_helper(
56         index => $searcher->get_elasticsearch_params->{index_name},
57         size => 5000,
58         body => {
59             query => {
60                 match_all => {}
61             },
62             stored_fields => []
63         },
64         scroll_in_qs => 1,
65     );
66
67     my @es_ids;
68
69     # Here is where we actually iterate through
70     # Fetching each record, pushing the id into the array
71     my $i = 1;
72     print "Fetching Elasticsearch records ids";
73     while (my $doc = $scroll->next ){
74         print "." if !($i % 500);
75         print "\n$i records retrieved" if !($i % 5000);
76         push @es_ids, $doc->{_id};
77         $i++;
78     }
79     print "\nComparing arrays, this may take a while\n";
80
81     # And compare the arrays
82     # array_diff returns only a list of values which are not common to both arrays
83     my @diff = array_diff(@db_records, @es_ids );
84
85     print "All records match\n" unless @diff;
86
87     # Fetch values for providing record links
88     my $es_params = $searcher->get_elasticsearch_params;
89     my $es_base   = "$es_params->{nodes}[0]/$es_params->{index_name}";
90     my $opac_base = C4::Context->preference('OPACBaseURL');
91
92
93     my @koha_problems;
94     my @es_problems;
95     foreach my $problem ( sort { $a <=> $b } @diff){
96         if ( (grep /^$problem$/, @db_records) ){
97             push @koha_problems, $problem;
98         } else {
99             push @es_problems, $problem;
100         }
101     }
102
103     if ( @koha_problems ){
104         print "=================\n";
105         print "Records that exist in Koha but not in ES\n";
106         for my $problem ( @koha_problems ){
107             if ( $index eq 'biblios' ) {
108                 print "  #$problem";
109                 print "  Visit here to see record: $opac_base/cgi-bin/koha/opac-detail.pl?biblionumber=$problem\n";
110             } elsif ( $index eq 'authorities' ) {
111                 print "#$problem";
112                 print "  Visit here to see record: $opac_base/cgi-bin/koha/opac-authoritiesdetail.pl?authid=$problem\n";
113             }
114         }
115     }
116     if ( @es_problems ){
117         print "=================\n";
118         print "Records that exist in ES but not in Koha\n";
119         for my $problem ( @es_problems ){
120             print "  #$problem";
121             print "  Enter this command to view record: curl $es_base/data/$problem?pretty=true\n";
122         }
123     }
124 }