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

(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-56 / +147 lines)
Lines 23-29 use Test::Warn; Link Here
23
use t::lib::Mocks;
23
use t::lib::Mocks;
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use Test::NoWarnings;
25
use Test::NoWarnings;
26
use Test::More tests => 9;
26
use Test::More tests => 10;
27
27
28
use List::Util qw( all );
28
use List::Util qw( all );
29
29
Lines 271-277 subtest 'build_query tests' => sub { Link Here
271
271
272
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
272
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
273
    is(
273
    is(
274
        $query->{query}{query_string}{query},
274
        $query->{query}{bool}{must}[0]{query_string}{query},
275
        "(donald duck)",
275
        "(donald duck)",
276
        "query not altered if QueryAutoTruncate disabled"
276
        "query not altered if QueryAutoTruncate disabled"
277
    );
277
    );
Lines 282-302 subtest 'build_query tests' => sub { Link Here
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}{bool}{must}[0]{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}{bool}{must}[0]{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}{bool}{must}[0]{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
    );
Lines 309-343 subtest 'build_query tests' => sub { Link Here
309
309
310
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
310
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
311
    is(
311
    is(
312
        $query->{query}{query_string}{query},
312
        $query->{query}{bool}{must}[0]{query_string}{query},
313
        '(date-of-publication:2019)',
313
        '(date-of-publication:2019)',
314
        'Year in an st-year search is handled properly'
314
        'Year in an st-year search is handled properly'
315
    );
315
    );
316
316
317
    ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
317
    ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
318
    is(
318
    is(
319
        $query->{query}{query_string}{query},
319
        $query->{query}{bool}{must}[0]{query_string}{query},
320
        '(date-of-publication:[2018 TO 2019])',
320
        '(date-of-publication:[2018 TO 2019])',
321
        'Year range in an st-year search is handled properly'
321
        'Year range in an st-year search is handled properly'
322
    );
322
    );
323
323
324
    ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
324
    ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
325
    is(
325
    is(
326
        $query->{query}{query_string}{query},
326
        $query->{query}{bool}{must}[0]{query_string}{query},
327
        '(date-of-publication:[* TO 2019])',
327
        '(date-of-publication:[* TO 2019])',
328
        'Open start year in year range of an st-year search is handled properly'
328
        'Open start year in year range of an st-year search is handled properly'
329
    );
329
    );
330
330
331
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
331
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
332
    is(
332
    is(
333
        $query->{query}{query_string}{query},
333
        $query->{query}{bool}{must}[0]{query_string}{query},
334
        '(date-of-publication:[2019 TO *])',
334
        '(date-of-publication:[2019 TO *])',
335
        'Open end year in year range of an st-year search is handled properly'
335
        'Open end year in year range of an st-year search is handled properly'
336
    );
336
    );
337
337
338
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
338
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
339
    is(
339
    is(
340
        $query->{query}{query_string}{query},
340
        $query->{query}{bool}{must}[0]{query_string}{query},
341
        '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
341
        '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
342
        'Open end year in year range of an st-year search is handled properly'
342
        'Open end year in year range of an st-year search is handled properly'
343
    );
343
    );
Lines 347-353 subtest 'build_query tests' => sub { Link Here
347
        [ 'yr,st-numeric:-2019', 'yr,st-numeric:2005', 'yr,st-numeric:1984-2022' ]
347
        [ 'yr,st-numeric:-2019', 'yr,st-numeric:2005', 'yr,st-numeric:1984-2022' ]
348
    );
348
    );
349
    is(
349
    is(
350
        $query->{query}{query_string}{query},
350
        $query->{query}{bool}{must}[0]{query_string}{query},
351
        '(date-of-publication:[2019 TO *]) AND (date-of-publication:[* TO 2019]) AND (date-of-publication:2005) AND (date-of-publication:[1984 TO 2022])',
351
        '(date-of-publication:[2019 TO *]) AND (date-of-publication:[* TO 2019]) AND (date-of-publication:2005) AND (date-of-publication:[1984 TO 2022])',
352
        'Limit on year search is handled properly when colon used'
352
        'Limit on year search is handled properly when colon used'
353
    );
353
    );
Lines 357-363 subtest 'build_query tests' => sub { Link Here
357
357
358
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
358
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
359
    is(
359
    is(
360
        $query->{query}{query_string}{query},
360
        $query->{query}{bool}{must}[0]{query_string}{query},
361
        "(donald* duck*)",
361
        "(donald* duck*)",
362
        "simple query is auto truncated when QueryAutoTruncate enabled"
362
        "simple query is auto truncated when QueryAutoTruncate enabled"
363
    );
363
    );
Lines 368-409 subtest 'build_query tests' => sub { Link Here
368
        ['donald or duck and mickey not mouse']
368
        ['donald or duck and mickey not mouse']
369
    );
369
    );
370
    is(
370
    is(
371
        $query->{query}{query_string}{query},
371
        $query->{query}{bool}{must}[0]{query_string}{query},
372
        "(donald* or duck* and mickey* not mouse*)",
372
        "(donald* or duck* and mickey* not mouse*)",
373
        "reserved words are not affected by QueryAutoTruncate"
373
        "reserved words are not affected by QueryAutoTruncate"
374
    );
374
    );
375
375
376
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
376
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
377
    is(
377
    is(
378
        $query->{query}{query_string}{query},
378
        $query->{query}{bool}{must}[0]{query_string}{query},
379
        "(donald* duck*)",
379
        "(donald* duck*)",
380
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
380
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
381
    );
381
    );
382
382
383
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
383
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
384
    is(
384
    is(
385
        $query->{query}{query_string}{query},
385
        $query->{query}{bool}{must}[0]{query_string}{query},
386
        "(donald* duck* and the* mouse*)",
386
        "(donald* duck* and the* mouse*)",
387
        "individual words are all truncated and stopwords ignored"
387
        "individual words are all truncated and stopwords ignored"
388
    );
388
    );
389
389
390
    ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
390
    ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
391
    is(
391
    is(
392
        $query->{query}{query_string}{query},
392
        $query->{query}{bool}{must}[0]{query_string}{query},
393
        "(*)",
393
        "(*)",
394
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
394
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
395
    );
395
    );
396
396
397
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
397
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
398
    is(
398
    is(
399
        $query->{query}{query_string}{query},
399
        $query->{query}{bool}{must}[0]{query_string}{query},
400
        '("donald duck") AND available:true',
400
        '("donald duck") AND available:true',
401
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
401
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
402
    );
402
    );
403
403
404
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
404
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
405
    is(
405
    is(
406
        $query->{query}{query_string}{query},
406
        $query->{query}{bool}{must}[0]{query_string}{query},
407
        '("donald duck" and "the mouse")',
407
        '("donald duck" and "the mouse")',
408
        "all quoted strings are unaltered if more than one in query"
408
        "all quoted strings are unaltered if more than one in query"
409
    );
409
    );
Lines 413-447 subtest 'build_query tests' => sub { Link Here
413
413
414
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
414
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
415
    is(
415
    is(
416
        $query->{query}{query_string}{query},
416
        $query->{query}{bool}{must}[0]{query_string}{query},
417
        '(barcode:123456*)',
417
        '(barcode:123456*)',
418
        "query of specific field is truncated"
418
        "query of specific field is truncated"
419
    );
419
    );
420
420
421
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:"donald"'] );
421
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:"donald"'] );
422
    is(
422
    is(
423
        $query->{query}{query_string}{query},
423
        $query->{query}{bool}{must}[0]{query_string}{query},
424
        '(personal-name:"donald")',
424
        '(personal-name:"donald")',
425
        "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
425
        "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
426
    );
426
    );
427
427
428
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:donald'] );
428
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:donald'] );
429
    is(
429
    is(
430
        $query->{query}{query_string}{query},
430
        $query->{query}{bool}{must}[0]{query_string}{query},
431
        '(personal-name:donald*)',
431
        '(personal-name:donald*)',
432
        "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
432
        "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
433
    );
433
    );
434
434
435
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:donald'] );
435
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:donald'] );
436
    is(
436
    is(
437
        $query->{query}{query_string}{query},
437
        $query->{query}{bool}{must}[0]{query_string}{query},
438
        '(personal-name.raw:donald*)',
438
        '(personal-name.raw:donald*)',
439
        "query of specific field including period and not quoted is truncated, field name is converted to lower case"
439
        "query of specific field including period and not quoted is truncated, field name is converted to lower case"
440
    );
440
    );
441
441
442
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:"donald"'] );
442
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:"donald"'] );
443
    is(
443
    is(
444
        $query->{query}{query_string}{query},
444
        $query->{query}{bool}{must}[0]{query_string}{query},
445
        '(personal-name.raw:"donald")',
445
        '(personal-name.raw:"donald")',
446
        "query of specific field including period and quoted is not truncated, field name is converted to lower case"
446
        "query of specific field including period and quoted is not truncated, field name is converted to lower case"
447
    );
447
    );
Lines 451-471 subtest 'build_query tests' => sub { Link Here
451
451
452
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
452
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
453
    is(
453
    is(
454
        $query->{query}{query_string}{query},
454
        $query->{query}{bool}{must}[0]{query_string}{query},
455
        '(barcode:123456)',
455
        '(barcode:123456)',
456
        "query of specific field excluded by ESPreventAutoTruncate is not truncated"
456
        "query of specific field excluded by ESPreventAutoTruncate is not truncated"
457
    );
457
    );
458
458
459
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
459
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
460
    is(
460
    is(
461
        $query->{query}{query_string}{query},
461
        $query->{query}{bool}{must}[0]{query_string}{query},
462
        '(local-number:123456)',
462
        '(local-number:123456)',
463
        "query of identifier is not truncated even if QueryAutoTruncate is set"
463
        "query of identifier is not truncated even if QueryAutoTruncate is set"
464
    );
464
    );
465
465
466
    ( undef, $query ) = $qb->build_query_compat( undef, ['onloan:true'] );
466
    ( undef, $query ) = $qb->build_query_compat( undef, ['onloan:true'] );
467
    is(
467
    is(
468
        $query->{query}{query_string}{query},
468
        $query->{query}{bool}{must}[0]{query_string}{query},
469
        '(onloan:true)',
469
        '(onloan:true)',
470
        "query of boolean type field is not truncated even if QueryAutoTruncate is set"
470
        "query of boolean type field is not truncated even if QueryAutoTruncate is set"
471
    );
471
    );
Lines 479-485 subtest 'build_query tests' => sub { Link Here
479
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
479
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
480
        );
480
        );
481
        is(
481
        is(
482
            $query->{query}{query_string}{query},
482
            $query->{query}{bool}{must}[0]{query_string}{query},
483
            '(First* title* subtitle* some* subtitle* Authors* Name. Second* title* Third* title* Second* Author*)',
483
            '(First* title* subtitle* some* subtitle* Authors* Name. Second* title* Third* title* Second* Author*)',
484
            "ISBD punctuation and problematic characters surrounded by spaces properly removed"
484
            "ISBD punctuation and problematic characters surrounded by spaces properly removed"
485
        );
485
        );
Lines 490-503 subtest 'build_query tests' => sub { Link Here
490
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
490
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
491
        );
491
        );
492
        is(
492
        is(
493
            $query->{query}{query_string}{query},
493
            $query->{query}{bool}{must}[0]{query_string}{query},
494
            '(First* title* subtitle* some* subtitle* / Authors* Name. Second* title* ; Third* title* / Second* Author*)',
494
            '(First* title* subtitle* some* subtitle* / Authors* Name. Second* title* ; Third* title* / Second* Author*)',
495
            "ISBD punctuation and problematic characters surrounded by spaces properly removed, RE saved"
495
            "ISBD punctuation and problematic characters surrounded by spaces properly removed, RE saved"
496
        );
496
        );
497
        ( undef, $query ) =
497
        ( undef, $query ) =
498
            $qb->build_query_compat( undef, ['Lorem / ipsum dolor / sit ; / amet'] );
498
            $qb->build_query_compat( undef, ['Lorem / ipsum dolor / sit ; / amet'] );
499
        is(
499
        is(
500
            $query->{query}{query_string}{query},
500
            $query->{query}{bool}{must}[0]{query_string}{query},
501
            '(Lorem* / ipsum* dolor* / sit* amet*)',
501
            '(Lorem* / ipsum* dolor* / sit* amet*)',
502
            "RE saved, last odd unescaped slash preceded by a semicolon removed"
502
            "RE saved, last odd unescaped slash preceded by a semicolon removed"
503
        );
503
        );
Lines 506-519 subtest 'build_query tests' => sub { Link Here
506
        ( undef, $query ) =
506
        ( undef, $query ) =
507
            $qb->build_query_compat( undef, ['Lorem \/ ipsum dolor \/ sit ; \/ amet'] );
507
            $qb->build_query_compat( undef, ['Lorem \/ ipsum dolor \/ sit ; \/ amet'] );
508
        is(
508
        is(
509
            $query->{query}{query_string}{query},
509
            $query->{query}{bool}{must}[0]{query_string}{query},
510
            '(Lorem* ipsum* dolor* sit* amet*)',
510
            '(Lorem* ipsum* dolor* sit* amet*)',
511
            "Escaped slashes (also preceded by another punctuation followed by a space) removed"
511
            "Escaped slashes (also preceded by another punctuation followed by a space) removed"
512
        );
512
        );
513
        ( undef, $query ) =
513
        ( undef, $query ) =
514
            $qb->build_query_compat( undef, ['comma , period . equal = hyphen - slash / escaped_slash \/'] );
514
            $qb->build_query_compat( undef, ['comma , period . equal = hyphen - slash / escaped_slash \/'] );
515
        is(
515
        is(
516
            $query->{query}{query_string}{query},
516
            $query->{query}{bool}{must}[0]{query_string}{query},
517
            '(comma* period* equal* hyphen* slash* escaped_slash* \/)',
517
            '(comma* period* equal* hyphen* slash* escaped_slash* \/)',
518
            "Other problematic characters surrounded by spaces properly removed"
518
            "Other problematic characters surrounded by spaces properly removed"
519
        );
519
        );
Lines 521-527 subtest 'build_query tests' => sub { Link Here
521
        ( undef, $query ) =
521
        ( undef, $query ) =
522
            $qb->build_query_compat( undef, [' &;,:=-/ // \/\/ /&&&==&&  ::-:: '] );
522
            $qb->build_query_compat( undef, [' &;,:=-/ // \/\/ /&&&==&&  ::-:: '] );
523
        is(
523
        is(
524
            $query->{query}{query_string}{query},
524
            $query->{query}{bool}{must}[0]{query_string}{query},
525
            '()',
525
            '()',
526
            "Repeated problematic characters surrounded by spaces removed"
526
            "Repeated problematic characters surrounded by spaces removed"
527
        );
527
        );
Lines 533-539 subtest 'build_query tests' => sub { Link Here
533
            ]
533
            ]
534
        );
534
        );
