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