3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use t::lib::TestBuilder;
25 use Test::More tests => 8;
27 use List::Util qw( all );
30 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
31 use Koha::SearchFilters;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
37 $se->mock( 'get_elasticsearch_mappings', sub {
54 'subject-heading-thesaurus' => {
58 'subject-heading-thesaurus-conventions' => {
67 sortablenumber__sort => {
85 'match-heading-see-from' => {
90 $all_mappings{$self->index} = $mappings;
101 $self->sort_fields($sort_fields->{$self->index});
103 return $all_mappings{$self->index};
106 subtest 'build_authorities_query_compat() tests' => sub {
113 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
114 'Creating new query builder object for authorities'
117 my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
118 my $search_term = 'a';
119 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
120 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
121 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
122 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
125 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
128 is( $query->{query}->{bool}->{must}[0]->{query_string}->{analyze_wildcard}, JSON::true, 'Set analyze_wildcard true' );
129 is( $query->{query}->{bool}->{must}[0]->{query_string}->{lenient}, JSON::true, 'Set lenient true' );
132 $search_term = 'Donald Duck';
133 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
134 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
135 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query}, "(Donald*) AND (Duck*)" );
136 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
137 isa_ok( $query->{query}->{bool}->{must}[0]->{query_string}->{fields}, 'ARRAY')
139 is( $query->{query}->{bool}->{must}[0]->{query_string}->{default_field}, $koha_to_index_name->{$koha_name} );
143 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
144 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['is'], [$search_term], 'AUTH_TYPE', 'asc' );
145 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
147 $query->{query}->{bool}->{must}[0]->{multi_match}->{query},
150 my $all_matches = all { /\.ci_raw$/ }
151 @{$query->{query}->{bool}->{must}[0]->{multi_match}->{fields}};
152 ok( $all_matches, 'Correct fields parameter for "is" query in "any" or "all"' );
155 $query->{query}->{bool}->{must}[0]->{term}->{$koha_to_index_name->{$koha_name} . ".ci_raw"},
161 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
162 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'asc' );
163 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
164 my $all_matches = all { (%{$_->{prefix}})[0] =~ /\.ci_raw$/ && (%{$_->{prefix}})[1] eq "Donald Duck" }
165 @{$query->{query}->{bool}->{must}[0]->{bool}->{should}};
166 ok( $all_matches, "Correct multiple prefix query" );
168 is( $query->{query}->{bool}->{must}[0]->{prefix}->{$koha_to_index_name->{$koha_name} . ".ci_raw"}, "Donald Duck" );
173 my $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'HeadingAsc' );
178 'heading__sort' => 'asc'
181 "ascending sort parameter properly formed"
183 $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'HeadingDsc' );
188 'heading__sort' => 'desc'
191 "descending sort parameter properly formed"
195 $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
197 $query->{query}->{bool}->{filter},
198 { term => { 'authtype.raw' => 'AUTH_TYPE' } },
199 "authorities type code is used as filter"
202 # Authorities marclist check
204 $query = $qb->build_authorities_query_compat( [ 'tomas','mainentry' ], undef, undef, ['contains'], [$search_term,$search_term], 'AUTH_TYPE', 'asc' )
206 qr/Unknown search field tomas/,
207 "Warning for unknown field in marclist";
209 $query->{query}->{bool}->{must}[0]->{query_string}->{default_field},
211 "If no mapping for marclist the index is passed through as defined"
214 $query->{query}->{bool}->{must}[1]->{query_string}{default_field},
216 "If mapping found for marclist the index is passed through converted"
221 subtest 'build_query tests' => sub {
227 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
228 'Creating new query builder object for biblios'
231 my @sort_by = 'title_asc';
232 my @sort_params = $qb->_convert_sort_fields(@sort_by);
234 $options{sort} = \@sort_params;
235 my $query = $qb->build_query('test', %options);
246 "sort parameter properly formed"
249 t::lib::Mocks::mock_preference('FacetMaxCount','37');
250 t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
251 $query = $qb->build_query('test', %options);
252 ok( defined $query->{aggregations}{ccode}{terms}{size},'we need to ask for a size or we get only 5 facet' );
253 is( $query->{aggregations}{ccode}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount');
254 is( $query->{aggregations}{homebranch}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount for homebranch');
255 is( $query->{aggregations}{holdingbranch}{terms}{size}, 37,'we ask for the size as defined by the syspref FacetMaxCount for holdingbranch');
257 t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
258 $query = $qb->build_query();
259 ok( defined $query->{aggregations}{homebranch},
260 'homebranch added to facets if DisplayLibraryFacets=both' );
261 ok( defined $query->{aggregations}{holdingbranch},
262 'holdingbranch added to facets if DisplayLibraryFacets=both' );
263 t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
264 $query = $qb->build_query();
265 ok( !defined $query->{aggregations}{homebranch},
266 'homebranch not added to facets if DisplayLibraryFacets=holding' );
267 ok( defined $query->{aggregations}{holdingbranch},
268 'holdingbranch added to facets if DisplayLibraryFacets=holding' );
269 t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
270 $query = $qb->build_query();
271 ok( defined $query->{aggregations}{homebranch},
272 'homebranch added to facets if DisplayLibraryFacets=home' );
273 ok( !defined $query->{aggregations}{holdingbranch},
274 'holdingbranch not added to facets if DisplayLibraryFacets=home' );
276 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
278 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
280 $query->{query}{query_string}{query},
282 "query not altered if QueryAutoTruncate disabled"
285 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
287 $query->{query}{query_string}{query},
289 "keyword as phrase correctly quotes search term and strips index"
292 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
294 $query->{query}{query_string}{query},
295 '(title:(donald duck))',
296 'multiple words in a query term are enclosed in parenthesis'
299 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
301 $query->{query}{query_string}{query},
302 '(title:(donald duck)) AND (author:disney)',
303 'multiple query terms are enclosed in parenthesis while a single one is not'
306 my ($simple_query, $query_cgi, $query_desc);
307 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['"donald duck"', 'walt disney'], ['ti', 'au'] );
308 is($query_cgi, 'idx=ti&q=%22donald%20duck%22&idx=au&q=walt%20disney', 'query cgi ok for multiterm query');
309 is($query_desc, '(title:("donald duck")) (author:(walt disney))', 'query desc ok for multiterm query');
311 ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
313 $query->{query}{query_string}{query},
314 '(date-of-publication:2019)',
315 'Year in an st-year search is handled properly'
318 ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
320 $query->{query}{query_string}{query},
321 '(date-of-publication:[2018 TO 2019])',
322 'Year range in an st-year search is handled properly'
325 ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
327 $query->{query}{query_string}{query},
328 '(date-of-publication:[* TO 2019])',
329 'Open start year in year range of an st-year search is handled properly'
332 ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
334 $query->{query}{query_string}{query},
335 '(date-of-publication:[2019 TO *])',
336 'Open end year in year range of an st-year search is handled properly'
339 ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
341 $query->{query}{query_string}{query},
342 '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
343 'Open end year in year range of an st-year search is handled properly'
346 ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'],
347 ['yr,st-numeric:-2019','yr,st-numeric:2005','yr,st-numeric:1984-2022'] );
349 $query->{query}{query_string}{query},
350 '(date-of-publication:[2019 TO *]) AND (date-of-publication:[* TO 2019]) AND (date-of-publication:2005) AND (date-of-publication:[1984 TO 2022])',
351 'Limit on year search is handled properly when colon used'
354 # Enable auto-truncation
355 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
357 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
359 $query->{query}{query_string}{query},
361 "simple query is auto truncated when QueryAutoTruncate enabled"
364 # Ensure reserved words are not truncated
365 ( undef, $query ) = $qb->build_query_compat( undef,
366 ['donald or duck and mickey not mouse'] );
368 $query->{query}{query_string}{query},
369 "(donald* or duck* and mickey* not mouse*)",
370 "reserved words are not affected by QueryAutoTruncate"
373 ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
375 $query->{query}{query_string}{query},
377 "query with '*' is unaltered when QueryAutoTruncate is enabled"
380 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
382 $query->{query}{query_string}{query},
383 "(donald* duck* and the* mouse*)",
384 "individual words are all truncated and stopwords ignored"
387 ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
389 $query->{query}{query_string}{query},
391 "query of just '*' is unaltered when QueryAutoTruncate is enabled"
394 ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
396 $query->{query}{query_string}{query},
397 '("donald duck") AND available:true',
398 "query with quotes is unaltered when QueryAutoTruncate is enabled"
402 ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
404 $query->{query}{query_string}{query},
405 '("donald duck" and "the mouse")',
406 "all quoted strings are unaltered if more than one in query"
409 ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
411 $query->{query}{query_string}{query},
413 "query of specific field is truncated"
416 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:"123456"'] );
418 $query->{query}{query_string}{query},
419 '(local-number:"123456")',
420 "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
423 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
425 $query->{query}{query_string}{query},
426 '(local-number:123456*)',
427 "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
430 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:123456'] );
432 $query->{query}{query_string}{query},
433 '(local-number.raw:123456*)',
434 "query of specific field including period and not quoted is truncated, field name is converted to lower case"
437 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:"123456"'] );
439 $query->{query}{query_string}{query},
440 '(local-number.raw:"123456")',
441 "query of specific field including period and quoted is not truncated, field name is converted to lower case"
444 ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
446 $query->{query}{query_string}{query},
448 "query including period is truncated but not split at periods"
451 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
453 $query->{query}{query_string}{query},
454 '(title:"donald duck")',
455 "query of specific field is not truncated when surrounded by quotes"
458 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
460 $query->{query}{query_string}{query},
461 '(title:(donald* duck*))',
462 'words of a multi-word term are properly truncated'
465 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
467 $query->{query}{query_string}{query},
468 '(title:(donald* duck*)) AND (author:disney*)',
469 'words of a multi-word term and single-word term are properly truncated'
472 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
474 $query->{query}{query_string}{query},
475 '(title:"donald duck") AND suppress:false',
476 "query of specific field is added AND suppress:false"
479 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
481 $query->{query}{query_string}{query},
482 '(title:"donald duck")',
483 "query of specific field is not added AND suppress:0"
486 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
488 $query->{query}{query_string}{query},
489 '(title:"donald duck") AND author:("Dillinger Escaplan")',
490 "Simple query with limit term quoted in parentheses"
493 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
495 $query->{query}{query_string}{query},
496 '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
497 "Simple query with each limit's term quoted in parentheses"
499 is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
500 is($query_desc, 'title:"donald duck"', 'query desc ok');
502 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
504 $query->{query}{query_string}{query},
505 '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
506 "Limits quoted correctly when passed as phrase"
509 ( undef, $query ) = $qb->build_query_compat( ['OR'], ['title:"donald duck"', 'author:"Dillinger Escaplan"'], undef, ['itype:BOOK'] );
511 $query->{query}{query_string}{query},
512 '((title:"donald duck") OR (author:"Dillinger Escaplan")) AND itype:("BOOK")',
513 "OR query with limit"
516 ( undef, $query ) = $qb->build_query_compat( undef, undef, undef, ['itype:BOOK'] );
518 $query->{query}{query_string}{query},
524 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
526 $query->{query}{query_string}{query},
528 "scan query is properly formed"
531 $query->{aggregations}{'author'}{'terms'},
533 field => 'author__facet',
534 order => { '_key' => 'asc' },
535 include => '[nN][eE][wW].*'
537 "scan aggregation request is properly formed"
539 is($query_cgi, 'idx=au&q=new&scan=1', 'query cgi');
540 is($query_desc, 'new', 'query desc ok');
542 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], [], undef, undef, 1 );
544 $query->{query}{query_string}{query},
546 "scan query is properly formed"
549 $query->{aggregations}{'subject'}{'terms'},
551 field => 'subject__facet',
552 order => { '_key' => 'asc' },
553 include => '[nN][eE][wW].*'
555 "scan aggregation request is properly formed"
557 is($query_cgi, 'idx=&q=new&scan=1', 'query cgi');
558 is($query_desc, 'new', 'query desc ok');
560 my( $limit, $limit_cgi, $limit_desc );
561 ( undef, $query, $simple_query, $query_cgi, $query_desc, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( ['AND'], ['kw:""'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
562 is( $limit, '(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))', "Limit formed correctly when no search terms");
563 is( $limit_cgi,'&limit=author%3ADillinger%20Escaplan&limit=mc-itype%2Cphr%3ABOOK&limit=mc-itype%2Cphr%3ACD', "Limit CGI formed correctly when no search terms");
564 is( $limit_desc,'(author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',"Limit desc formed correctly when no search terms");
568 subtest 'build query from form subtests' => sub {
571 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
572 #when searching for authorities from a record the form returns marclist with blanks for unentered terms
573 my @marclist = ('mainmainentry','mainentry','match', 'all');
574 my @values = ( undef, 'Hamilton', undef, undef);
575 my @operator = ( 'contains', 'contains', 'contains', 'contains');
577 my $query = $qb->build_authorities_query_compat( \@marclist, undef,
578 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
579 is($query->{query}->{bool}->{must}[0]->{query_string}->{query}, "Hamilton*","Expected search is populated");
580 is( scalar @{ $query->{query}->{bool}->{must} }, 1,"Only defined search is populated");
582 @values[2] = 'Jefferson';
583 $query = $qb->build_authorities_query_compat( \@marclist, undef,
584 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
585 is($query->{query}->{bool}->{must}[0]->{query_string}->{query}, "Hamilton*","First index searched as expected");
586 is($query->{query}->{bool}->{must}[1]->{query_string}->{query}, "Jefferson*","Second index searched when populated");
587 is( scalar @{ $query->{query}->{bool}->{must} }, 2,"Only defined searches are populated");
592 subtest 'build_query with weighted fields tests' => sub {
595 $se->mock( '_load_elasticsearch_mappings', sub {
605 marc_type => 'marc21',
609 label => 'headingmain',
615 marc_type => 'marc21',
627 marc_type => 'marc21',
636 marc_field => '952d',
637 marc_type => 'marc21',
640 marc_field => '9955',
641 marc_type => 'marc21',
652 marc_type => 'marc21'
661 marc_field => '600a',
662 marc_type => 'marc21'
669 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'biblios' } );
670 Koha::SearchFields->search({})->delete;
671 Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings();
674 $search_field = Koha::SearchFields->find({ name => 'title' });
675 $search_field->update({ weight => 25.0 });
676 $search_field = Koha::SearchFields->find({ name => 'subject' });
677 $search_field->update({ weight => 15.5 });
678 Koha::SearchEngine::Elasticsearch->clear_search_fields_cache();
680 my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
681 undef, undef, undef, { weighted_fields => 1 });
683 my $fields = $query->{query}{query_string}{fields};
685 is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
687 my @found = grep { $_ eq 'title^25.00' } @{$fields};
688 is(@found, 1, 'Search field title has correct weight');
690 @found = grep { $_ eq 'subject^15.50' } @{$fields};
691 is(@found, 1, 'Search field subject has correct weight');
693 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
694 undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
696 $fields = $query->{query}{query_string}{fields};
701 'Only OPAC search fields are used when opac search is performed'
704 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'authorities' } );
705 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
706 undef, undef, undef, { weighted_fields => 1 });
707 $fields = $query->{query}{query_string}{fields};
708 is_deeply( [sort @$fields], ['heading','headingmain'],'Authorities fields retrieve for authorities index');
710 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
711 undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
712 $fields = $query->{query}{query_string}{fields};
713 is_deeply($fields,['headingmain'],'Only opac authorities fields retrieved for authorities index is is_opac');
717 subtest 'build_query_compat() SearchLimitLibrary tests' => sub {
721 $schema->storage->txn_begin;
723 my $builder = t::lib::TestBuilder->new;
725 my $branch_1 = $builder->build_object({ class => 'Koha::Libraries' });
726 my $branch_2 = $builder->build_object({ class => 'Koha::Libraries' });
727 my $group = $builder->build_object({ class => 'Koha::Library::Groups', value => {
728 ft_search_groups_opac => 1,
729 ft_search_groups_staff => 1,
734 my $group_1 = $builder->build_object({ class => 'Koha::Library::Groups', value => {
735 parent_id => $group->id,
736 branchcode => $branch_1->id
739 my $group_2 = $builder->build_object({ class => 'Koha::Library::Groups', value => {
740 parent_id => $group->id,
741 branchcode => $branch_2->id
744 my $groupid = $group->id;
745 my @branchcodes = sort { $a cmp $b } ( $branch_1->id, $branch_2->id );
748 my $query_builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
749 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'both');
750 my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
751 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
752 is( $limit, '(homebranch: "CPL" OR holdingbranch: "CPL")', "Branch limit expanded to home/holding branch");
753 is( $limit_desc, '(homebranch: "CPL" OR holdingbranch: "CPL")', "Limit description correctly expanded");
754 is( $limit_cgi, '&limit=branch%3ACPL', "Limit cgi does not get expanded");
755 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
756 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
757 is( $limit, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\" OR holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "Multibranch limit expanded to home/holding branches");
758 is( $limit_desc, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\" OR holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "Multibranch limit description correctly expanded");
759 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Multibranch limit cgi does not get expanded");
761 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'homebranch');
762 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
763 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
764 is( $limit, "(homebranch: \"CPL\")", "branch limit expanded to home branch");
765 is( $limit_desc, "(homebranch: \"CPL\")", "limit description correctly expanded");
766 is( $limit_cgi, "&limit=branch%3ACPL", "limit cgi does not get expanded");
767 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
768 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
769 is( $limit, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\")", "branch limit expanded to home branch");
770 is( $limit_desc, "(homebranch: \"$branchcodes[0]\" OR homebranch: \"$branchcodes[1]\")", "limit description correctly expanded");
771 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
773 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'holdingbranch');
774 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
775 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
776 is( $limit, "(holdingbranch: \"CPL\")", "branch limit expanded to holding branch");
777 is( $limit_desc, "(holdingbranch: \"CPL\")", "Limit description correctly expanded");
778 is( $limit_cgi, "&limit=branch%3ACPL", "Limit cgi does not get expanded");
779 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
780 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
781 is( $limit, "(holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "branch limit expanded to holding branch");
782 is( $limit_desc, "(holdingbranch: \"$branchcodes[0]\" OR holdingbranch: \"$branchcodes[1]\")", "Limit description correctly expanded");
783 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
787 subtest "Handle search filters" => sub {
793 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
794 'Creating new query builder object for biblios'
797 my $filter = Koha::SearchFilter->new({
799 query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
800 limits => q|{"limits":["mc-itype,phr:BK","mc-itype,phr:MU","available"]}|,
802 my $filter_id = $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"] );
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");
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");
810 $filter = Koha::SearchFilter->new({
812 query => q|{"operands":["su:biography"],"indexes":[],"operators":[]}|,
813 limits => q|{"limits":[]}|,
815 $filter_id = $filter->id;
817 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
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");
825 subtest "_convert_sort_fields() tests" => sub {
831 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
832 'Creating new query builder object for biblios'
835 my @sort_by = $qb->_convert_sort_fields(qw( call_number_asc author_dsc ));
839 { field => 'cn-sort', direction => 'asc' },
840 { field => 'author', direction => 'desc' }
842 'sort fields should have been split correctly'
845 # We could expect this to pass, but direction is undef instead of 'desc'
846 @sort_by = $qb->_convert_sort_fields(qw( call_number_asc author_desc ));
850 { field => 'cn-sort', direction => 'asc' },
851 { field => 'author', direction => 'desc' }
853 'sort fields should have been split correctly'
857 subtest "_sort_field() tests" => sub {
863 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
864 'Creating new query builder object for biblios'
867 my $f = $qb->_sort_field('title');
871 'title sort mapped correctly'
874 $f = $qb->_sort_field('subject');
878 'subject sort mapped correctly'
881 $f = $qb->_sort_field('itemnumber');
885 'itemnumber sort mapped correctly'
888 $f = $qb->_sort_field('sortablenumber');
891 'sortablenumber__sort',
892 'sortablenumber sort mapped correctly'
896 $schema->storage->txn_rollback;