535
        is(
535
        is(
536
            $query->{query}{query_string}{query},
536
            $query->{query}{bool}{must}[0]{query_string}{query},
537
            '(&amp* amp& semicolon; ;colonsemi* full* full* comma, ,comma* dot. .dot* equal* equal* hyphen- -hypen* slash\/ \/slash*)',
537
            '(&amp* amp& semicolon; ;colonsemi* full* full* comma, ,comma* dot. .dot* equal* equal* hyphen- -hypen* slash\/ \/slash*)',
538
            "ISBD punctuation and problematic characters not removed when not surrounded by spaces."
538
            "ISBD punctuation and problematic characters not removed when not surrounded by spaces."
539
        );
539
        );
Lines 548-554 subtest 'build_query tests' => sub { Link Here
548
548
549
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
549
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
550
    is(
550
    is(
551
        $query->{query}{query_string}{query},
551
        $query->{query}{bool}{must}[0]{query_string}{query},
552
        '(isbn:"9780141930848")',
552
        '(isbn:"9780141930848")',
553
        "nb query transformed into isbn search field"
553
        "nb query transformed into isbn search field"
554
    );
554
    );
Lines 558-599 subtest 'build_query tests' => sub { Link Here
558
558
559
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
559
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
560
    is(
560
    is(
561
        $query->{query}{query_string}{query},
561
        $query->{query}{bool}{must}[0]{query_string}{query},
562
        '(isbn-all:"9780141930848")',
562
        '(isbn-all:"9780141930848")',
563
        "nb query transformed into isbn-all search field"
563
        "nb query transformed into isbn-all search field"
564
    );
564
    );
565
565
566
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848" ns:"1089-6891"'] );
566
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848" ns:"1089-6891"'] );
567
    is(
567
    is(
568
        $query->{query}{query_string}{query},
568
        $query->{query}{bool}{must}[0]{query_string}{query},
569
        '(isbn-all:"9780141930848" issn-all:"1089-6891")',
569
        '(isbn-all:"9780141930848" issn-all:"1089-6891")',
570
        "nb and ns query transformed into isbn-all and issn-all search field"
570
        "nb and ns query transformed into isbn-all and issn-all search field"
571
    );
571
    );
572
572
573
    ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
573
    ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
574
    is(
574
    is(
575
        $query->{query}{query_string}{query},
575
        $query->{query}{bool}{must}[0]{query_string}{query},
576
        '(J.R.R*)',
576
        '(J.R.R*)',
577
        "query including period is truncated but not split at periods"
577
        "query including period is truncated but not split at periods"
578
    );
578
    );
579
579
580
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
580
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
581
    is(
581
    is(
582
        $query->{query}{query_string}{query},
582
        $query->{query}{bool}{must}[0]{query_string}{query},
583
        '(title:"donald duck")',
583
        '(title:"donald duck")',
584
        "query of specific field is not truncated when surrounded by quotes"
584
        "query of specific field is not truncated when surrounded by quotes"
585
    );
585
    );
586
586
587
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
587
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
588
    is(
588
    is(
589
        $query->{query}{query_string}{query},
589
        $query->{query}{bool}{must}[0]{query_string}{query},
590
        '(title:(donald* duck*))',
590
        '(title:(donald* duck*))',
591
        'words of a multi-word term are properly truncated'
591
        'words of a multi-word term are properly truncated'
592
    );
592
    );
593
593
594
    ( undef, $query ) = $qb->build_query_compat( ['AND'], [ 'donald duck', 'disney' ], [ 'title', 'author' ] );
594
    ( undef, $query ) = $qb->build_query_compat( ['AND'], [ 'donald duck', 'disney' ], [ 'title', 'author' ] );
595
    is(
595
    is(
596
        $query->{query}{query_string}{query},
596
        $query->{query}{bool}{must}[0]{query_string}{query},
597
        '(title:(donald* duck*)) AND (author:disney*)',
597
        '(title:(donald* duck*)) AND (author:disney*)',
598
        'words of a multi-word term and single-word term are properly truncated'
598
        'words of a multi-word term and single-word term are properly truncated'
599
    );
599
    );
Lines 601-607 subtest 'build_query tests' => sub { Link Here
601
    ( undef, $query ) =
601
    ( undef, $query ) =
602
        $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
602
        $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
603
    is(
603
    is(
604
        $query->{query}{query_string}{query},
604
        $query->{query}{bool}{must}[0]{query_string}{query},
605
        '(title:"donald duck") AND suppress:false',
605
        '(title:"donald duck") AND suppress:false',
606
        "query of specific field is added AND suppress:false"
606
        "query of specific field is added AND suppress:false"
607
    );
607
    );
Lines 609-615 subtest 'build_query tests' => sub { Link Here
609
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) =
609
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) =
610
        $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
610
        $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
611
    is(
611
    is(
612
        $query->{query}{query_string}{query},
612
        $query->{query}{bool}{must}[0]{query_string}{query},
613
        '(title:"donald duck")',
613
        '(title:"donald duck")',
614
        "query of specific field is not added AND suppress:0"
614
        "query of specific field is not added AND suppress:0"
615
    );
615
    );
Lines 617-623 subtest 'build_query tests' => sub { Link Here
617
    ( undef, $query ) =
617
    ( undef, $query ) =
618
        $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
618
        $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
619
    is(
619
    is(
620
        $query->{query}{query_string}{query},
620
        $query->{query}{bool}{must}[0]{query_string}{query},
621
        '(title:"donald duck") AND author:("Dillinger Escaplan")',
621
        '(title:"donald duck") AND author:("Dillinger Escaplan")',
622
        "Simple query with limit term quoted in parentheses"
622
        "Simple query with limit term quoted in parentheses"
623
    );
623
    );
Lines 627-633 subtest 'build_query tests' => sub { Link Here
627
        [ 'author:Dillinger Escaplan', 'itype:BOOK' ]
627
        [ 'author:Dillinger Escaplan', 'itype:BOOK' ]
628
    );
628
    );
629
    is(
629
    is(
630
        $query->{query}{query_string}{query},
630
        $query->{query}{bool}{must}[0]{query_string}{query},
631
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
631
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
632
        "Simple query with each limit's term quoted in parentheses"
632
        "Simple query with each limit's term quoted in parentheses"
633
    );
633
    );
Lines 639-645 subtest 'build_query tests' => sub { Link Here
639
        [ 'author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD' ]
639
        [ 'author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD' ]
640
    );
640
    );
641
    is(
641
    is(
642
        $query->{query}{query_string}{query},
642
        $query->{query}{bool}{must}[0]{query_string}{query},
643
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
643
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
644
        "Limits quoted correctly when passed as phrase"
644
        "Limits quoted correctly when passed as phrase"
645
    );
645
    );
Lines 649-662 subtest 'build_query tests' => sub { Link Here
649
        ['itype:BOOK']
649
        ['itype:BOOK']
650
    );
650
    );
