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

(-)a/C4/Search.pm (-2 / +5 lines)
Lines 1295-1306 sub buildQuery { Link Here
1295
            $limit .= "$this_limit";
1295
            $limit .= "$this_limit";
1296
            $limit_desc .= " $this_limit";
1296
            $limit_desc .= " $this_limit";
1297
        } elsif ( $this_limit =~ '^search_filter:' ) {
1297
        } elsif ( $this_limit =~ '^search_filter:' ) {
1298
            # Here we will get the query as a string, append to the limits, and pass through buildQuery
1299
            # again to clean the terms and handle nested silters
1298
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1300
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1299
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1301
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1300
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1302
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1301
            next unless $search_filter;
1303
            next unless $search_filter;
1302
            my $expanded = $search_filter->expand_filter;
1304
            my ($expanded_lim, $query_lim) = $search_filter->expand_filter;
1303
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded, undef, undef, $lang);
1305
            push @$expanded_lim, $query_lim;
1306
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded_lim, undef, undef, $lang);
1304
            $limit .= " and " if $limit || $query;
1307
            $limit .= " and " if $limit || $query;
1305
            $limit .= "$fixed_limit";
1308
            $limit .= "$fixed_limit";
1306
            $limit_desc .= " $limit";
1309
            $limit_desc .= " $limit";
(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-4 / +8 lines)
Lines 1062-1075 sub _fix_limit_special_cases { Link Here
1062
            push @new_lim, "date-of-publication:[$start TO $end]";
1062
            push @new_lim, "date-of-publication:[$start TO $end]";
1063
        }
1063
        }
1064
        elsif( $l =~ /^search_filter:/ ){
1064
        elsif( $l =~ /^search_filter:/ ){
1065
            # Here we are going to get the query as a string, clean it, and take care of the part of the limit
1066
            # Calling build_query_compat here is avoided because we generate more complex query structures
1065
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1067
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1066
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1068
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1067
            next unless $search_filter;
1069
            next unless $search_filter;
1068
            my $expanded = $search_filter->expand_filter;
1070
            my ($expanded_lim,$query_lim) = $search_filter->expand_filter;
1069
            foreach my $e ( @{$self->_fix_limit_special_cases($expanded)} ) {
1071
            # In the case of nested filters we need to expand them all
1070
                push @new_lim, $self->clean_search_term( $e );
1072
            foreach my $el ( @{$self->_fix_limit_special_cases($expanded_lim)} ){
1073
                push @new_lim, $el;
1071
            }
1074
            }
1072
#            push @new_lim, @{$self->_fix_limit_special_cases($expanded)};
1075
            # We need to clean the query part as we have built a string from the original search
1076
            push @new_lim, $self->clean_search_term( $query_lim );
1073
        }
1077
        }
1074
        elsif ( $l =~ /^yr,st-numeric=/ ) {
1078
        elsif ( $l =~ /^yr,st-numeric=/ ) {
1075
            my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ );
1079
            my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ );
(-)a/Koha/SearchFilter.pm (-6 / +4 lines)
Lines 43-52 sub to_api_mapping { Link Here
43
43
44
=head3 expand_filter
44
=head3 expand_filter
45
45
46
    my $expanded_filter = $filter->expand_filter;
46
    my ($expanded_limit, $query_limit) = $filter->expand_filter;
47
47
48
    Returns the filter as an arrayref of limit queries suitable to
48
    Returns the filter as an arrayref of limit queries, and the query parts combined
49
    be passed to QueryBuilder
49
    into a string suitable to be passed to QueryBuilder
50
50
51
=cut
51
=cut
52
52
Lines 74-82 sub expand_filter { Link Here
74
        $query_limit .= $limit;
74
        $query_limit .= $limit;
75
    }
75
    }
76
76
77
    push @$limits, "(".$query_limit.")" if $query_limit;
77
    return ($limits, $query_limit);
78
79
    return $limits;
