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 22-28 use Test::Exception; Link Here
22
use Test::Warn;
22
use Test::Warn;
23
use t::lib::Mocks;
23
use t::lib::Mocks;
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use Test::More tests => 8;
25
use Test::More tests => 9;
26
26
27
use List::Util qw( all );
27
use List::Util qw( all );
28
28
Lines 258-285 subtest 'build_query tests' => sub { Link Here
258
258
259
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
259
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
260
    is(
260
    is(
261
        $query->{query}{query_string}{query},
261
        $query->{query}{bool}{must}[0]{query_string}{query},
262
        "(donald duck)",
262
        "(donald duck)",
263
        "query not altered if QueryAutoTruncate disabled"
263
        "query not altered if QueryAutoTruncate disabled"
264
    );
264
    );
265
265
266
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
266
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['kw,phr'] );
267
    is(
267
    is(
268
        $query->{query}{query_string}{query},
268
        $query->{query}{bool}{must}[0]{query_string}{query},
269
        '("donald duck")',
269
        '("donald duck")',
270
        "keyword as phrase correctly quotes search term and strips index"
270
        "keyword as phrase correctly quotes search term and strips index"
271
    );
271
    );
272
272
273
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
273
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
274
    is(
274
    is(
275
        $query->{query}{query_string}{query},
275
        $query->{query}{bool}{must}[0]{query_string}{query},
276
        '(title:(donald duck))',
276
        '(title:(donald duck))',
277
        'multiple words in a query term are enclosed in parenthesis'
277
        'multiple words in a query term are enclosed in parenthesis'
278
    );
278
    );
279
279
280
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
280
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
281
    is(
281
    is(
282
        $query->{query}{query_string}{query},
282
        $query->{query}{bool}{must}[0]{query_string}{query},
283
        '(title:(donald duck)) AND (author:disney)',
283
        '(title:(donald duck)) AND (author:disney)',
284
        'multiple query terms are enclosed in parenthesis while a single one is not'
284
        'multiple query terms are enclosed in parenthesis while a single one is not'
285
    );
285
    );