651
    is(
651
    is(
652
        $query->{query}{query_string}{query},
652
        $query->{query}{bool}{must}[0]{query_string}{query},
653
        '((title:"donald duck") OR (author:"Dillinger Escaplan")) AND itype:("BOOK")',
653
        '((title:"donald duck") OR (author:"Dillinger Escaplan")) AND itype:("BOOK")',
654
        "OR query with limit"
654
        "OR query with limit"
655
    );
655
    );
656
656
657
    ( undef, $query ) = $qb->build_query_compat( undef, undef, undef, ['itype:BOOK'] );
657
    ( undef, $query ) = $qb->build_query_compat( undef, undef, undef, ['itype:BOOK'] );
658
    is(
658
    is(
659
        $query->{query}{query_string}{query},
659
        $query->{query}{bool}{must}[0]{query_string}{query},
660
        'itype:("BOOK")',
660
        'itype:("BOOK")',
661
        "Limit only"
661
        "Limit only"
662
    );
662
    );
Lines 724-730 subtest 'build_query tests' => sub { Link Here
724
        ['acqdate,st-date-normalized= - ']
724
        ['acqdate,st-date-normalized= - ']
725
    );
725
    );
726
    is(
726
    is(
727
        $query->{query}{query_string}{query},
727
        $query->{query}{bool}{must}[0]{query_string}{query},
728
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO *]',
728
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO *]',
729
        'If no date all date-of-acquisition are selected'
