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

(-)a/C4/Search.pm (-2 / +5 lines)
Lines 1510-1521 sub buildQuery { Link Here
1510
            $limit .= "$this_limit";
1510
            $limit .= "$this_limit";
1511
            $limit_desc .= " $this_limit";
1511
            $limit_desc .= " $this_limit";
1512
        } elsif ( $this_limit =~ '^search_filter:' ) {
1512
        } elsif ( $this_limit =~ '^search_filter:' ) {
1513
            # Here we will get the query as a string, append to the limits, and pass through buildQuery
1514
            # again to clean the terms and handle nested silters
1513
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1515
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1514
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1516
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1515
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1517
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1516
            next unless $search_filter;
1518
            next unless $search_filter;
1517
            my $expanded = $search_filter->expand_filter;
1519
            my ($expanded_lim, $query_lim) = $search_filter->expand_filter;
1518
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded, undef, undef, $lang);
1520
            push @$expanded_lim, $query_lim;
1521
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded_lim, undef, undef, $lang);
1519
            $limit .= " and " if $limit || $query;
1522
            $limit .= " and " if $limit || $query;
1520
            $limit .= "$fixed_limit";
1523
            $limit .= "$fixed_limit";
1521
            $limit_desc .= " $limit";
1524
            $limit_desc .= " $limit";
(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-4 / +8 lines)
Lines 1060-1073 sub _fix_limit_special_cases { Link Here
1060
            push @new_lim, "date-of-publication:[$start TO $end]";
1060
            push @new_lim, "date-of-publication:[$start TO $end]";
1061
        }
1061
        }
1062
        elsif( $l =~ /^search_filter:/ ){
1062
        elsif( $l =~ /^search_filter:/ ){
1063
            # Here we are going to get the query as a string, clean it, and take care of the part of the limit
1064
            # Calling build_query_compat here is avoided because we generate more complex query structures
1063
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1065
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1064
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1066
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1065
            next unless $search_filter;
1067
            next unless $search_filter;
1066
            my $expanded = $search_filter->expand_filter;
1068
            my ($expanded_lim,$query_lim) = $search_filter->expand_filter;
1067
            foreach my $e ( @{$self->_fix_limit_special_cases($expanded)} ) {
1069
            # In the case of nested filters we need to expand them all
1068
                push @new_lim, $self->clean_search_term( $e );
1070
            foreach my $el ( @{$self->_fix_limit_special_cases($expanded_lim)} ){
1071
                push @new_lim, $el;
1069
            }
1072
            }
1070
#            push @new_lim, @{$self->_fix_limit_special_cases($expanded)};
1073
            # We need to clean the query part as we have built a string from the original search
1074
            push @new_lim, $self->clean_search_term( $query_lim );
1071
        }
1075
        }
1072
        elsif ( $l =~ /^yr,st-numeric=/ ) {
1076
        elsif ( $l =~ /^yr,st-numeric=/ ) {
1073
            my ($date) = ( $l =~ /^yr,st-numeric=(.*)$/ );
1077
            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 762-768 subtest 'build_query_compat() SearchLimitLibrary tests' => sub { Link Here
762
};
762
};
763
763
764
subtest "Handle search filters" => sub {
764
subtest "Handle search filters" => sub {
765
    plan tests => 4;
765
    plan tests => 7;
766
766
767
    my $qb;
767
    my $qb;
768
768
Lines 774-788 subtest "Handle search filters" => sub { Link Here
774
    my $filter = Koha::SearchFilter->new({
774
    my $filter = Koha::SearchFilter->new({
775
        name => "test",
775
        name => "test",
776
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
776
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
777
        limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
777
        limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU","available"]}|,
778
    })->store;
778
    })->store;
779
    my $filter_id = $filter->id;
779
    my $filter_id = $filter->id;
780
780
781
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
781
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
782
782
783
    is( $limit,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit correctly formed");
783
    is( $limit,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit correctly formed");
784
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
784
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
785
    is( $limit_desc,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit description is correctly expanded");
785
    is( $limit_desc,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit description is correctly expanded");
786
787
    $filter = Koha::SearchFilter->new({
788
        name => "test",
789
        query => q|{"operands":["su:biography"],"indexes":[],"operators":[]}|,
790
        limits => q|{"limits":[]}|,
791
    })->store;
792
    $filter_id = $filter->id;
793
794
    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
795
796
    is( $limit,q{(subject:biography)},"Limit correctly formed for ccl type query");
797
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
798
    is( $limit_desc,q{(subject:biography)},"Limit description is correctly handled for ccl type query");
786
799
787
};
800
};
788
801
(-)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