Lines 291-325 subtest 'build_query tests' => sub { Link Here
291
291
292
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
292
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019'], ['yr,st-year'] );
293
    is(
293
    is(
294
        $query->{query}{query_string}{query},
294
        $query->{query}{bool}{must}[0]{query_string}{query},
295
        '(date-of-publication:2019)',
295
        '(date-of-publication:2019)',
296
        'Year in an st-year search is handled properly'
296
        'Year in an st-year search is handled properly'
297
    );
297
    );
298
298
299
    ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
299
    ( undef, $query ) = $qb->build_query_compat( undef, ['2018-2019'], ['yr,st-year'] );
300
    is(
300
    is(
301
        $query->{query}{query_string}{query},
301
        $query->{query}{bool}{must}[0]{query_string}{query},
302
        '(date-of-publication:[2018 TO 2019])',
302
        '(date-of-publication:[2018 TO 2019])',
303
        'Year range in an st-year search is handled properly'
303
        'Year range in an st-year search is handled properly'
304
    );
304
    );
305
305
306
    ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
306
    ( undef, $query ) = $qb->build_query_compat( undef, ['-2019'], ['yr,st-year'] );
307
    is(
307
    is(
308
        $query->{query}{query_string}{query},
308
        $query->{query}{bool}{must}[0]{query_string}{query},
309
        '(date-of-publication:[* TO 2019])',
309
        '(date-of-publication:[* TO 2019])',
310
        'Open start year in year range of an st-year search is handled properly'
310
        'Open start year in year range of an st-year search is handled properly'
311
    );
311
    );
312
312
313
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
313
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'] );
314
    is(
314
    is(
315
        $query->{query}{query_string}{query},
315
        $query->{query}{bool}{must}[0]{query_string}{query},
316
        '(date-of-publication:[2019 TO *])',
316
        '(date-of-publication:[2019 TO *])',
317
        'Open end year in year range of an st-year search is handled properly'
317
        'Open end year in year range of an st-year search is handled properly'
318
    );
318
    );
319
319
320
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
320
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'], ['yr,st-numeric=-2019'] );
321
    is(
321
    is(
322
        $query->{query}{query_string}{query},
322
        $query->{query}{bool}{must}[0]{query_string}{query},
323
        '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
323
        '(date-of-publication:[2019 TO *]) AND date-of-publication:[* TO 2019]',
324
        'Open end year in year range of an st-year search is handled properly'
324
        'Open end year in year range of an st-year search is handled properly'
325
    );
325
    );
Lines 327-333 subtest 'build_query tests' => sub { Link Here
327
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'],
327
    ( undef, $query ) = $qb->build_query_compat( undef, ['2019-'], ['yr,st-year'],
328
        ['yr,st-numeric:-2019','yr,st-numeric:2005','yr,st-numeric:1984-2022'] );
328
        ['yr,st-numeric:-2019','yr,st-numeric:2005','yr,st-numeric:1984-2022'] );
329
    is(
329
    is(
330
        $query->{query}{query_string}{query},
330
        $query->{query}{bool}{must}[0]{query_string}{query},
331
        '(date-of-publication:[2019 TO *]) AND (date-of-publication:[* TO 2019]) AND (date-of-publication:2005) AND (date-of-publication:[1984 TO 2022])',
331
        '(date-of-publication:[2019 TO *]) AND (date-of-publication:[* TO 2019]) AND (date-of-publication:2005) AND (date-of-publication:[1984 TO 2022])',
332
        'Limit on year search is handled properly when colon used'
332
        'Limit on year search is handled properly when colon used'
333
    );
333
    );
Lines 337-343 subtest 'build_query tests' => sub { Link Here
337
337
338
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
338
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'] );
339
    is(
339
    is(
340
        $query->{query}{query_string}{query},
340
        $query->{query}{bool}{must}[0]{query_string}{query},
341
        "(donald* duck*)",
341
        "(donald* duck*)",
342
        "simple query is auto truncated when QueryAutoTruncate enabled"
342
        "simple query is auto truncated when QueryAutoTruncate enabled"
343
    );
343
    );
Lines 346-380 subtest 'build_query tests' => sub { Link Here
346
    ( undef, $query ) = $qb->build_query_compat( undef,
346
    ( undef, $query ) = $qb->build_query_compat( undef,
347
        ['donald or duck and mickey not mouse'] );
347
        ['donald or duck and mickey not mouse'] );
348
    is(
348
    is(
349
        $query->{query}{query_string}{query},
349
        $query->{query}{bool}{must}[0]{query_string}{query},
350
        "(donald* or duck* and mickey* not mouse*)",
350
        "(donald* or duck* and mickey* not mouse*)",
351
        "reserved words are not affected by QueryAutoTruncate"
351
        "reserved words are not affected by QueryAutoTruncate"
352
    );
352
    );
353
353
354
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
354
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald* duck*'] );
355
    is(
355
    is(
356
        $query->{query}{query_string}{query},
356
        $query->{query}{bool}{must}[0]{query_string}{query},
357
        "(donald* duck*)",
357
        "(donald* duck*)",
358
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
358
        "query with '*' is unaltered when QueryAutoTruncate is enabled"
359
    );
359
    );
360
360
361
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
361
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck and the mouse'] );
362
    is(
362
    is(
363
        $query->{query}{query_string}{query},
363
        $query->{query}{bool}{must}[0]{query_string}{query},
364
        "(donald* duck* and the* mouse*)",
364
        "(donald* duck* and the* mouse*)",
365
        "individual words are all truncated and stopwords ignored"
365
        "individual words are all truncated and stopwords ignored"
366
    );
366
    );
367
367
368
    ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
368
    ( undef, $query ) = $qb->build_query_compat( undef, ['*'] );
369
    is(
369
    is(
370
        $query->{query}{query_string}{query},
370
        $query->{query}{bool}{must}[0]{query_string}{query},
371
        "(*)",
371
        "(*)",
372
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
372
        "query of just '*' is unaltered when QueryAutoTruncate is enabled"
373
    );
373
    );
374
374
375
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
375
    ( undef, $query ) = $qb->build_query_compat( undef, ['"donald duck"'], undef, ['available'] );
376
    is(
376
    is(
377
        $query->{query}{query_string}{query},
377
        $query->{query}{bool}{must}[0]{query_string}{query},
378
        '("donald duck") AND available:true',
378
        '("donald duck") AND available:true',
379
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
379
        "query with quotes is unaltered when QueryAutoTruncate is enabled"
380
    );
380
    );
Lines 382-388 subtest 'build_query tests' => sub { Link Here
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
        "all quoted strings are unaltered if more than one in query"
387
        "all quoted strings are unaltered if more than one in query"
388
    );
388
    );
Lines 392-426 subtest 'build_query tests' => sub { Link Here
392
392
393
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
393
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
394
    is(
394
    is(
395
        $query->{query}{query_string}{query},
395
        $query->{query}{bool}{must}[0]{query_string}{query},
396
        '(barcode:123456*)',
396
        '(barcode:123456*)',
397
        "query of specific field is truncated"
397
        "query of specific field is truncated"
398
    );
398
    );
399
399
400
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:"donald"'] );
400
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:"donald"'] );
401
    is(
401
    is(
402
        $query->{query}{query_string}{query},
402
        $query->{query}{bool}{must}[0]{query_string}{query},
403
        '(personal-name:"donald")',
403
        '(personal-name:"donald")',
404
        "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
404
        "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case"
405
    );
405
    );
406
406
407
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:donald'] );
407
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:donald'] );
408
    is(
408
    is(
409
        $query->{query}{query_string}{query},
409
        $query->{query}{bool}{must}[0]{query_string}{query},
410
        '(personal-name:donald*)',
410
        '(personal-name:donald*)',
411
        "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
411
        "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case"
412
    );
412
    );
413
413
414
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:donald'] );
414
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:donald'] );
415
    is(
415
    is(
416
        $query->{query}{query_string}{query},
416
        $query->{query}{bool}{must}[0]{query_string}{query},
417
        '(personal-name.raw:donald*)',
417
        '(personal-name.raw:donald*)',
418
        "query of specific field including period and not quoted is truncated, field name is converted to lower case"
418
        "query of specific field including period and not quoted is truncated, field name is converted to lower case"
419
    );
419
    );
420
420
421
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:"donald"'] );
421
    ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:"donald"'] );
422
    is(
422
    is(
423
        $query->{query}{query_string}{query},
423
        $query->{query}{bool}{must}[0]{query_string}{query},
424
        '(personal-name.raw:"donald")',
424
        '(personal-name.raw:"donald")',
425
        "query of specific field including period and quoted is not truncated, field name is converted to lower case"
425
        "query of specific field including period and quoted is not truncated, field name is converted to lower case"
426
    );
426
    );
Lines 430-450 subtest 'build_query tests' => sub { Link Here
430
430
431
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
431
    ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] );
432
    is(
432
    is(
433
        $query->{query}{query_string}{query},
433
        $query->{query}{bool}{must}[0]{query_string}{query},
434
        '(barcode:123456)',
434
        '(barcode:123456)',
435
        "query of specific field excluded by ESPreventAutoTruncate is not truncated"
435
        "query of specific field excluded by ESPreventAutoTruncate is not truncated"
436
    );
436
    );
437
437
438
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
438
    ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] );