729
        'If no date all date-of-acquisition are selected'
730
    );
730
    );
Lines 734-740 subtest 'build_query tests' => sub { Link Here
734
        ['acqdate,st-date-normalized=2024-08-01 - ']
734
        ['acqdate,st-date-normalized=2024-08-01 - ']
735
    );
735
    );
736
    is(
736
    is(
737
        $query->{query}{query_string}{query},
737
        $query->{query}{bool}{must}[0]{query_string}{query},
738
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO *]',
738
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO *]',
739
        'Open start date in date range of an st-date-normalized search is handled properly'
739
        'Open start date in date range of an st-date-normalized search is handled properly'
740
    );
740
    );
Lines 744-750 subtest 'build_query tests' => sub { Link Here
744
        ['acqdate,st-date-normalized= - 2024-08-30']
744
        ['acqdate,st-date-normalized= - 2024-08-30']
745
    );
745
    );
746
    is(
746
    is(
747
        $query->{query}{query_string}{query},
747
        $query->{query}{bool}{must}[0]{query_string}{query},
748
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO 2024-08-30]',
748
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO 2024-08-30]',
749
        'Open end date in date range of an st-date-normalized search is handled properly'
749
        'Open end date in date range of an st-date-normalized search is handled properly'
750
    );
750
    );
