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

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

Return to bug 32707