439
    is(
439
    is(
440
        $query->{query}{query_string}{query},
440
        $query->{query}{bool}{must}[0]{query_string}{query},
441
        '(local-number:123456)',
441
        '(local-number:123456)',
442
        "query of identifier is not truncated even if QueryAutoTruncate is set"
442
        "query of identifier is not truncated even if QueryAutoTruncate is set"
443
    );
443
    );
444
444
445
    ( undef, $query ) = $qb->build_query_compat( undef, ['onloan:true'] );
445
    ( undef, $query ) = $qb->build_query_compat( undef, ['onloan:true'] );
446
    is(
446
    is(
447
        $query->{query}{query_string}{query},
447
        $query->{query}{bool}{must}[0]{query_string}{query},
448
        '(onloan:true)',
448
        '(onloan:true)',
449
        "query of boolean type field is not truncated even if QueryAutoTruncate is set"
449
        "query of boolean type field is not truncated even if QueryAutoTruncate is set"
450
    );
450
    );
Lines 458-464 subtest 'build_query tests' => sub { Link Here
458
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
458
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
459
        );
459
        );
460
        is(
460
        is(
461
            $query->{query}{query_string}{query},
461
            $query->{query}{bool}{must}[0]{query_string}{query},
462
            '(First* title* subtitle* some* subtitle* Authors* Name. Second* title* Third* title* Second* Author*)',
462
            '(First* title* subtitle* some* subtitle* Authors* Name. Second* title* Third* title* Second* Author*)',
463
            "ISBD punctuation and problematic characters surrounded by spaces properly removed"
463
            "ISBD punctuation and problematic characters surrounded by spaces properly removed"
464
        );
464
        );