Lines 754-760 subtest 'build_query tests' => sub { Link Here
754
        ['acqdate,st-date-normalized=2024-08-01 - 2024-08-30']
754
        ['acqdate,st-date-normalized=2024-08-01 - 2024-08-30']
755
    );
755
    );
756
    is(
756
    is(
757
        $query->{query}{query_string}{query},
757
        $query->{query}{bool}{must}[0]{query_string}{query},
758
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO 2024-08-30]',
758
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO 2024-08-30]',
759
        'Date range in an st-date-normalized search is handled properly'
759
        'Date range in an st-date-normalized search is handled properly'
760
    );
760
    );
Lines 900-906 subtest 'build_query with weighted fields tests' => sub { Link Here
900
        undef, undef, undef, { weighted_fields => 1 }
900
        undef, undef, undef, { weighted_fields => 1 }
901
    );
901
    );
902
902
903
    my $fields = $query->{query}{query_string}{fields};
903
    my $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
904
904
905
    is( @{$fields}, 2, 'Search field with no searchable mappings has been excluded' );
905
    is( @{$fields}, 2, 'Search field with no searchable mappings has been excluded' );
906
906
Lines 915-921 subtest 'build_query with weighted fields tests' => sub { Link Here
915
        undef, undef, undef, { weighted_fields => 1, is_opac => 1 }
915
        undef, undef, undef, { weighted_fields => 1, is_opac => 1 }
916
    );
