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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-8 / +8 lines)
Lines 715-728 to ensure those parts are correct. Link Here
715
sub _clean_search_term {
715
sub _clean_search_term {
716
    my ( $self, $term ) = @_;
716
    my ( $self, $term ) = @_;
717
717
718
    my $auto_truncation  = C4::Context->preference("QueryAutoTruncate")    || 0;
718
    my $auto_truncation = C4::Context->preference("QueryAutoTruncate") || 0;
719
719
720
    # Some hardcoded searches (like with authorities) produce things like
720
    # Some hardcoded searches (like with authorities) produce things like
721
    # 'an=123', when it ought to be 'an:123' for our purposes.
721
    # 'an=123', when it ought to be 'an:123' for our purposes.
722
    $term =~ s/=/:/g;
722
    $term =~ s/=/:/g;
723
    $term = $self->_convert_index_strings_freeform($term);
723
    $term = $self->_convert_index_strings_freeform($term);
724
    $term =~ s/[{}]/"/g;
724
    $term =~ s/[{}]/"/g;
725
    $term = $self->_truncate_terms($term) if ( $auto_truncation );
725
    $term = $self->_truncate_terms($term) if ($auto_truncation);
726
    return $term;
726
    return $term;
727
}
727
}
728
728
Lines 793-808 operands. Link Here
793
=cut
793
=cut
794
794
795
sub _truncate_terms {
795
sub _truncate_terms {
796
    my ($self, $query) = @_;
796
    my ( $self, $query ) = @_;
797
    my @stops = qw/and or not/;
797
    my @stops = qw/and or not/;
798
    my @new_terms;
798
    my @new_terms;
799
    my @split_query = split /[\(\s\)]/, $query ;
799
    my @split_query = split /[\(\s\)]/, $query;
800
    foreach  my $term ( @split_query ) {
800
    foreach my $term (@split_query) {
801
        next if ($term eq '' || $term eq ' ' ) ;
801
        next if ( $term eq '' || $term eq ' ' );
802
        $term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ($term =~ /\*$/ ) );
802
        $term .= "*" unless ( ( grep { lc($term) =~ /^$_$/ } @stops ) || ( $term =~ /\*$/ ) );
803
        push @new_terms, $term;
803
        push @new_terms, $term;
804
    }
804
    }
805
    $query=join ' ' ,@new_terms;
805
    $query = join ' ', @new_terms;
806
    return $query;
806
    return $query;
807
}
807
}
808
808
(-)a/t/db_dependent/Koha_SearchEngine_Elasticsearch_Search.t (-10 / +29 lines)
Lines 102-119 subtest 'build_query tests' => sub { Link Here
102
    ok( !defined $query->{aggregations}{holdingbranch},
102
    ok( !defined $query->{aggregations}{holdingbranch},
103
        'holdingbranch not added to facets if DisplayLibraryFacets=home' );
103
        'holdingbranch not added to facets if DisplayLibraryFacets=home' );
104
104
105
    t::lib::Mocks::mock_preference('QueryAutoTruncate','');
105
    t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
106
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
107
    is( $query->{query}{query_string}{query}, "(donald duck)", "query not altered if QueryAutoTruncate disabled" );
108
106
109
    t::lib::Mocks::mock_preference('QueryAutoTruncate','1');
110
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
107
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
111
    is( $query->{query}{query_string}{query}, "(donald* duck*)", "simple query is auto truncated when QueryAutoTruncate enabled" );
108
    is(
112
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald or duck and mickey not mouse'] );
109
        $query->{query}{query_string}{query},
113
    is( $query->{query}{query_string}{query}, "(donald* or duck* and mickey* not mouse*)", "reserved words are not affected by QueryAutoTruncate" ); #Ensure reserved words are not truncated
110
        "(donald duck)",
114
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
111
        "query not altered if QueryAutoTruncate disabled"
115
    is( $query->{query}{query_string}{query}, "(donald* duck*)", "query with '*' is unaltered when QueryAutoTruncate is enabled" );
112
    );
116
113
114
    t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
117
115
116
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
117
    is(
118
        $query->{query}{query_string}{query},
119
        "(donald* duck*)",
120
        "simple query is auto truncated when QueryAutoTruncate enabled"
121
    );
122
123
    # Ensure reserved words are not truncated
124
    ( undef, $query ) = $builder->build_query_compat( undef,
125
        ['donald or duck and mickey not mouse'] );
126
    is(
127
        $query->{query}{query_string}{query},
128
        "(donald* or duck* and mickey* not mouse*)",
129
        "reserved words are not affected by QueryAutoTruncate"
130
    );
131
132
    ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
133
    is(
134
        $query->{query}{query_string}{query},
135
        "(donald* duck*)",
136
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
137
    );
118
};
138
};
119
139
120
- 

Return to bug 18374