Lines 469-482 subtest 'build_query tests' => sub { Link Here
469
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
469
            ['First title ; subtitle : some & subtitle / Authors Name. Second title ; Third title / Second Author']
470
        );
470
        );
471
        is(
471
        is(
472
            $query->{query}{query_string}{query},
472
            $query->{query}{bool}{must}[0]{query_string}{query},
473
            '(First* title* subtitle* some* subtitle* / Authors* Name. Second* title* ; Third* title* / Second* Author*)',
473
            '(First* title* subtitle* some* subtitle* / Authors* Name. Second* title* ; Third* title* / Second* Author*)',
474
            "ISBD punctuation and problematic characters surrounded by spaces properly removed, RE saved"
474
            "ISBD punctuation and problematic characters surrounded by spaces properly removed, RE saved"
475
        );
475
        );
476
        ( undef, $query ) =
476
        ( undef, $query ) =
477
            $qb->build_query_compat( undef, ['Lorem / ipsum dolor / sit ; / amet'] );
477
            $qb->build_query_compat( undef, ['Lorem / ipsum dolor / sit ; / amet'] );
478
        is(
478
        is(
479
            $query->{query}{query_string}{query},
479
            $query->{query}{bool}{must}[0]{query_string}{query},
480
            '(Lorem* / ipsum* dolor* / sit* amet*)',
480
            '(Lorem* / ipsum* dolor* / sit* amet*)',
481
            "RE saved, last odd unescaped slash preceded by a semicolon removed"
481
            "RE saved, last odd unescaped slash preceded by a semicolon removed"
482
        );
482
        );
Lines 485-498 subtest 'build_query tests' => sub { Link Here
485
        ( undef, $query ) =
485
        ( undef, $query ) =
486
            $qb->build_query_compat( undef, ['Lorem \/ ipsum dolor \/ sit ; \/ amet'] );
486
            $qb->build_query_compat( undef, ['Lorem \/ ipsum dolor \/ sit ; \/ amet'] );
487
        is(
487
        is(
488
            $query->{query}{query_string}{query},
488
            $query->{query}{bool}{must}[0]{query_string}{query},
489
            '(Lorem* ipsum* dolor* sit* amet*)',
489
            '(Lorem* ipsum* dolor* sit* amet*)',
490
            "Escaped slashes (also preceded by another punctuation followed by a space) removed"
490
            "Escaped slashes (also preceded by another punctuation followed by a space) removed"
491
        );
491
        );
492
        ( undef, $query ) =
492
        ( undef, $query ) =
493
            $qb->build_query_compat( undef, ['comma , period . equal = hyphen - slash / escaped_slash \/'] );
493
            $qb->build_query_compat( undef, ['comma , period . equal = hyphen - slash / escaped_slash \/'] );
494
        is(
494
        is(
495
            $query->{query}{query_string}{query},
495
            $query->{query}{bool}{must}[0]{query_string}{query},
496
            '(comma* period* equal* hyphen* slash* escaped_slash* \/)',
496
            '(comma* period* equal* hyphen* slash* escaped_slash* \/)',
497
            "Other problematic characters surrounded by spaces properly removed"
497
            "Other problematic characters surrounded by spaces properly removed"
498
        );
498
        );
Lines 500-506 subtest 'build_query tests' => sub { Link Here
500
        ( undef, $query ) =
500
        ( undef, $query ) =
501
            $qb->build_query_compat( undef, [' &;,:=-/ // \/\/ /&&&==&&  ::-:: '] );
501
            $qb->build_query_compat( undef, [' &;,:=-/ // \/\/ /&&&==&&  ::-:: '] );
502
        is(
502
        is(
503
            $query->{query}{query_string}{query},
503
            $query->{query}{bool}{must}[0]{query_string}{query},
504
            '()',
504
            '()',
505
            "Repeated problematic characters surrounded by spaces removed"
505
            "Repeated problematic characters surrounded by spaces removed"
506
        );
506
        );
Lines 512-518 subtest 'build_query tests' => sub { Link Here
512
            ]
512
            ]
513
        );