80
}
78
}
81
79
82
=head2 Internal methods
80
=head2 Internal methods
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-4 / +17 lines)
Lines 760-766 subtest 'build_query_compat() SearchLimitLibrary tests' => sub { Link Here
760
};
760
};
761
761
762
subtest "Handle search filters" => sub {
762
subtest "Handle search filters" => sub {
763
    plan tests => 4;
763
    plan tests => 7;
764
764
765
    my $qb;
765
    my $qb;
766
766
Lines 772-786 subtest "Handle search filters" => sub { Link Here
772
    my $filter = Koha::SearchFilter->new({
772
    my $filter = Koha::SearchFilter->new({
773
        name => "test",
773
        name => "test",
774
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
774
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
775
        limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
775
        limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU","available"]}|,
776
    })->store;
776
    })->store;
777
    my $filter_id = $filter->id;
777
    my $filter_id = $filter->id;
778
778
779
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
779
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
780
780
781
    is( $limit,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit correctly formed");
781
    is( $limit,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit correctly formed");
782
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
782
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
783
    is( $limit_desc,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit description is correctly expanded");
783
    is( $limit_desc,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit description is correctly expanded");
784
785
    $filter = Koha::SearchFilter->new({
786
        name => "test",
787
        query => q|{"operands":["su:biography"],"indexes":[],"operators":[]}|,
788
        limits => q|{"limits":[]}|,
789
    })->store;
790
    $filter_id = $filter->id;
791
792
    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
793
794
    is( $limit,q{(subject:biography)},"Limit correctly formed for ccl type query");
795
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
796
    is( $limit_desc,q{(subject:biography)},"Limit description is correctly handled for ccl type query");
784
797
785
};
798
};
786
799
(-)a/t/db_dependent/Koha/SearchEngine/Zebra/QueryBuilder.t (-4 / +17 lines)
Lines 98-104 subtest 'build_query_compat() SearchLimitLibrary tests' => sub { Link Here
98
};
98
};
99
99
100
subtest "Handle search filters" => sub {
100
subtest "Handle search filters" => sub {
101
    plan tests => 4;
101
    plan tests => 7;
102
102
103
    my $qb;
103
    my $qb;
104
104
Lines 110-123 subtest "Handle search filters" => sub { Link Here
110
    my $filter = Koha::SearchFilter->new({
110
    my $filter = Koha::SearchFilter->new({
111
        name => "test",
111
        name => "test",
112
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
112
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
113
        limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
113
        limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU","available"]}|,
114
    })->store;
114
    })->store;
115
    my $filter_id = $filter->id;
115
    my $filter_id = $filter->id;
116
116
117
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
117
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
118
118
119
    is( $limit,q{(kw=(cat) AND ti=(bat) OR au=(rat)) and (mc-itype,phr=BK) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit correctly formed");
119
    is( $limit,q{kw=(cat) AND ti=(bat) OR au=(rat) and (mc-itype,phr=BK or mc-itype,phr=MU) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit correctly formed");
120
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
120
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
121
    is( $limit_desc,q{(kw=(cat) AND ti=(bat) OR au=(rat)) and (mc-itype,phr=BK) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit description is correctly expanded");
121
    is( $limit_desc,q{kw=(cat) AND ti=(bat) OR au=(rat) and (mc-itype,phr=BK or mc-itype,phr=MU) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit description is correctly expanded");
122
123
    $filter = Koha::SearchFilter->new({
124
        name => "test",
125
        query => q|{"operands":["su:biography"],"indexes":[],"operators":[]}|,
126
        limits => q|{"limits":[]}|,
127
    })->store;
128
    $filter_id = $filter->id;
129
130
    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
131
132
    is( $limit,q{(su=biography)},"Limit correctly formed for ccl type query");
133
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
134
    is( $limit_desc,q{(su=biography)},"Limit description is correctly handled for ccl type query");
122
135
123
};
136
};
(-)a/t/db_dependent/Koha/SearchFilters.t (-1 / +54 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2022 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 6;
23
24
use Koha::SearchFilters;
25
use Koha::Database;
26
27
use t::lib::TestBuilder;
28
29
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
31
32
my $builder = t::lib::TestBuilder->new;
33
my $nb_of_filters = Koha::SearchFilters->search->count;
34
my $new_filter_1 = Koha::SearchFilter->new({
35
    name => "Test 1",
36
    query => q|{"operators":["OR"],"indexes":["kw","kw","kw"],"operands":["street","bruce",""]}|,
37
    limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU"]}|
38
})->store;
39
my $new_filter_2 = Koha::SearchFilter->new({ name => "Test 2" })->store;
40
41
like( $new_filter_1->id, qr|^\d+$|, 'Adding a new filter should have set the id');
42
is( Koha::SearchFilters->search->count, $nb_of_filters + 2, 'The 2 filters should have been added' );
43
44
my $retrieved_filter_2 = Koha::SearchFilters->find( $new_filter_2->id );
45
is( $retrieved_filter_2->name, $new_filter_2->name, 'Find a filter by id should return the correct filter' );
46
47
$retrieved_filter_2->delete;
48
is( Koha::SearchFilters->search->count, $nb_of_filters + 1, 'Delete should have deleted the filter' );
49
50
my ($expanded_lim,$expanded_query) = $new_filter_1->expand_filter;
51
is_deeply( $expanded_lim, ["mc-itype,phr:BK","mc-itype,phr:MU"], "Expanded limits is an arrayref of limits");
52
is( $expanded_query,"kw=(street) OR kw=(bruce)","Expanded query is a search string");
53
54
$schema->storage->txn_rollback;

Return to bug 17170