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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-4 / +3 lines)
Lines 209-215 sub build_query_compat { Link Here
209
        my %options;
209
        my %options;
210
        $options{fields} = \@fields;
210
        $options{fields} = \@fields;
211
        $options{sort} = \@sort_params;
211
        $options{sort} = \@sort_params;
212
        $options{expanded_facet} = $params->{expanded_facet};
213
        $query = $self->build_query( $query_str, %options );
212
        $query = $self->build_query( $query_str, %options );
214
    }
213
    }
215
214
Lines 452-458 sub build_authorities_query_compat { Link Here
452
451
453
=head2 _build_scan_query
452
=head2 _build_scan_query
454
453
455
    my ($query, $query_str) = $builder->build_scan_query(\@operands, \@indexes)
454
    my ($query, $query_str) = $builder->_build_scan_query(\@operands, \@indexes)
456
455
457
This will build an aggregation scan query that can be issued to elasticsearch from
456
This will build an aggregation scan query that can be issued to elasticsearch from
458
the provided string input.
457
the provided string input.
Lines 471-477 sub _build_scan_query { Link Here
471
    my ( $self, $operands, $indexes ) = @_;
470
    my ( $self, $operands, $indexes ) = @_;
472
471
473
    my $term = scalar( @$operands ) == 0 ? '' : $operands->[0];
472
    my $term = scalar( @$operands ) == 0 ? '' : $operands->[0];
474
    my $index = scalar( @$indexes ) == 0 ? 'title' : $indexes->[0];
473
    my $index = scalar( @$indexes ) == 0 ? 'subject' : $indexes->[0];
475
474
476
    my ( $f, $d ) = split( /,/, $index);
475
    my ( $f, $d ) = split( /,/, $index);
477
    $index = $scan_field_convert{$f} || $f;
476
    $index = $scan_field_convert{$f} || $f;
Lines 496-502 sub _build_scan_query { Link Here
496
495
497
=head2 _create_regex_filter
496
=head2 _create_regex_filter
498
497
499
    my $filter = $builder->create_regex_filter('term')
498
    my $filter = $builder->_create_regex_filter('term')
500
499
501
This will create a regex filter that can be used with an aggregation query.
500
This will create a regex filter that can be used with an aggregation query.
502
501
(-)a/Koha/SearchEngine/Elasticsearch/Search.pm (-12 / +23 lines)
Lines 132-139 sub count { Link Here
132
132
133
    my ( $error, $results, $facets ) = $search->search_compat(
133
    my ( $error, $results, $facets ) = $search->search_compat(
134
        $query,            $simple_query, \@sort_by,       \@servers,
134
        $query,            $simple_query, \@sort_by,       \@servers,
135
        $results_per_page, $offset,       $branches,       $query_type,
135
        $results_per_page, $offset,       undef,           $item_types,
136
        $scan
136
        $query_type,       $scan
137
      )
137
      )
138
138
139
A search interface somewhat compatible with L<C4::Search->getRecords>. Anything
139
A search interface somewhat compatible with L<C4::Search->getRecords>. Anything
Lines 144-152 get ignored here, along with some other things (like C<@servers>.) Link Here
144
144
145
sub search_compat {
145
sub search_compat {
146
    my (
146
    my (
147
        $self,     $query,            $simple_query, $sort_by,
147
        $self,       $query,            $simple_query, $sort_by,
148
        $servers,  $results_per_page, $offset,       $branches,
148
        $servers,    $results_per_page, $offset,       $branches,
149
        $query_type,       $scan
149
        $item_types, $query_type,       $scan
150
    ) = @_;
150
    ) = @_;
151
151
152
    if ( $scan ) {
152
    if ( $scan ) {
Lines 544-558 sub _aggregation_scan { Link Here
544
    my $count = scalar(@{$results->{aggregations}{$field}{buckets}});
544
    my $count = scalar(@{$results->{aggregations}{$field}{buckets}});
545
    for (my $index = $offset; $index - $offset < $results_per_page && $index < $count; $index++) {
545
    for (my $index = $offset; $index - $offset < $results_per_page && $index < $count; $index++) {
546
        my $bucket = $results->{aggregations}{$field}{buckets}->[$index];
546
        my $bucket = $results->{aggregations}{$field}{buckets}->[$index];
547
        # Scan values are expressed as 100 a (count) and 245a (term)
547
        # Scan values are expressed as:
548
        # - MARC21: 100a (count) and 245a (term)
549
        # - UNIMARC: 200f (count) and 200a (term)
548
        my $marc = MARC::Record->new;
550
        my $marc = MARC::Record->new;
549
        $marc->encoding('UTF-8');
551
        $marc->encoding('UTF-8');
550
        $marc->append_fields(
552
        if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
551
            MARC::Field->new((100, ' ',  ' ', 'a' => $bucket->{doc_count}))
553
            $marc->append_fields(
552
        );
554
                MARC::Field->new((200, ' ',  ' ', 'f' => $bucket->{doc_count}))
553
        $marc->append_fields(
555
            );
554
            MARC::Field->new((245, ' ',  ' ', 'a' => $bucket->{key}))
556
            $marc->append_fields(
555
        );
557
                MARC::Field->new((200, ' ',  ' ', 'a' => $bucket->{key}))
558
            );
559
        } else {
560
            $marc->append_fields(
561
                MARC::Field->new((100, ' ',  ' ', 'a' => $bucket->{doc_count}))
562
            );
563
            $marc->append_fields(
564
                MARC::Field->new((245, ' ',  ' ', 'a' => $bucket->{key}))
565
            );
566
        }
556
        $records[$index] = $marc->as_usmarc();
567
        $records[$index] = $marc->as_usmarc();
557
    };
568
    };
558
    # consumers of this expect a namespaced result, we provide the default
569
    # consumers of this expect a namespaced result, we provide the default
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-3 / +21 lines)
Lines 170-176 subtest 'build_authorities_query_compat() tests' => sub { Link Here
170
};
170
};
171
171
172
subtest 'build_query tests' => sub {
172
subtest 'build_query tests' => sub {
173
    plan tests => 40;
173
    plan tests => 48;
174
174
175
    my $qb;
175
    my $qb;
176
176
Lines 419-425 subtest 'build_query tests' => sub { Link Here
419
    is($query_desc, 'title:"donald duck"', 'query desc ok');
419
    is($query_desc, 'title:"donald duck"', 'query desc ok');
420
420
421
    # Scan queries
421
    # Scan queries
422
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['su'], undef, undef, 1 );
422
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
423
    is(
424
        $query->{query}{query_string}{query},
425
        '*',
426
        "scan query is properly formed"
427
    );
428
    is_deeply(
429
        $query->{aggregations}{'author'}{'terms'},
430
        {
431
            field => 'author__facet',
432
            order => { '_term' => 'asc' },
433
            include => '[nN][eE][wW].*'
434
        },
435
        "scan aggregation request is properly formed"
436
    );
437
    is($query_cgi, 'idx=au&q=new&scan=1', 'query cgi');
438
    is($query_desc, 'new', 'query desc ok');
439
440
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], [], undef, undef, 1 );
423
    is(
441
    is(
424
        $query->{query}{query_string}{query},
442
        $query->{query}{query_string}{query},
425
        '*',
443
        '*',
Lines 434-440 subtest 'build_query tests' => sub { Link Here
434
        },
452
        },
435
        "scan aggregation request is properly formed"
453
        "scan aggregation request is properly formed"
436
    );
454
    );
437
    is($query_cgi, 'idx=su&q=new&scan=1', 'query cgi');
455
    is($query_cgi, 'idx=&q=new&scan=1', 'query cgi');
438
    is($query_desc, 'new', 'query desc ok');
456
    is($query_desc, 'new', 'query desc ok');
439
};
457
};
440
458
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Search.t (-2 / +11 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 11;
20
use Test::More tests => 13;
21
use t::lib::Mocks;
21
use t::lib::Mocks;
22
22
23
use Koha::SearchEngine::Elasticsearch::QueryBuilder;
23
use Koha::SearchEngine::Elasticsearch::QueryBuilder;
Lines 100-105 SKIP: { Link Here
100
100
101
    ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
101
    ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
102
102
103
    my ( undef, $scan_query ) = $builder->build_query_compat( undef, ['easy'], [], undef, undef, 1 );
104
    ok ((undef, $results) = $searcher->search_compat( $scan_query, undef, [], [], 20, 0, undef, undef, undef, 1 ), 'Test search_compat scan query' );
105
    my $expected = {
106
        biblioserver => {
107
            hits => 0,
108
            RECORDS => []
109
        }
110
    };
111
    is_deeply($results, $expected, 'Scan query results ok');
112
103
    ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
113
    ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
104
114
105
    is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
115
    is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
106
- 

Return to bug 22592