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 1068-1081 sub _fix_limit_special_cases { Link Here
1068
            push @new_lim, "date-of-publication:[$start TO $end]";
1068
            push @new_lim, "date-of-publication:[$start TO $end]";
1069
        }
1069
        }
1070
        elsif( $l =~ /^search_filter:/ ){
1070
        elsif( $l =~ /^search_filter:/ ){
1071
            # Here we are going to get the query as a string, clean it, and take care of the part of the limit
1072
            # Calling build_query_compat here is avoided because we generate more complex query structures
1071
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1073
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1072
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1074
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1073
            next unless $search_filter;
1075
            next unless $search_filter;
1074
            my $expanded = $search_filter->expand_filter;
1076
            my ($expanded_lim,$query_lim) = $search_filter->expand_filter;
1075
            foreach my $e ( @{$self->_fix_limit_special_cases($expanded)} ) {
1077
            # In the case of nested filters we need to expand them all
1076
                push @new_lim, $self->clean_search_term( $e );
1078
            foreach my $el ( @{$self->_fix_limit_special_cases($expanded_lim)} ){
1079
                push @new_lim, $el;
1077
            }
1080
            }
1078
#            push @new_lim, @{$self->_fix_limit_special_cases($expanded)};
1081
            # We need to clean the query part as we have built a string from the original search
1082
            push @new_lim, $self->clean_search_term( $query_lim );
1079
        }
1083
        }
1080
        elsif ( $l =~ /^yr,st-numeric=/ ) {
1084
        elsif ( $l =~ /^yr,st-numeric=/ ) {
1081
            my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ );
1085
            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 774-780 subtest 'build_query_compat() SearchLimitLibrary tests' => sub { Link Here
774
};
774
};
775
775
776
subtest "Handle search filters" => sub {
776
subtest "Handle search filters" => sub {
777
    plan tests => 4;
777
    plan tests => 7;
778
778
779
    my $qb;
779
    my $qb;
780
780
Lines 786-800 subtest "Handle search filters" => sub { Link Here
786
    my $filter = Koha::SearchFilter->new({
786
    my $filter = Koha::SearchFilter->new({
787
        name => "test",
787
        name => "test",
788
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
788
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
789
        limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
789
        limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU","available"]}|,
790
    })->store;
790
    })->store;
791
    my $filter_id = $filter->id;
791
    my $filter_id = $filter->id;
792
792
793
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
793
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
794
794
795
    is( $limit,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit correctly formed");
795
    is( $limit,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit correctly formed");
796
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
796
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
797
    is( $limit_desc,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit description is correctly expanded");
797
    is( $limit_desc,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit description is correctly expanded");
798
799
    $filter = Koha::SearchFilter->new({
800
        name => "test",
801
        query => q|{"operands":["su:biography"],"indexes":[],"operators":[]}|,
802
        limits => q|{"limits":[]}|,
803
    })->store;
804
    $filter_id = $filter->id;
805
806
    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
807
808
    is( $limit,q{(subject:biography)},"Limit correctly formed for ccl type query");
809
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
810
    is( $limit_desc,q{(subject:biography)},"Limit description is correctly handled for ccl type query");
798
811
799
};
812
};
800
813
(-)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