513
        );
514
        is(
514
        is(
515
            $query->{query}{query_string}{query},
515
            $query->{query}{bool}{must}[0]{query_string}{query},
516
            '(&amp* amp& semicolon; ;colonsemi* full* full* comma, ,comma* dot. .dot* equal* equal* hyphen- -hypen* slash\/ \/slash*)',
516
            '(&amp* amp& semicolon; ;colonsemi* full* full* comma, ,comma* dot. .dot* equal* equal* hyphen- -hypen* slash\/ \/slash*)',
517
            "ISBD punctuation and problematic characters not removed when not surrounded by spaces."
517
            "ISBD punctuation and problematic characters not removed when not surrounded by spaces."
518
        );
518
        );
Lines 527-533 subtest 'build_query tests' => sub { Link Here
527
527
528
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
528
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
529
    is(
529
    is(
530
        $query->{query}{query_string}{query},
530
        $query->{query}{bool}{must}[0]{query_string}{query},
531
        '(isbn:"9780141930848")',
531
        '(isbn:"9780141930848")',
532
        "nb query transformed into isbn search field"
532
        "nb query transformed into isbn search field"
533
    );
533
    );
Lines 537-606 subtest 'build_query tests' => sub { Link Here
537
537
538
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
538
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848"'] );
539
    is(
539
    is(
540
        $query->{query}{query_string}{query},
540
        $query->{query}{bool}{must}[0]{query_string}{query},
541
        '(isbn-all:"9780141930848")',
541
        '(isbn-all:"9780141930848")',
542
        "nb query transformed into isbn-all search field"
542
        "nb query transformed into isbn-all search field"
543
    );
543
    );
544
544
545
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848" ns:"1089-6891"'] );
545
    ( undef, $query ) = $qb->build_query_compat( undef, ['nb:"9780141930848" ns:"1089-6891"'] );
546
    is(
546
    is(
547
        $query->{query}{query_string}{query},
547
        $query->{query}{bool}{must}[0]{query_string}{query},
548
        '(isbn-all:"9780141930848" issn-all:"1089-6891")',
548
        '(isbn-all:"9780141930848" issn-all:"1089-6891")',
549
        "nb and ns query transformed into isbn-all and issn-all search field"
549
        "nb and ns query transformed into isbn-all and issn-all search field"
550
    );
550
    );
551
551
552
    ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
552
    ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] );
553
    is(
553
    is(
554
        $query->{query}{query_string}{query},
554
        $query->{query}{bool}{must}[0]{query_string}{query},
555
        '(J.R.R*)',
555
        '(J.R.R*)',
556
        "query including period is truncated but not split at periods"
556
        "query including period is truncated but not split at periods"
557
    );
557
    );
558
558
559
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
559
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'] );
560
    is(
560
    is(
561
        $query->{query}{query_string}{query},
561
        $query->{query}{bool}{must}[0]{query_string}{query},
562
        '(title:"donald duck")',
562
        '(title:"donald duck")',
563
        "query of specific field is not truncated when surrounded by quotes"
563
        "query of specific field is not truncated when surrounded by quotes"
564
    );
564
    );
565
565
566
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
566
    ( undef, $query ) = $qb->build_query_compat( undef, ['donald duck'], ['title'] );