916
    );
917
917
918
    $fields = $query->{query}{query_string}{fields};
918
    $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
919
919
920
    is_deeply(
920
    is_deeply(
921
        $fields,
921
        $fields,
Lines 928-941 subtest 'build_query with weighted fields tests' => sub { Link Here
928
        undef, ['title:"donald duck"'], undef, undef,
928
        undef, ['title:"donald duck"'], undef, undef,
929
        undef, undef, undef, { weighted_fields => 1 }
929
        undef, undef, undef, { weighted_fields => 1 }
930
    );
930
    );
931
    $fields = $query->{query}{query_string}{fields};
931
    $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
932
    is_deeply( [ sort @$fields ], [ 'heading', 'headingmain' ], 'Authorities fields retrieve for authorities index' );
932
    is_deeply( [ sort @$fields ], [ 'heading', 'headingmain' ], 'Authorities fields retrieve for authorities index' );
933
933
934
    ( undef, $query ) = $qb->build_query_compat(
934
    ( undef, $query ) = $qb->build_query_compat(
935
        undef, ['title:"donald duck"'], undef, undef,
935
        undef, ['title:"donald duck"'], undef, undef,
936
        undef, undef, undef, { weighted_fields => 1, is_opac => 1 }
936
        undef, undef, undef, { weighted_fields => 1, is_opac => 1 }
937
    );
937
    );
938
    $fields = $query->{query}{query_string}{fields};
938
    $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
939
    is_deeply( $fields, ['headingmain'], 'Only opac authorities fields retrieved for authorities index is is_opac' );
939
    is_deeply( $fields, ['headingmain'], 'Only opac authorities fields retrieved for authorities index is is_opac' );
940
940
941
};
941
};
Lines 1169-1172 subtest "_sort_field() tests" => sub { Link Here
1169
    );
