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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (+3 lines)
Lines 306-311 sub build_query_compat { Link Here
306
        # them into a structured ES query itself. Maybe later, though that'd be
306
        # them into a structured ES query itself. Maybe later, though that'd be
307
        # more robust.
307
        # more robust.
308
        $search_param_query_str = join( ' ', $self->_create_query_string(@search_params) );
308
        $search_param_query_str = join( ' ', $self->_create_query_string(@search_params) );
309
        if ($search_param_query_str) {
310
            $search_param_query_str = "($search_param_query_str)";
311
        }
309
        $query_str = join( ' AND ',
312
        $query_str = join( ' AND ',
310
            $search_param_query_str || (),
313
            $search_param_query_str || (),
311
            $self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
314
            $self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-33 / +46 lines)
Lines 217-223 subtest 'build_authorities_query_compat() tests' => sub { Link Here
217
};
217
};
218
218
219
subtest 'build_query tests' => sub {
219
subtest 'build_query tests' => sub {
220
    plan tests => 57;
220
    plan tests => 59;
221
221
222
    my $qb;
222
    my $qb;
223
223
Lines 276-343 subtest 'build_query tests' => sub { Link Here
276
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
276
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
277
    is(
277
    is(
278
        $query->{query}{query_string}{query},
278
        $query->{query}{query_string}{query},
279
        "(donald duck)",
279
        "((donald duck))",
280
        "query not altered if QueryAutoTruncate disabled"
280
        "query not altered if QueryAutoTruncate disabled"
281
    );
281
    );
282
282
283
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
283
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
284
    is(
284
    is(
285
        $query->{query}{query_string}{query},
285
        $query->{query}{query_string}{query},
286
        '("donald duck")',
286
        '(("donald duck"))',
287
        "keyword as phrase correctly quotes search term and strips index"
287
        "keyword as phrase correctly quotes search term and strips index"
288
    );
288
    );
289
289
290
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
290
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
291
    is(
291
    is(
292
        $query->{query}{query_string}{query},
292
        $query->{query}{query_string}{query},
293
        '(title:(donald duck))',
293
        '((title:(donald duck)))',
294
        'multiple words in a query term are enclosed in parenthesis'
294
        'multiple words in a query term are enclosed in parenthesis'
295
    );
295
    );
296
296
297
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
297
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
298
    is(
298
    is(
299
        $query->{query}{query_string}{query},
299
        $query->{query}{query_string}{query},
300
        '(title:(donald duck)) AND (author:disney)',
300
        '((title:(donald duck)) AND (author:disney))',
301
        'multiple query terms are enclosed in parenthesis while a single one is not'
301
        'multiple query terms are enclosed in parenthesis while a single one is not'
302
    );
302
    );
303
303
304
    my ($simple_query, $query_cgi, $query_desc);
304
    my ($simple_query, $query_cgi, $query_desc);
305
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['"donald duck"', 'walt disney'], ['ti', 'au'] );
305
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['"donald duck"', 'walt disney'], ['ti', 'au'] );
306
    is($query_cgi, 'idx=ti&q=%22donald%20duck%22&idx=au&q=walt%20disney', 'query cgi ok for multiterm query');
306
    is($query_cgi, 'idx=ti&q=%22donald%20duck%22&idx=au&q=walt%20disney', 'query cgi ok for multiterm query');
307
    is($query_desc, '(title:("donald duck")) (author:(walt disney))', 'query desc ok for multiterm query');
307
    is($query_desc, '((title:("donald duck")) (author:(walt disney)))', 'query desc ok for multiterm query');
308
308
309
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
309
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
310
    is(
310
    is(
311
        $query->{query}{query_string}{query},
311
        $query->{query}{query_string}{query},
312
        '(date-of-publication:2019)',
312
        '((date-of-publication:2019))',
313
        'Year in an st-year search is handled properly'
313
        'Year in an st-year search is handled properly'
314
    );
314
    );
315
315
316
    ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
316
    ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
317
    is(
317
    is(
318
        $query->{query}{query_string}{query},
318
        $query->{query}{query_string}{query},
319
        '(date-of-publication:[2018 TO 2019])',
319
        '((date-of-publication:[2018 TO 2019]))',
320
        'Year range in an st-year search is handled properly'
320
        'Year range in an st-year search is handled properly'
321
    );
321
    );
322
322
323
    ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
323
    ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
324
    is(
324
    is(
325
        $query->{query}{query_string}{query},
325
        $query->{query}{query_string}{query},
326
        '(date-of-publication:[* TO 2019])',
326
        '((date-of-publication:[* TO 2019]))',
327
        'Open start year in year range of an st-year search is handled properly'
327
        'Open start year in year range of an st-year search is handled properly'
328
    );
328
    );
329
329
330
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
330
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
331
    is(
331
    is(
332
        $query->{query}{query_string}{query},
332
        $query->{query}{query_string}{query},
333
        '(date-of-publication:[2019 TO *])',
333
        '((date-of-publication:[2019 TO *]))',
334
        'Open end year in year range of an st-year search is handled properly'
334
        'Open end year in year range of an st-year search is handled properly'
335
    );
335
    );
336
336
337
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
337
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
338
    is(
338
    is(
339
        $query->{query}{query_string}{query},
339
        $query->{query}{query_string}{query},
340
        '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
340
        '((date-of-publication:[2019 TO *])) AND date-of-publication:[* TO 2019]',
341
        'Open end year in year range of an st-year search is handled properly'
341
        'Open end year in year range of an st-year search is handled properly'
342
    );
342
    );
343
343
Lines 347-353 subtest 'build_query tests' => sub { Link Here
347
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
347
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
348
    is(
348
    is(
349
        $query->{query}{query_string}{query},
349
        $query->{query}{query_string}{query},
350
        "(donald* duck*)",
350
        "((donald* duck*))",
351
        "simple query is auto truncated when QueryAutoTruncate enabled"
351
        "simple query is auto truncated when QueryAutoTruncate enabled"
352
    );
352
    );
353
353
Lines 356-390 subtest 'build_query tests' => sub { Link Here
356
        ['donald or duck and mickey not mouse'] );
356
        ['donald or duck and mickey not mouse'] );
357
    is(
357
    is(
358
        $query->{query}{query_string}{query},
358
        $query->{query}{query_string}{query},
359
        "(donald* or duck* and mickey* not mouse*)",
359
        "((donald* or duck* and mickey* not mouse*))",
360
        "reserved words are not affected by QueryAutoTruncate"
360
        "reserved words are not affected by QueryAutoTruncate"
361
    );
361
    );
362
362
363
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
363
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
364
    is(
364
    is(
365
        $query->{query}{query_string}{query},
365
        $query->{query}{query_string}{query},
366
        "(donald* duck*)",
366
        "((donald* duck*))",
367
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
367
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
368
    );
368
    );
369
369
370
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
370
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
371
    is(
371
    is(
372
        $query->{query}{query_string}{query},
372
        $query->{query}{query_string}{query},
373
        "(donald* duck* and the* mouse*)",
373
        "((donald* duck* and the* mouse*))",
374
        "individual words are all truncated and stopwords ignored"
374
        "individual words are all truncated and stopwords ignored"
375
    );
375
    );
376
376
377
    ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
377
    ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
378
    is(
378
    is(
379
        $query->{query}{query_string}{query},
379
        $query->{query}{query_string}{query},
380
        "(*)",
380
        "((*))",
381
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
381
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
382
    );
382
    );
383
383
384
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
384
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
385
    is(
385
    is(
386
        $query->{query}{query_string}{query},
386
        $query->{query}{query_string}{query},
387
        '("donald duck") AND onloan:false',
387
        '(("donald duck")) AND onloan:false',
388
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
388
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
389
    );
389
    );
390
390
Lines 392-489 subtest 'build_query tests' => sub { Link Here
392
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
392
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
393
    is(
393
    is(
394
        $query->{query}{query_string}{query},
394
        $query->{query}{query_string}{query},
395
        '("donald duck" and "the mouse")',
395
        '(("donald duck" and "the mouse"))',
396
        "all quoted strings are unaltered if more than one in query"
396
        "all quoted strings are unaltered if more than one in query"
397
    );
397
    );
398
398
399
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
399
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
400
    is(
400
    is(
401
        $query->{query}{query_string}{query},
401
        $query->{query}{query_string}{query},
402
        '(barcode:123456*)',
402
        '((barcode:123456*))',
403
        "query of specific field is truncated"
403
        "query of specific field is truncated"
404
    );
404
    );
405
405
406
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:"123456"'] );
406
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:"123456"'] );
407
    is(
407
    is(
408
        $query->{query}{query_string}{query},
408
        $query->{query}{query_string}{query},
409
        '(local-number:"123456")',
409
        '((local-number:"123456"))',
410
        "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
410
        "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
411
    );
411
    );
412
412
413
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
413
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
414
    is(
414
    is(
415
        $query->{query}{query_string}{query},
415
        $query->{query}{query_string}{query},
416
        '(local-number:123456*)',
416
        '((local-number:123456*))',
417
        "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
417
        "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
418
    );
418
    );
419
419
420
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:123456'] );
420
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:123456'] );
421
    is(
421
    is(
422
        $query->{query}{query_string}{query},
422
        $query->{query}{query_string}{query},
423
        '(local-number.raw:123456*)',
423
        '((local-number.raw:123456*))',
424
        "query of specific field including period and not quoted is truncated, field name is converted to lower case"
424
        "query of specific field including period and not quoted is truncated, field name is converted to lower case"
425
    );
425
    );
426
426
427
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:"123456"'] );
427
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:"123456"'] );
428
    is(
428
    is(
429
        $query->{query}{query_string}{query},
429
        $query->{query}{query_string}{query},
430
        '(local-number.raw:"123456")',
430
        '((local-number.raw:"123456"))',
431
        "query of specific field including period and quoted is not truncated, field name is converted to lower case"
431
        "query of specific field including period and quoted is not truncated, field name is converted to lower case"
432
    );
432
    );
433
433
434
    ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
434
    ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
435
    is(
435
    is(
436
        $query->{query}{query_string}{query},
436
        $query->{query}{query_string}{query},
437
        '(J.R.R*)',
437
        '((J.R.R*))',
438
        "query including period is truncated but not split at periods"
438
        "query including period is truncated but not split at periods"
439
    );
439
    );
440
440
441
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
441
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
442
    is(
442
    is(
443
        $query->{query}{query_string}{query},
443
        $query->{query}{query_string}{query},
444
        '(title:"donald duck")',
444
        '((title:"donald duck"))',
445
        "query of specific field is not truncated when surrounded by quotes"
445
        "query of specific field is not truncated when surrounded by quotes"
446
    );
446
    );
447
447
448
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
448
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
449
    is(
449
    is(
450
        $query->{query}{query_string}{query},
450
        $query->{query}{query_string}{query},
451
        '(title:(donald* duck*))',
451
        '((title:(donald* duck*)))',
452
        'words of a multi-word term are properly truncated'
452
        'words of a multi-word term are properly truncated'
453
    );
453
    );
454
454
455
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
455
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
456
    is(
456
    is(
457
        $query->{query}{query_string}{query},
457
        $query->{query}{query_string}{query},
458
        '(title:(donald* duck*)) AND (author:disney*)',
458
        '((title:(donald* duck*)) AND (author:disney*))',
459
        'words of a multi-word term and single-word term are properly truncated'
459
        'words of a multi-word term and single-word term are properly truncated'
460
    );
460
    );
461
461
462
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
462
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
463
    is(
463
    is(
464
        $query->{query}{query_string}{query},
464
        $query->{query}{query_string}{query},
465
        '(title:"donald duck") AND suppress:false',
465
        '((title:"donald duck")) AND suppress:false',
466
        "query of specific field is added AND suppress:false"
466
        "query of specific field is added AND suppress:false"
467
    );
467
    );
468
468
469
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
469
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
470
    is(
470
    is(
471
        $query->{query}{query_string}{query},
471
        $query->{query}{query_string}{query},
472
        '(title:"donald duck")',
472
        '((title:"donald duck"))',
473
        "query of specific field is not added AND suppress:0"
473
        "query of specific field is not added AND suppress:0"
474
    );
474
    );
475
475
476
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
476
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
477
    is(
477
    is(
478
        $query->{query}{query_string}{query},
478
        $query->{query}{query_string}{query},
479
        '(title:"donald duck") AND author:("Dillinger Escaplan")',
479
        '((title:"donald duck")) AND author:("Dillinger Escaplan")',
480
        "Simple query with limit term quoted in parentheses"
480
        "Simple query with limit term quoted in parentheses"
481
    );
481
    );
482
482
483
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
483
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
484
    is(
484
    is(
485
        $query->{query}{query_string}{query},
485
        $query->{query}{query_string}{query},
486
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
486
        '((title:"donald duck")) AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
487
        "Simple query with each limit's term quoted in parentheses"
487
        "Simple query with each limit's term quoted in parentheses"
488
    );
488
    );
489
    is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
489
    is($query_cgi, 'idx=&q=title%3A%22donald%20duck%22', 'query cgi');
Lines 492-501 subtest 'build_query tests' => sub { Link Here
492
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
492
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
493
    is(
493
    is(
494
        $query->{query}{query_string}{query},
494
        $query->{query}{query_string}{query},
495
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
495
        '((title:"donald duck")) AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
496
        "Limits quoted correctly when passed as phrase"
496
        "Limits quoted correctly when passed as phrase"
497
    );
497
    );
498
498
499
    ( undef, $query ) = $qb->build_query_compat( ['OR'], ['title:"donald duck"', 'author:"Dillinger Escaplan"'], undef, ['itype:BOOK'] );
500
    is(
501
        $query->{query}{query_string}{query},
502
        '((title:"donald duck") OR (author:"Dillinger Escaplan")) AND itype:("BOOK")',
503
        "OR query with limit"
504
    );
505
506
    ( undef, $query ) = $qb->build_query_compat( undef, undef, undef, ['itype:BOOK'] );
507
    is(
508
        $query->{query}{query_string}{query},
509
        'itype:("BOOK")',
510
        "Limit only"
511
    );
512
499
    # Scan queries
513
    # Scan queries
500
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
514
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['new'], ['au'], undef, undef, 1 );
501
    is(
515
    is(
502
- 

Return to bug 30152