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

(-)a/C4/Search.pm (-2 / +5 lines)
Lines 1301-1312 sub buildQuery { Link Here
1301
            $limit .= "$this_limit";
1301
            $limit .= "$this_limit";
1302
            $limit_desc .= " $this_limit";
1302
            $limit_desc .= " $this_limit";
1303
        } elsif ( $this_limit =~ '^search_filter:' ) {
1303
        } elsif ( $this_limit =~ '^search_filter:' ) {
1304
            # Here we will get the query as a string, append to the limits, and pass through buildQuery
1305
            # again to clean the terms and handle nested silters
1304
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1306
            $limit_cgi  .= "&limit=" . uri_escape_utf8($this_limit);
1305
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1307
            my ($filter_id) = ( $this_limit =~ /^search_filter:(.*)$/ );
1306
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1308
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1307
            next unless $search_filter;
1309
            next unless $search_filter;
1308
            my $expanded = $search_filter->expand_filter;
1310
            my ($expanded_lim, $query_lim) = $search_filter->expand_filter;
1309
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded, undef, undef, $lang);
1311
            push @$expanded_lim, $query_lim;
1312
            my ( $error, undef, undef, undef, undef, $fixed_limit, undef, undef, undef ) = buildQuery ( undef, undef, undef, $expanded_lim, undef, undef, $lang);
1310
            $limit .= " and " if $limit || $query;
1313
            $limit .= " and " if $limit || $query;
1311
            $limit .= "$fixed_limit";
1314
            $limit .= "$fixed_limit";
1312
            $limit_desc .= " $limit";
1315
            $limit_desc .= " $limit";
(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-3 / +8 lines)
Lines 1069-1081 sub _fix_limit_special_cases { Link Here
1069
            push @new_lim, "date-of-publication:[$start TO $end]";
1069
            push @new_lim, "date-of-publication:[$start TO $end]";
1070
        }
1070
        }
1071
        elsif( $l =~ /^search_filter:/ ){
1071
        elsif( $l =~ /^search_filter:/ ){
1072
            # Here we are going to get the query as a string, clean it, and take care of the part of the limit
1073
            # Calling build_query_compat here is avoided because we generate more complex query structures
1072
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1074
            my ($filter_id) = ( $l =~ /^search_filter:(.*)$/ );
1073
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1075
            my $search_filter = Koha::SearchFilters->find( $filter_id );
1074
            next unless $search_filter;
1076
            next unless $search_filter;
1075
            my $expanded = $search_filter->expand_filter;
1077
            my ($expanded_lim,$query_lim) = $search_filter->expand_filter;
1076
            foreach my $e ( @{$self->_fix_limit_special_cases($expanded)} ) {
1078
            # In the case of nested filters we need to expand them all
1077
                push @new_lim, $self->clean_search_term( $e );
1079
            foreach my $el ( @{$self->_fix_limit_special_cases($expanded_lim)} ){
1080
                push @new_lim, $el;
1078
            }
1081
            }
1082
            # We need to clean the query part as we have built a string from the original search
1083
            push @new_lim, $self->clean_search_term( $query_lim );
1079
        }
1084
        }
1080
        elsif ( $l =~ /^yr,st-numeric[=:]/ ) {
1085
        elsif ( $l =~ /^yr,st-numeric[=:]/ ) {
1081
            my ($date) = ( $l =~ /^yr,st-numeric[=:](.*)$/ );
1086
            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 782-788 subtest 'build_query_compat() SearchLimitLibrary tests' => sub { Link Here
782
};
782
};
783
783
784
subtest "Handle search filters" => sub {
784
subtest "Handle search filters" => sub {
785
    plan tests => 4;
785
    plan tests => 7;
786
786
787
    my $qb;
787
    my $qb;
788
788
Lines 794-808 subtest "Handle search filters" => sub { Link Here
794
    my $filter = Koha::SearchFilter->new({
794
    my $filter = Koha::SearchFilter->new({
795
        name => "test",
795
        name => "test",
796
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
796
        query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
797
        limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
797
        limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU","available"]}|,
798
    })->store;
798
    })->store;
799
    my $filter_id = $filter->id;
799
    my $filter_id = $filter->id;
800
800
801
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
801
    my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
802
802
803
    is( $limit,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit correctly formed");
803
    is( $limit,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit correctly formed");
804
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
804
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
805
    is( $limit_desc,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit description is correctly expanded");
805
    is( $limit_desc,q{(onloan:false) AND ((cat) AND title:(bat) OR author:(rat)) AND itype:(("BK") OR ("MU"))},"Limit description is correctly expanded");
806
807
    $filter = Koha::SearchFilter->new({
808
        name => "test",
809
        query => q|{"operands":["su:biography"],"indexes":[],"operators":[]}|,
810
        limits => q|{"limits":[]}|,
811
    })->store;
812
    $filter_id = $filter->id;
813
814
    ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
815
816
    is( $limit,q{(subject:biography)},"Limit correctly formed for ccl type query");
817
    is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
818
    is( $limit_desc,q{(subject:biography)},"Limit description is correctly handled for ccl type query");
806
819
807
};
820
};
808
821
(-)a/t/db_dependent/Koha/SearchEngine/Zebra/QueryBuilder.t (-5 / +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
};
124
- 

Return to bug 17170