1169
    );
1170
};
1170
};
1171
1171
1172
subtest "_build_field_match_boost_query() tests" => sub {
1173
    plan tests => 10;
1174
1175
    my $qb;
1176
1177
    ok(
1178
        $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { 'index' => 'biblios' } ),
1179
        'Creating new query builder object for biblios'
1180
    );
1181
1182
    my $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => [], operands => [] } );
1183
1184
    is_deeply(
1185
        $field_match_boost_query,
1186
        [],
1187
        'Empty array is returned when no indexes or operands passed'
1188
    );
1189
    $field_match_boost_query = $qb->_build_field_match_boost_query();
1190
1191
    is_deeply(
1192
        $field_match_boost_query,
1193
        [],
1194
        'Empty array is returned when no parameters passed'
1195
    );
1196
1197
    my @indexes = ( 'kw', 'subject', 'title' );
1198
    my @operands;
1199
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1200
    is_deeply(
1201
        $field_match_boost_query,
1202
        [],
1203
        'Empty array is returned when no parameters passed'
1204
    );
1205
1206
    push @operands, "turkey";
1207
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1208
    is_deeply(
1209
        $field_match_boost_query,
1210
        [ { match => { 'title-cover' => { query => 'turkey' } } } ],
1211
        'Keyword is converted to title-cover'
1212
    );
