View | Details | Raw Unified | Return to bug 32707
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-2 / +59 lines)
Lines 294-300 sub build_query_compat { Link Here
294
        while ( my ( $oand, $otor, $index ) = $ea->() ) {
294
        while ( my ( $oand, $otor, $index ) = $ea->() ) {
295
            next if ( !defined($oand) || $oand eq '' );
295
            next if ( !defined($oand) || $oand eq '' );
296
            $oand = $self->clean_search_term($oand);
296
            $oand = $self->clean_search_term($oand);
297
            $oand = $self->_truncate_terms($oand) if ($truncate);
297
            $oand = $self->_truncate_terms($oand) if $truncate && $self->_is_safe_to_auto_truncate($index->{field}, $oand);
298
            push @search_params, {
298
            push @search_params, {
299
                operand => $oand,      # the search terms
299
                operand => $oand,      # the search terms
300
                operator => defined($otor) ? uc $otor : undef,    # AND and so on
300
                operator => defined($otor) ? uc $otor : undef,    # AND and so on
Lines 1338-1341 sub _search_fields { Link Here
1338
    }
1338
    }
1339
}
1339
}
1340
1340
1341
=pod
1342
1343
=head2 _is_safe_to_auto_truncate
1344
1345
_is_safe_to_auto_truncate($index_field, $oand);
1346
1347
Checks if it is safe to auto truncate a search term within a given search field.
1348
1349
The search term should not be auto truncated when searching for identifiers, e.g.
1350
koha-auth-number, record-control-number, local-number etc.  Also, non-text fields
1351
must not be auto truncated (doing so would generate ES exception).
1352
1353
=cut
1354
1355
sub _is_safe_to_auto_truncate {
1356
    my ($self, $index_field, $oand) = @_;
1357
    # Do not auto truncate fields that should not be auto truncated,
1358
    # primarily various types of identifiers, above all record identifiers.
1359
    # Other search fields that should not be auto truncated can be defined
1360
    # with ESPreventAutoTruncate syspref.
1361
    my %do_not_autotruncate_fields;
1362
    my $cache = Koha::Caches->get_instance();
1363
    my $cache_key = 'elasticsearch_search_do_not_autotruncate';
1364
    my $do_not_autotruncate_fields_ref = $cache->get_from_cache($cache_key, { unsafe => 1 });
1365
    %do_not_autotruncate_fields = %$do_not_autotruncate_fields_ref if $do_not_autotruncate_fields_ref;
1366
    if (! scalar(keys %do_not_autotruncate_fields)) {
1367
        %do_not_autotruncate_fields = map {$_ => 1} qw / biblioitemnumber host-item-number itemnumber koha-auth-number local-number /;
1368
        # In addition, under no circumstances should non-text fields
1369
        # be auto truncated.
1370
        my $schema = Koha::Database->new()->schema();
1371
        my $sfs = $schema->resultset('SearchField')->search({ '-or' => [ {type => 'boolean'}, {type => 'number'}, {type => 'sum'}, {type => 'year'} ] });
1372
        while (my $sf = $sfs->next) {
1373
            $do_not_autotruncate_fields{$sf->name} = 1;
1374
        }
1375
        $cache->set_in_cache($cache_key, \%do_not_autotruncate_fields);
1376
    }
1377
    # processing of the syspref is done outside cache since the systempreference
1378
    # can be modified and the modification should be reflected in the
1379
    # $do_not_autotruncate_fields array
1380
    my $prevent_autotruncate = C4::Context->preference('ESPreventAutoTruncate');
1381
    for my $field (split(/\s*[,;|]\s*/, $prevent_autotruncate)) {
1382
        $do_not_autotruncate_fields{$field} = 1;
1383
    }
1384
1385
    # search fields can be given as a explicit index name (e.g. from advanced
1386
    # search):
1387
    if ($index_field) {
1388
        return 0 if grep { $index_field eq $_ } keys %do_not_autotruncate_fields;
1389
    # OR can be given implicitly, as prefix in the operand (e.g. in links generated
1390
    # by Koha like catalogue/search.pl?q=an:<authid>):
1391
    } elsif ($oand =~ /\b($field_name_pattern):/) { # check field name prefixing operand
1392
        my $field_name = $1;
1393
        return 0 if grep { $field_name eq $_ } keys %do_not_autotruncate_fields;
1394
    }
1395
    # It is safe to auto truncate:
1396
    return 1;
1397
}
1398
1341
1;
1399
1;
1342
- 

Return to bug 32707