567
    is(
567
    is(
568
        $query->{query}{query_string}{query},
568
        $query->{query}{bool}{must}[0]{query_string}{query},
569
        '(title:(donald* duck*))',
569
        '(title:(donald* duck*))',
570
        'words of a multi-word term are properly truncated'
570
        'words of a multi-word term are properly truncated'
571
    );
571
    );
572
572
573
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
573
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['donald duck', 'disney'], ['title', 'author'] );
574
    is(
574
    is(
575
        $query->{query}{query_string}{query},
575
        $query->{query}{bool}{must}[0]{query_string}{query},
576
        '(title:(donald* duck*)) AND (author:disney*)',
576
        '(title:(donald* duck*)) AND (author:disney*)',
577
        'words of a multi-word term and single-word term are properly truncated'
577
        'words of a multi-word term and single-word term are properly truncated'
578
    );
578
    );
579
579
580
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
580
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
581
    is(
581
    is(
582
        $query->{query}{query_string}{query},
582
        $query->{query}{bool}{must}[0]{query_string}{query},
583
        '(title:"donald duck") AND suppress:false',
583
        '(title:"donald duck") AND suppress:false',
584
        "query of specific field is added AND suppress:false"
584
        "query of specific field is added AND suppress:false"
585
    );
585
    );
586
586
587
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
587
    ( undef, $query, $simple_query, $query_cgi, $query_desc ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
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
        "query of specific field is not added AND suppress:0"
591
        "query of specific field is not added AND suppress:0"
592
    );
592
    );
593
593
594
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
594
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan'] );
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:("Dillinger Escaplan")',
597
        '(title:"donald duck") AND author:("Dillinger Escaplan")',
598
        "Simple query with limit term quoted in parentheses"
598
        "Simple query with limit term quoted in parentheses"
599
    );
599
    );
600
600
601
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
601
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'itype:BOOK'] );
602
    is(
602
    is(
603
        $query->{query}{query_string}{query},
603
        $query->{query}{bool}{must}[0]{query_string}{query},
604
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
604
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND (itype:("BOOK"))',
605
        "Simple query with each limit's term quoted in parentheses"
605
        "Simple query with each limit's term quoted in parentheses"
606
    );
606
    );
Lines 609-629 subtest 'build_query tests' => sub { Link Here
609
609
610
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
610
    ( undef, $query ) = $qb->build_query_compat( ['AND'], ['title:"donald duck"'], undef, ['author:Dillinger Escaplan', 'mc-itype,phr:BOOK', 'mc-itype,phr:CD'] );
611
    is(
611
    is(
612
        $query->{query}{query_string}{query},
612
        $query->{query}{bool}{must}[0]{query_string}{query},
613
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
613
        '(title:"donald duck") AND (author:("Dillinger Escaplan")) AND itype:(("BOOK") OR ("CD"))',
614
        "Limits quoted correctly when passed as phrase"
614
        "Limits quoted correctly when passed as phrase"
615
    );
615
    );
616
616
617
    ( undef, $query ) = $qb->build_query_compat( ['OR'], ['title:"donald duck"', 'author:"Dillinger Escaplan"'], undef, ['itype:BOOK'] );
617
    ( undef, $query ) = $qb->build_query_compat( ['OR'], ['title:"donald duck"', 'author:"Dillinger Escaplan"'], undef, ['itype:BOOK'] );
618
    is(
618
    is(
619
        $query->{query}{query_string}{query},
619
        $query->{query}{bool}{must}[0]{query_string}{query},
620
        '((title:"donald duck") OR (author:"Dillinger Escaplan")) AND itype:("BOOK")',
620
        '((title:"donald duck") OR (author:"Dillinger Escaplan")) AND itype:("BOOK")',
621
        "OR query with limit"
621
        "OR query with limit"
622
    );
622
    );
623
623
624
    ( undef, $query ) = $qb->build_query_compat( undef, undef, undef, ['itype:BOOK'] );
624
    ( undef, $query ) = $qb->build_query_compat( undef, undef, undef, ['itype:BOOK'] );
625
    is(
625
    is(
626
        $query->{query}{query_string}{query},
626
        $query->{query}{bool}{must}[0]{query_string}{query},
627
        'itype:("BOOK")',
627
        'itype:("BOOK")',
628
        "Limit only"
628
        "Limit only"
629
    );
629
    );
Lines 676-682 subtest 'build_query tests' => sub { Link Here
676
        ['acqdate,st-date-normalized= - ']
676
        ['acqdate,st-date-normalized= - ']
677
    );
677
    );