1213
1214
    push @operands, "gravy";
1215
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1216
    is_deeply(
1217
        $field_match_boost_query,
1218
        [
1219
            { match => { 'title-cover' => { query => 'turkey' } } },
1220
            { match => { 'subject'     => { query => 'gravy' } } }
1221
        ],
1222
        'Subject is not converted'
1223
    );
1224
1225
    push @operands, "mashed";
1226
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1227
    is_deeply(
1228
        $field_match_boost_query,
1229
        [
1230
            { match => { 'title-cover' => { query => 'turkey' } } },
1231
            { match => { 'subject'     => { query => 'gravy' } } },
1232
            { match => { 'title-cover' => { query => 'mashed' } } },
1233
        ],
1234
        'Title is not converted to title-cover'
1235
    );
1236
1237
    t::lib::Mocks::mock_preference( 'ESBoostFieldMatch', '0' );
1238
    t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
1239
    my ( undef, $query ) = $qb->build_query_compat( ['AND'], [ 'donald duck', 'disney' ], [ 'title', 'author' ] );
1240
    is(
1241
        $query->{query}{bool}{must}[0]{query_string}{query},
1242
        '(title:(donald* duck*)) AND (author:disney*)',
1243
        'words of a multi-word term and single-word term are properly truncated'
1244
    );
1245
    is(
1246
        $query->{query}{bool}{should},
1247
        undef,
1248
        'No should query added for boosting'
1249
    );
1250
1251
    t::lib::Mocks::mock_preference( 'ESBoostFieldMatch', '1' );
1252
    ( undef, $query ) = $qb->build_query_compat( ['AND'], [ 'donald duck', 'disney' ], [ 'title', 'author' ] );
1253
    is_deeply(
1254
        $query->{query}{bool}{should},
1255
        [
1256
            { match => { 'title-cover' => { query => 'donald duck' } } },
1257
            { match => { 'author'      => { query => 'disney' } } }
1258
        ],
1259
        'SHould query is correctly added'
1260
    );
1261
1262
};
1263
1172
$schema->storage->txn_rollback;
1264
$schema->storage->txn_rollback;
1173
- 

Return to bug 38694