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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-5 / +9 lines)
Lines 315-321 sub build_authorities_query { Link Here
315
    foreach my $s ( @{ $search->{searches} } ) {
315
    foreach my $s ( @{ $search->{searches} } ) {
316
        my ( $wh, $op, $val ) = @{$s}{qw(where operator value)};
316
        my ( $wh, $op, $val ) = @{$s}{qw(where operator value)};
317
        $wh = '_all' if $wh eq '';
317
        $wh = '_all' if $wh eq '';
318
        if ( $op eq 'is' || $op eq '='  || $op eq 'exact' ) {
318
        if ( defined $op && ($op eq 'is' || $op eq '='  || $op eq 'exact') ) {
319
319
320
            # look for something that matches a term completely
320
            # look for something that matches a term completely
321
            # note, '=' is about numerical vals. May need special handling.
321
            # note, '=' is about numerical vals. May need special handling.
Lines 324-330 sub build_authorities_query { Link Here
324
            # search analyzer applied to them.
324
            # search analyzer applied to them.
325
            push @query_parts, { match_phrase => {"$wh.phrase" => lc $val} };
325
            push @query_parts, { match_phrase => {"$wh.phrase" => lc $val} };
326
        }
326
        }
327
        elsif ( $op eq 'start' ) {
327
        elsif ( defined $op && $op eq 'start' ) {
328
            # startswith search, uses lowercase untokenized version of heading
328
            # startswith search, uses lowercase untokenized version of heading
329
            push @query_parts, { match_phrase_prefix => {"$wh.phrase" => lc $val} };
329
            push @query_parts, { match_phrase_prefix => {"$wh.phrase" => lc $val} };
330
        }
330
        }
Lines 449-469 sub build_authorities_query_compat { Link Here
449
    # This turns the old-style many-options argument form into a more
449
    # This turns the old-style many-options argument form into a more
450
    # extensible hash form that is understood by L<build_authorities_query>.
450
    # extensible hash form that is understood by L<build_authorities_query>.
451
    my @searches;
451
    my @searches;
452
    my $mappings = $self->get_elasticsearch_mappings();
452
453
453
    # Convert to lower case
454
    # Convert to lower case
454
    $marclist = [map(lc, @{$marclist})];
455
    $marclist = [map(lc, @{$marclist})];
455
    $orderby  = lc $orderby;
456
    $orderby  = lc $orderby;
456
457
458
    my @indexes;
457
    # Make sure everything exists
459
    # Make sure everything exists
458
    foreach my $m (@$marclist) {
460
    foreach my $m (@$marclist) {
459
        Koha::Exceptions::WrongParameter->throw("Invalid marclist field provided: $m")
461
460
            unless exists $koha_to_index_name->{$m};
462
        $m = exists $koha_to_index_name->{$m} ? $koha_to_index_name->{$m} : $m;
463
        push @indexes, $m;
464
        warn "Unknown search field $m in marclist" unless (defined $mappings->{data}->{properties}->{$m} || $m eq '');
461
    }
465
    }
462
    for ( my $i = 0 ; $i < @$value ; $i++ ) {
466
    for ( my $i = 0 ; $i < @$value ; $i++ ) {
463
        next unless $value->[$i]; #clean empty form values, ES doesn't like undefined searches
467
        next unless $value->[$i]; #clean empty form values, ES doesn't like undefined searches
464
        push @searches,
468
        push @searches,
465
          {
469
          {
466
            where    => $koha_to_index_name->{$marclist->[$i]},
470
            where    => $indexes[$i],
467
            operator => $operator->[$i],
471
            operator => $operator->[$i],
468
            value    => $value->[$i],
472
            value    => $value->[$i],
469
          };
473
          };
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-10 / +37 lines)
Lines 19-24 use Modern::Perl; Link Here
19
19
20
use C4::Context;
20
use C4::Context;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use t::lib::Mocks;
23
use t::lib::Mocks;
23
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
24
use Test::More tests => 6;
25
use Test::More tests => 6;
Lines 47-52 $se->mock( 'get_elasticsearch_mappings', sub { Link Here
47
                subject => {
48
                subject => {
48
                    type => 'text'
49
                    type => 'text'
49
                },
50
                },
51
                'subject-heading-thesaurus' => {
52
                    type => 'text',
53
                    facet => 1
54
                },
50
                itemnumber => {
55
                itemnumber => {
51
                    type => 'integer'
56
                    type => 'integer'
52
                },
57
                },
Lines 56-67 $se->mock( 'get_elasticsearch_mappings', sub { Link Here
56
                sortablenumber__sort => {
61
                sortablenumber__sort => {
57
                    type => 'integer'
62
                    type => 'integer'
58
                },
63
                },
59
                Heading => {
64
                heading => {
60
                    type => 'text'
65
                    type => 'text'
61
                },
66
                },
62
                Heading__sort => {
67
                'heading-main' => {
63
                    type => 'text'
68
                    type => 'text'
64
                }
69
                },
70
                heading__sort => {
71
                    type => 'text'
72
                },
73
                match => {
74
                    type => 'text'
75
                },
76
                'match-heading' => {
77
                    type => 'text'
78
                },
79
                'match-heading-see-from' => {
80
                    type => 'text'
81
                },
65
            }
82
            }
66
        }
83
        }
67
    };
84
    };
Lines 82-88 $se->mock( 'get_elasticsearch_mappings', sub { Link Here
82
});
99
});
83
100
84
subtest 'build_authorities_query_compat() tests' => sub {
101
subtest 'build_authorities_query_compat() tests' => sub {
85
    plan tests => 45;
102
    plan tests => 47;
86
103
87
    my $qb;
104
    my $qb;
88
105
Lines 169-180 subtest 'build_authorities_query_compat() tests' => sub { Link Here
169
        "authorities type code is used as filter"
186
        "authorities type code is used as filter"
170
    );
187
    );
171
188
172
    # Failing case
189
    # Authorities marclist check
173
    throws_ok {
190
    warning_like {
174
        $qb->build_authorities_query_compat( [ 'tomas' ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
191
        $query = $qb->build_authorities_query_compat( [ 'tomas','mainentry' ],  undef, undef, ['contains'], [$search_term,$search_term], 'AUTH_TYPE', 'asc' )
175
    }
192
    }
176
    'Koha::Exceptions::WrongParameter',
193
    qr/Unknown search field tomas/,
177
        'Exception thrown on invalid value in the marclist param';
194
    "Warning for unknown field in marclist";
195
    is_deeply(
196
        $query->{query}->{bool}->{must}[0]->{query_string}->{default_field},
197
        'tomas',
198
        "If no mapping for marclist the index is passed through as defined"
199
    );
200
    is_deeply(
201
        $query->{query}->{bool}->{must}[1]->{query_string}{default_field},
202
        'heading',
203
        "If mapping found for marclist the index is passed through converted"
204
    );
205
178
};
206
};
179
207
180
subtest 'build_query tests' => sub {
208
subtest 'build_query tests' => sub {
181
- 

Return to bug 23719