678
    is(
678
    is(
679
        $query->{query}{query_string}{query},
679
        $query->{query}{bool}{must}[0]{query_string}{query},
680
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO *]',
680
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO *]',
681
        'If no date all date-of-acquisition are selected'
681
        'If no date all date-of-acquisition are selected'
682
    );
682
    );
Lines 686-692 subtest 'build_query tests' => sub { Link Here
686
        ['acqdate,st-date-normalized=2024-08-01 - ']
686
        ['acqdate,st-date-normalized=2024-08-01 - ']
687
    );
687
    );
688
    is(
688
    is(
689
        $query->{query}{query_string}{query},
689
        $query->{query}{bool}{must}[0]{query_string}{query},
690
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO *]',
690
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO *]',
691
        'Open start date in date range of an st-date-normalized search is handled properly'
691
        'Open start date in date range of an st-date-normalized search is handled properly'
692
    );
692
    );
Lines 696-702 subtest 'build_query tests' => sub { Link Here
696
        ['acqdate,st-date-normalized= - 2024-08-30']
696
        ['acqdate,st-date-normalized= - 2024-08-30']
697
    );
697
    );
698
    is(
698
    is(
699
        $query->{query}{query_string}{query},
699
        $query->{query}{bool}{must}[0]{query_string}{query},
700
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO 2024-08-30]',
700
        '(barcode:barcode123123) AND date-of-acquisition.raw:[* TO 2024-08-30]',
701
        'Open end date in date range of an st-date-normalized search is handled properly'
701
        'Open end date in date range of an st-date-normalized search is handled properly'
702
    );
702
    );
Lines 706-712 subtest 'build_query tests' => sub { Link Here
706
        ['acqdate,st-date-normalized=2024-08-01 - 2024-08-30']
706
        ['acqdate,st-date-normalized=2024-08-01 - 2024-08-30']
707
    );
707
    );
708
    is(
708
    is(
709
        $query->{query}{query_string}{query},
709
        $query->{query}{bool}{must}[0]{query_string}{query},
710
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO 2024-08-30]',
710
        '(barcode:barcode123123) AND date-of-acquisition.raw:[2024-08-01 TO 2024-08-30]',
711
        'Date range in an st-date-normalized search is handled properly'
711
        'Date range in an st-date-normalized search is handled properly'
712
    );
712
    );
Lines 828-834 subtest 'build_query with weighted fields tests' => sub { Link Here
828
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
828
    my ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
829
    undef, undef, undef, { weighted_fields => 1 });
829
    undef, undef, undef, { weighted_fields => 1 });
830
830
831
    my $fields = $query->{query}{query_string}{fields};
831
    my $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
832
832
833
    is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
833
    is(@{$fields}, 2, 'Search field with no searchable mappings has been excluded');
834
834
Lines 841-847 subtest 'build_query with weighted fields tests' => sub { Link Here
841
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
841
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
842
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
842
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
843
843
844
    $fields = $query->{query}{query_string}{fields};
844
    $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
845
845
846
    is_deeply(
846
    is_deeply(
847
        $fields,
847
        $fields,
Lines 852-863 subtest 'build_query with weighted fields tests' => sub { Link Here
852
    $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'authorities' } );
852
    $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'authorities' } );
853
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
853
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
854
    undef, undef, undef, { weighted_fields => 1 });
854
    undef, undef, undef, { weighted_fields => 1 });
855
    $fields = $query->{query}{query_string}{fields};
855
    $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
856
    is_deeply( [sort @$fields], ['heading','headingmain'],'Authorities fields retrieve for authorities index');
856
    is_deeply( [sort @$fields], ['heading','headingmain'],'Authorities fields retrieve for authorities index');
857
857
858
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
858
    ( undef, $query ) = $qb->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
859
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
859
    undef, undef, undef, { weighted_fields => 1, is_opac => 1 });
