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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-2 / +51 lines)
Lines 293-299 sub build_query_compat { Link Here
293
        while ( my ( $oand, $otor, $index ) = $ea->() ) {
293
        while ( my ( $oand, $otor, $index ) = $ea->() ) {
294
            next if ( !defined($oand) || $oand eq '' );
294
            next if ( !defined($oand) || $oand eq '' );
295
            $oand = $self->clean_search_term($oand);
295
            $oand = $self->clean_search_term($oand);
296
            $oand = $self->_truncate_terms($oand) if ($truncate);
296
            $oand = $self->_truncate_terms($oand) if $truncate && $self->_is_safe_to_auto_truncate($index->{field}, $oand);
297
            push @search_params, {
297
            push @search_params, {
298
                operand => $oand,      # the search terms
298
                operand => $oand,      # the search terms
299
                operator => defined($otor) ? uc $otor : undef,    # AND and so on
299
                operator => defined($otor) ? uc $otor : undef,    # AND and so on
Lines 1305-1308 sub _search_fields { Link Here
1305
    }
1305
    }
1306
}
1306
}
1307
1307
1308
=pod
1309
1310
=head2 _is_safe_to_auto_truncate
1311
1312
_is_safe_to_auto_truncate($index_field, $oand);
1313
1314
Checks if it is safe to auto truncate a search term within a given search field.
1315
1316
The search term should not be auto truncated when searching for identifiers, e.g.
1317
koha-auth-number, record-control-number, local-number etc.  Also, non-text fields
1318
must not be auto truncated (doing so would generate ES exception).
1319
1320
=cut
1321
1322
sub _is_safe_to_auto_truncate {
1323
    my ($self, $index_field, $oand) = @_;
1324
    # Do not auto truncate fields that should not be auto truncated,
1325
    # primarily various types of identifiers, above all record identifiers.
1326
    # Alternatively this could be moved to the ElasticSearch search fields
1327
    # configuration page.
1328
    my @do_not_truncate = qw / barcode biblioitemnumber control-number control-number-identifier date-of-acquisition date-of-publication date-time-last-modified host-item-number identifier-standard isbn issn itemnumber itype koha-auth-number lc-card-number local-number number-local-acquisition other-control-number record-control-number /;
1329
    # In addition, under no circumstances should non-text fields
1330
    # be auto truncated.
1331
    my $cache = Koha::Caches->get_instance();
1332
    my $cache_key = 'elasticsearch_search_non_text_fields';
1333
    my $non_text_search_fields = $cache->get_from_cache($cache_key, { unsafe => 1 });
1334
    if (!$non_text_search_fields) {
1335
        my $schema = Koha::Database->new()->schema();
1336
        my $sfs = $schema->resultset('SearchField')->search({ '-not' => { '-or' => [ {type => ''}, {type => 'string'} ] } });
1337
        while (my $sf = $sfs->next) {
1338
	    push @$non_text_search_fields, $sf->name;
1339
	}
1340
        $cache->set_in_cache($cache_key, $non_text_search_fields);
1341
    }
1342
    push @do_not_truncate, @$non_text_search_fields if $non_text_search_fields;
1343
1344
    # search fields can be given as a explicit index name (e.g. from advanced
1345
    # search):
1346
    if ($index_field) {
1347
	return 0 if grep { $index_field eq $_ } @do_not_truncate;
1348
    # OR can be given implicite, as prefix in the operand (e.g. in links generated
1349
    # by Koha like catalogue/search.pl?q=an:<authid>):
1350
    } elsif ($oand =~ /\b($field_name_pattern):/) { # check field name prefixing operand
1351
	my $field_name = $1;
1352
	return 0 if grep { $field_name eq $_ } @do_not_truncate;
1353
    }
1354
    # It safe to auto truncate:
1355
    return 1;
1356
}
1357
1308
1;
1358
1;
1309
- 

Return to bug 32707