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>.
23 use t::lib::TestBuilder;
24 use Test::More tests => 6;
27 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
32 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
33 $se->mock( 'get_elasticsearch_mappings', sub {
56 sortablenumber__sort => {
68 $all_mappings{$self->index} = $mappings;
79 $self->sort_fields($sort_fields->{$self->index});
81 return $all_mappings{$self->index};
84 subtest 'build_authorities_query_compat() tests' => sub {
90 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
91 'Creating new query builder object for authorities'
94 my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
95 my $search_term = 'a';
96 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
97 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
98 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
99 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
102 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
107 $search_term = 'Donald Duck';
108 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
109 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
110 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
111 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
112 "(Donald*) AND (Duck*)");
114 is( $query->{query}->{bool}->{must}[0]->{query_string}->{query},
115 "(Donald*) AND (Duck*)");
119 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
120 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['is'], [$search_term], 'AUTH_TYPE', 'asc' );
121 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
122 is( $query->{query}->{bool}->{must}[0]->{match_phrase}->{"_all.phrase"},
125 is( $query->{query}->{bool}->{must}[0]->{match_phrase}->{$koha_to_index_name->{$koha_name}.".phrase"},
130 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
131 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'asc' );
132 if ( $koha_name eq 'all' || $koha_name eq 'any' ) {
133 is( $query->{query}->{bool}->{must}[0]->{match_phrase_prefix}->{"_all.phrase"},
136 is( $query->{query}->{bool}->{must}[0]->{match_phrase_prefix}->{$koha_to_index_name->{$koha_name}.".phrase"},
142 my $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'HeadingAsc' );
147 'heading__sort' => 'asc'
150 "ascending sort parameter properly formed"
152 $query = $qb->build_authorities_query_compat( [ 'mainentry' ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'HeadingDsc' );
157 'heading__sort' => 'desc'
160 "descending sort parameter properly formed"
165 $qb->build_authorities_query_compat( [ 'tomas' ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
167 'Koha::Exceptions::WrongParameter',
168 'Exception thrown on invalid value in the marclist param';
171 subtest 'build_query tests' => sub {
177 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
178 'Creating new query builder object for biblios'
181 my @sort_by = 'title_asc';
182 my @sort_params = $qb->_convert_sort_fields(@sort_by);
184 $options{sort} = \@sort_params;
185 my $query = $qb->build_query('test', %options);
191 'title__sort.phrase' => {
196 "sort parameter properly formed"
199 t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
200 $query = $qb->build_query();
201 ok( defined $query->{aggregations}{homebranch},
202 'homebranch added to facets if DisplayLibraryFacets=both' );
203 ok( defined $query->{aggregations}{holdingbranch},
204 'holdingbranch added to facets if DisplayLibraryFacets=both' );
205 t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
206 $query = $qb->build_query();
207 ok( !defined $query->{aggregations}{homebranch},
208 'homebranch not added to facets if DisplayLibraryFacets=holding' );
209 ok( defined $query->{aggregations}{holdingbranch},
210 'holdingbranch added to facets if DisplayLibraryFacets=holding' );
211 t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
212 $query = $qb->build_query();
213 ok( defined $query->{aggregations}{homebranch},
214 'homebranch added to facets if DisplayLibraryFacets=home' );
215 ok( !defined $query->{aggregations}{holdingbranch},
216 'holdingbranch not added to facets if DisplayLibraryFacets=home' );
218 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
220 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
222 $query->{query}{query_string}{query},
224 "query not altered if QueryAutoTruncate disabled"
227 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
229 $query->{query}{query_string}{query},
230 '(title:(donald duck))',
231 'multiple words in a query term are enclosed in parenthesis'
234 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
236 $query->{query}{query_string}{query},
237 '(title:(donald duck)) AND (author:disney)',
238 'multiple query terms are enclosed in parenthesis while a single one is not'
241 my ($simple_query, $query_cgi, $query_desc);
242 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['"donald duck"', 'walt disney'], ['ti', 'au'] );
243 is($query_cgi, 'idx=ti&q=%22donald%20duck%22&idx=au&q=walt%20disney', 'query cgi ok for multiterm query');
244 is($query_desc, '(title:("donald duck")) (author:(walt disney))', 'query desc ok for multiterm query');
246 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
248 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
250 $query->{query}{query_string}{query},
252 "simple query is auto truncated when QueryAutoTruncate enabled"
255 # Ensure reserved words are not truncated
256 ( undef, $query ) = $qb->build_query_compat( undef,
257 ['donald or duck and mickey not mouse'] );
259 $query->{query}{query_string}{query},
260 "(donald* or duck* and mickey* not mouse*)",
261 "reserved words are not affected by QueryAutoTruncate"
264 ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
266 $query->{query}{query_string}{query},
268 "query with '*' is unaltered when QueryAutoTruncate is enabled"
271 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
273 $query->{query}{query_string}{query},
274 "(donald* duck* and the* mouse*)",
275 "individual words are all truncated and stopwords ignored"
278 ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
280 $query->{query}{query_string}{query},
282 "query of just '*' is unaltered when QueryAutoTruncate is enabled"
285 ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'] );
287 $query->{query}{query_string}{query},
289 "query with quotes is unaltered when QueryAutoTruncate is enabled"
293 ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
295 $query->{query}{query_string}{query},
296 '("donald duck" and "the mouse")',
297 "all quoted strings are unaltered if more than one in query"
300 ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
302 $query->{query}{query_string}{query},
304 "query of specific field is truncated"
307 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:"123456"'] );
309 $query->{query}{query_string}{query},
310 '(local-number:"123456")',
311 "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
314 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
316 $query->{query}{query_string}{query},
317 '(local-number:123456*)',
318 "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
321 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:123456'] );
323 $query->{query}{query_string}{query},
324 '(local-number.raw:123456*)',
325 "query of specific field including period and not quoted is truncated, field name is converted to lower case"
328 ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:"123456"'] );
330 $query->{query}{query_string}{query},
331 '(local-number.raw:"123456")',
332 "query of specific field including period and quoted is not truncated, field name is converted to lower case"
335 ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
337 $query->{query}{query_string}{query},
339 "query including period is truncated but not split at periods"
342 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
344 $query->{query}{query_string}{query},
345 '(title:"donald duck")',
346 "query of specific field is not truncated when surrounded by quotes"
349 ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
351 $query->{query}{query_string}{query},
352 '(title:(donald* duck*))',
353 'words of a multi-word term are properly truncated'
356 ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
358 $query->{query}{query_string}{query},
359 '(title:(donald* duck*)) AND (author:disney*)',
360 'words of a multi-word term and single-word term are properly truncated'
363 ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
365 $query->{query}{query_string}{query},
366 '(title:"donald duck") AND suppress:0',
367 "query of specific field is added AND suppress:0"
370 ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
372 $query->{query}{query_string}{query},
373 '(title:"donald duck")',
374 "query of specific field is not added AND suppress:0"
376 is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
377 is($query_desc, 'title:"donald duck"', 'query desc ok');
381 subtest 'build query from form subtests' => sub {
384 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
385 #when searching for authorities from a record the form returns marclist with blanks for unentered terms
386 my @marclist = ('mainmainentry','mainentry','match', 'all');
387 my @values = ( undef, 'Hamilton', undef, undef);
388 my @operator = ( 'contains', 'contains', 'contains', 'contains');
390 my $query = $qb->build_authorities_query_compat( \@marclist, undef,
391 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
392 is($query->{query}->{bool}->{must}[0]->{query_string}->{query}, "Hamilton*","Expected search is populated");
393 is( scalar @{ $query->{query}->{bool}->{must} }, 1,"Only defined search is populated");
395 @values[2] = 'Jefferson';
396 $query = $qb->build_authorities_query_compat( \@marclist, undef,
397 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
398 is($query->{query}->{bool}->{must}[0]->{query_string}->{query}, "Hamilton*","First index searched as expected");
399 is($query->{query}->{bool}->{must}[1]->{query_string}->{query}, "Jefferson*","Second index searched when populated");
400 is( scalar @{ $query->{query}->{bool}->{must} }, 2,"Only defined searches are populated");
405 subtest 'build_query with weighted fields tests' => sub {
408 my $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
409 my $db_builder = t::lib::TestBuilder->new();
411 Koha::SearchFields->search({})->delete;
414 source => 'SearchField',
423 source => 'SearchField',
432 source => 'SearchField',
440 my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
441 undef, undef, undef, { weighted_fields => 1 });
443 my $fields = $query->{query}{query_string}{fields};
444 is(scalar(@$fields), 3, 'Search is done on 3 fields');
445 is($fields->[0], '_all', 'First search field is _all');
446 is($fields->[1], 'title^25.00', 'Second search field is title');
447 is($fields->[2], 'subject^15.00', 'Third search field is subject');
450 subtest "_convert_sort_fields() tests" => sub {
456 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
457 'Creating new query builder object for biblios'
460 my @sort_by = $qb->_convert_sort_fields(qw( call_number_asc author_dsc ));
464 { field => 'local-classification', direction => 'asc' },
465 { field => 'author', direction => 'desc' }
467 'sort fields should have been split correctly'
470 # We could expect this to pass, but direction is undef instead of 'desc'
471 @sort_by = $qb->_convert_sort_fields(qw( call_number_asc author_desc ));
475 { field => 'local-classification', direction => 'asc' },
476 { field => 'author', direction => 'desc' }
478 'sort fields should have been split correctly'
482 subtest "_sort_field() tests" => sub {
488 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }),
489 'Creating new query builder object for biblios'
492 my $f = $qb->_sort_field('title');
495 'title__sort.phrase',
496 'title sort mapped correctly'
499 $f = $qb->_sort_field('subject');
503 'subject sort mapped correctly'
506 $f = $qb->_sort_field('itemnumber');
510 'itemnumber sort mapped correctly'
513 $f = $qb->_sort_field('sortablenumber');
516 'sortablenumber__sort',
517 'sortablenumber sort mapped correctly'
521 $schema->storage->txn_rollback;