860
    $fields = $query->{query}{query_string}{fields};
860
    $fields = $query->{query}{bool}{must}[0]{query_string}{fields};
861
    is_deeply($fields,['headingmain'],'Only opac authorities fields retrieved for authorities index is is_opac');
861
    is_deeply($fields,['headingmain'],'Only opac authorities fields retrieved for authorities index is is_opac');
862
862
863
};
863
};
Lines 1041-1044 subtest "_sort_field() tests" => sub { Link Here
1041
    );
1041
    );
1042
};
1042
};
1043
1043
1044
subtest "_build_field_match_boost_query() tests" => sub {
1045
    plan tests => 10;
1046
1047
    my $qb;
1048
1049
    ok(
1050
        $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { 'index' => 'biblios' } ),
1051
        'Creating new query builder object for biblios'
1052
    );
1053
1054
    my $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => [], operands => [] } );
1055
1056
    is_deeply(
1057
        $field_match_boost_query,
1058
        [],
1059
        'Empty array is returned when no indexes or operands passed'
1060
    );
1061
    $field_match_boost_query = $qb->_build_field_match_boost_query();
1062
1063
    is_deeply(
1064
        $field_match_boost_query,
1065
        [],
1066
        'Empty array is returned when no parameters passed'
1067
    );
1068
1069
    my @indexes = ( 'kw', 'subject', 'title' );
1070
    my @operands;
1071
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1072
    is_deeply(
1073
        $field_match_boost_query,
1074
        [],
1075
        'Empty array is returned when no parameters passed'
1076
    );
1077
1078
    push @operands, "turkey";
1079
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1080
    is_deeply(
1081
        $field_match_boost_query,
1082
        [ { match => { 'title-cover' => { query => 'turkey' } } } ],
1083
        'Keyword is converted to title-cover'
1084
    );
1085
1086
    push @operands, "gravy";
1087
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1088
    is_deeply(
1089
        $field_match_boost_query,
1090
        [
1091
            { match => { 'title-cover' => { query => 'turkey' } } },
1092
            { match => { 'subject'     => { query => 'gravy' } } }
1093
        ],
1094
        'Subject is not converted'
1095
    );
1096
1097
    push @operands, "mashed";
1098
    $field_match_boost_query = $qb->_build_field_match_boost_query( { indexes => \@indexes, operands => \@operands } );
1099
    is_deeply(
1100
        $field_match_boost_query,
1101
        [
1102
            { match => { 'title-cover' => { query => 'turkey' } } },
1103
            { match => { 'subject'     => { query => 'gravy' } } },
1104
            { match => { 'title-cover' => { query => 'mashed' } } },
1105
        ],
1106
        'Title is not converted to title-cover'
1107
    );
1108
1109
    t::lib::Mocks::mock_preference( 'ESBoostFieldMatch', '0' );
1110
    t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
1111
    my ( undef, $query ) = $qb->build_query_compat( ['AND'], [ 'donald duck', 'disney' ], [ 'title', 'author' ] );
1112
    is(
1113
        $query->{query}{bool}{must}[0]{query_string}{query},
1114
        '(title:(donald* duck*)) AND (author:disney*)',
1115
        'words of a multi-word term and single-word term are properly truncated'
1116
    );
1117
    is(
1118
        $query->{query}{bool}{should},
1119
        undef,
1120
        'No should query added for boosting'
1121
    );
1122
1123
    t::lib::Mocks::mock_preference( 'ESBoostFieldMatch', '1' );
1124
    ( undef, $query ) = $qb->build_query_compat( ['AND'], [ 'donald duck', 'disney' ], [ 'title', 'author' ] );
1125
    is_deeply(
1126
        $query->{query}{bool}{should},
1127
        [
1128
            { match => { 'title-cover' => { query => 'donald duck' } } },
1129
            { match => { 'author'      => { query => 'disney' } } }
1130
        ],
1131
        'SHould query is correctly added'
1132
    );
1133
1134
};
1135
1044
$schema->storage->txn_rollback;
1136
$schema->storage->txn_rollback;
1045
- 

Return to bug 38694