Lines 112-124
sub build_query {
Link Here
|
112 |
# See _convert_facets in Search.pm for how these get turned into |
112 |
# See _convert_facets in Search.pm for how these get turned into |
113 |
# things that Koha can use. |
113 |
# things that Koha can use. |
114 |
$res->{aggregations} = { |
114 |
$res->{aggregations} = { |
115 |
author => { terms => { field => "author__facet" } }, |
115 |
author => { terms => { field => "author__facet" } }, |
116 |
subject => { terms => { field => "subject__facet" } }, |
116 |
subject => { terms => { field => "subject__facet" } }, |
117 |
itype => { terms => { field => "itype__facet" } }, |
117 |
itype => { terms => { field => "itype__facet" } }, |
118 |
location => { terms => { field => "location__facet" } }, |
118 |
location => { terms => { field => "location__facet" } }, |
119 |
'su-geo' => { terms => { field => "su-geo__facet" } }, |
119 |
'su-geo' => { terms => { field => "su-geo__facet" } }, |
120 |
se => { terms => { field => "se__facet" } }, |
120 |
'title-series' => { terms => { field => "title-series__facet" } }, |
121 |
ccode => { terms => { field => "ccode__facet" } }, |
121 |
ccode => { terms => { field => "ccode__facet" } }, |
122 |
}; |
122 |
}; |
123 |
|
123 |
|
124 |
my $display_library_facets = C4::Context->preference('DisplayLibraryFacets'); |
124 |
my $display_library_facets = C4::Context->preference('DisplayLibraryFacets'); |
Lines 411-422
appropriate search object.
Link Here
|
411 |
=cut |
411 |
=cut |
412 |
|
412 |
|
413 |
our $koha_to_index_name = { |
413 |
our $koha_to_index_name = { |
414 |
mainmainentry => 'Heading-Main', |
414 |
mainmainentry => 'heading-main', |
415 |
mainentry => 'Heading', |
415 |
mainentry => 'heading', |
416 |
match => 'Match', |
416 |
match => 'match', |
417 |
'match-heading' => 'Match-heading', |
417 |
'match-heading' => 'match-heading', |
418 |
'see-from' => 'Match-heading-see-from', |
418 |
'see-from' => 'match-heading-see-from', |
419 |
thesaurus => 'Subject-heading-thesaurus', |
419 |
thesaurus => 'subject-heading-thesaurus', |
420 |
all => '' |
420 |
all => '' |
421 |
}; |
421 |
}; |
422 |
|
422 |
|
Lines 429-434
sub build_authorities_query_compat {
Link Here
|
429 |
# extensible hash form that is understood by L<build_authorities_query>. |
429 |
# extensible hash form that is understood by L<build_authorities_query>. |
430 |
my @searches; |
430 |
my @searches; |
431 |
|
431 |
|
|
|
432 |
# Convert to lower case |
433 |
$marclist = [map(lc, @{$marclist})]; |
434 |
|
432 |
# Make sure everything exists |
435 |
# Make sure everything exists |
433 |
foreach my $m (@$marclist) { |
436 |
foreach my $m (@$marclist) { |
434 |
Koha::Exceptions::WrongParameter->throw("Invalid marclist field provided: $m") |
437 |
Koha::Exceptions::WrongParameter->throw("Invalid marclist field provided: $m") |
Lines 445-452
sub build_authorities_query_compat {
Link Here
|
445 |
|
448 |
|
446 |
my %sort; |
449 |
my %sort; |
447 |
my $sort_field = |
450 |
my $sort_field = |
448 |
( $orderby =~ /^Heading/ ) ? 'Heading' |
451 |
( $orderby =~ /^heading/ ) ? 'heading' |
449 |
: ( $orderby =~ /^Auth/ ) ? 'Local-Number' |
452 |
: ( $orderby =~ /^auth/ ) ? 'local-number' |
450 |
: undef; |
453 |
: undef; |
451 |
if ($sort_field) { |
454 |
if ($sort_field) { |
452 |
my $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc'; |
455 |
my $sort_order = ( $orderby =~ /Asc$/ ) ? 'asc' : 'desc'; |
Lines 477-492
sub _convert_sort_fields {
Link Here
|
477 |
|
480 |
|
478 |
# Turn the sorting into something we care about. |
481 |
# Turn the sorting into something we care about. |
479 |
my %sort_field_convert = ( |
482 |
my %sort_field_convert = ( |
480 |
acqdate => 'acqdate', |
483 |
acqdate => 'date-of-acquisition', |
481 |
author => 'author', |
484 |
author => 'author', |
482 |
call_number => 'callnum', |
485 |
call_number => 'local-classification', |
483 |
popularity => 'issues', |
486 |
popularity => 'issues', |
484 |
relevance => undef, # default |
487 |
relevance => undef, # default |
485 |
title => 'title', |
488 |
title => 'title', |
486 |
pubdate => 'pubdate', |
489 |
pubdate => 'date-of-publication', |
|
|
490 |
); |
491 |
my %sort_order_convert = ( |
492 |
dsc => 'desc', |
493 |
asc => 'asc', |
494 |
az => 'asc', |
495 |
za => 'desc', |
487 |
); |
496 |
); |
488 |
my %sort_order_convert = |
|
|
489 |
( qw( dsc desc ), qw( asc asc ), qw( az asc ), qw( za desc ) ); |
490 |
|
497 |
|
491 |
# Convert the fields and orders, drop anything we don't know about. |
498 |
# Convert the fields and orders, drop anything we don't know about. |
492 |
grep { $_->{field} } map { |
499 |
grep { $_->{field} } map { |
Lines 513-533
types.
Link Here
|
513 |
=cut |
520 |
=cut |
514 |
|
521 |
|
515 |
our %index_field_convert = ( |
522 |
our %index_field_convert = ( |
516 |
'kw' => '_all', |
523 |
'kw' => '_all', |
517 |
'ti' => 'title', |
524 |
'ab' => 'abstract', |
518 |
'au' => 'author', |
525 |
'au' => 'author', |
519 |
'su' => 'subject', |
526 |
'lcn' => 'local-classification', |
520 |
'nb' => 'isbn', |
527 |
'callnum' => 'local-classification', |
521 |
'se' => 'title-series', |
528 |
'record-type' => 'rtype', |
522 |
'callnum' => 'callnum', |
529 |
'mc-rtype' => 'rtype', |
523 |
'itype' => 'itype', |
530 |
'mus' => 'rtype', |
524 |
'ln' => 'ln', |
531 |
'ctype' => 'content-type', |
525 |
'branch' => 'homebranch', |
532 |
'lc-card' => 'lc-card-number', |
526 |
'fic' => 'lf', |
533 |
'sn' => 'local-number', |
527 |
'mus' => 'rtype', |
534 |
'yr' => 'date-of-publication', |
528 |
'aud' => 'ta', |
535 |
'pubdate' => 'date-of-publication', |
529 |
'hi' => 'Host-Item-Number', |
536 |
'acqdate' => 'date-of-acquisition', |
|
|
537 |
'date/time-last-modified' => 'date-time-last-modified', |
538 |
'dtlm' => 'date/time-last-modified', |
539 |
'diss' => 'dissertation-information', |
540 |
'ean' => 'ean', |
541 |
'nb' => 'isbn', |
542 |
'isbn' => 'isbn', |
543 |
'ns' => 'issn', |
544 |
'issn' => 'issn', |
545 |
'music-number' => 'identifier-publisher-for-music', |
546 |
'number-music-publisher' => 'identifier-publisher-for-music', |
547 |
'music' => 'identifier-publisher-for-music', |
548 |
'ident' => 'identifier-standard', |
549 |
'name' => 'name', |
550 |
'cpn' => 'corporate-name', |
551 |
'cfn' => 'conference-name', |
552 |
'pn' => 'personal-name', |
553 |
'pb' => 'publisher', |
554 |
'pv' => 'provider', |
555 |
'nt' => 'note', |
556 |
'notes' => 'note', |
557 |
'rcn' => 'record-control-number', |
558 |
'su' => 'subject', |
559 |
'su-to' => 'subject', |
560 |
#'su-geo' => 'subject', |
561 |
'su-ut' => 'subject', |
562 |
'ti' => 'title', |
563 |
'se' => 'title-series', |
564 |
'ut' => 'title-uniform', |
565 |
'an' => 'authority-number', |
566 |
'koha-auth-number' => 'authority-number', |
567 |
'at' => 'authtype', |
568 |
'he' => 'heading', |
569 |
'rank' => 'relevance', |
570 |
'phr' => 'st-phrase', |
571 |
'wrdl' => 'st-word-list', |
572 |
'rt' => 'right-truncation', |
573 |
'rtrn' => 'right-truncation', |
574 |
'ltrn' => 'left-truncation', |
575 |
'rltrn' => 'left-and-right', |
576 |
'mc-itemtype' => 'itemtype', |
577 |
'mc-ccode' => 'ccode', |
578 |
'branch' => 'homebranch', |
579 |
'mc-loc' => 'location', |
580 |
'stocknumber' => 'number-local-acquisition', |
581 |
'inv' => 'number-local-acquisition', |
582 |
'bc' => 'barcode', |
583 |
'mc-itype' => 'itype', |
584 |
'aub' => 'author-personal-bibliography', |
585 |
'auo' => 'author-in-order', |
586 |
'ff8-22' => 'ta', |
587 |
'aud' => 'ta', |
588 |
'audience' => 'ta', |
589 |
'frequency-code' => 'ff8-18', |
590 |
'illustration-code' => 'ff8-18-21', |
591 |
'regularity-code' => 'ff8-19', |
592 |
'type-of-serial' => 'ff8-21', |
593 |
'format' => 'ff8-23', |
594 |
'conference-code' => 'ff8-29', |
595 |
'festschrift-indicator' => 'ff8-30', |
596 |
'index-indicator' => 'ff8-31', |
597 |
'fiction' => 'lf', |
598 |
'fic' => 'lf', |
599 |
'literature-code' => 'lf', |
600 |
'biography' => 'bio', |
601 |
'ff8-34' => 'bio', |
602 |
'biography-code' => 'bio', |
603 |
'l-format' => 'ff7-01-02', |
604 |
'lex' => 'lexile-number', |
605 |
'video-mt' => 'video-mt', |
606 |
'graphic-type' => 'graphics-type', |
607 |
'graphic-support' => 'graphics-support', |
608 |
'item' => 'item', |
609 |
'hi' => 'host-item-number', |
610 |
'itu' => 'index-term-uncontrolled', |
611 |
'itg' => 'index-term-genre', |
530 |
); |
612 |
); |
|
|
613 |
my $field_name_pattern = '[\w\-]+'; |
614 |
my $multi_field_pattern = "(?:\\.$field_name_pattern)*"; |
531 |
|
615 |
|
532 |
sub _convert_index_fields { |
616 |
sub _convert_index_fields { |
533 |
my ( $self, @indexes ) = @_; |
617 |
my ( $self, @indexes ) = @_; |
Lines 540-553
sub _convert_index_fields {
Link Here
|
540 |
# when joining things, to indicate we make it an 'OR' join. |
624 |
# when joining things, to indicate we make it an 'OR' join. |
541 |
# (Sorry, this got a bit ugly after special cases were found.) |
625 |
# (Sorry, this got a bit ugly after special cases were found.) |
542 |
grep { $_->{field} } map { |
626 |
grep { $_->{field} } map { |
543 |
my ( $f, $t ) = split /,/; |
627 |
# Lower case all field names |
|
|
628 |
my ( $f, $t ) = map(lc, split /,/); |
544 |
my $mc = ''; |
629 |
my $mc = ''; |
545 |
if ($f =~ /^mc-/) { |
630 |
if ($f =~ /^mc-/) { |
546 |
$mc = 'mc-'; |
631 |
$mc = 'mc-'; |
547 |
$f =~ s/^mc-//; |
632 |
$f =~ s/^mc-//; |
548 |
} |
633 |
} |
549 |
my $r = { |
634 |
my $r = { |
550 |
field => $index_field_convert{$f}, |
635 |
field => exists $index_field_convert{$f} ? $index_field_convert{$f} : $f, |
551 |
type => $index_type_convert{ $t // '__default' } |
636 |
type => $index_type_convert{ $t // '__default' } |
552 |
}; |
637 |
}; |
553 |
$r->{field} = ($mc . $r->{field}) if $mc && $r->{field}; |
638 |
$r->{field} = ($mc . $r->{field}) if $mc && $r->{field}; |
Lines 599-610
second part is stripped off as we can't yet handle that. Making it work
Link Here
|
599 |
will have to wait for a real query parser. |
684 |
will have to wait for a real query parser. |
600 |
|
685 |
|
601 |
=cut |
686 |
=cut |
602 |
|
|
|
603 |
sub _convert_index_strings_freeform { |
687 |
sub _convert_index_strings_freeform { |
604 |
my ( $self, $search ) = @_; |
688 |
my ( $self, $search ) = @_; |
605 |
while ( my ( $zeb, $es ) = each %index_field_convert ) { |
689 |
# @TODO: Currenty will alter also fields contained within quotes: |
606 |
$search =~ s/\b$zeb(?:,[\w\-]*)?:/$es:/g; |
690 |
# `searching for "stuff cn:123"` for example will become |
607 |
} |
691 |
# `searching for "stuff local-number:123" |
|
|
692 |
# |
693 |
# Fixing this is tricky, one possibility: |
694 |
# https://stackoverflow.com/questions/19193876/perl-regex-to-match-a-string-that-is-not-enclosed-in-quotes |
695 |
# Still not perfect, and will not handle escaped quotes within quotes and assumes balanced quotes. |
696 |
# |
697 |
# Another, not so elegant, solution could be to replace all quoted content with placeholders, and put |
698 |
# them back when processing is done. |
699 |
|
700 |
# Lower case field names |
701 |
$search =~ s/($field_name_pattern)(?:,[\w-]*)?($multi_field_pattern):/\L$1\E$2:/og; |
702 |
# Resolve possible field aliases |
703 |
$search =~ s/($field_name_pattern)($multi_field_pattern):/(exists $index_field_convert{$1} ? $index_field_convert{$1} : $1)."$2:"/oge; |
608 |
return $search; |
704 |
return $search; |
609 |
} |
705 |
} |
610 |
|
706 |
|
Lines 793-804
operands and double quoted strings.
Link Here
|
793 |
|
889 |
|
794 |
=cut |
890 |
=cut |
795 |
|
891 |
|
|
|
892 |
my $tokenize_split_re = qr/((?:${field_name_pattern}${multi_field_pattern}:)?"[^"]+"|\s+)/; |
796 |
sub _truncate_terms { |
893 |
sub _truncate_terms { |
797 |
my ( $self, $query ) = @_; |
894 |
my ( $self, $query ) = @_; |
798 |
|
895 |
|
799 |
# '"donald duck" title:"the mouse" and peter" get split into |
896 |
# '"donald duck" title:"the mouse" and peter" get split into |
800 |
# ['', '"donald duck"', '', ' ', '', 'title:"the mouse"', '', ' ', 'and', ' ', 'pete'] |
897 |
# ['', '"donald duck"', '', ' ', '', 'title:"the mouse"', '', ' ', 'and', ' ', 'pete'] |
801 |
my @tokens = split /((?:[\w\-.]+:)?"[^"]+"|\s+)/, $query; |
898 |
my @tokens = split $tokenize_split_re, $query; |
802 |
|
899 |
|
803 |
# Filter out empty tokens |
900 |
# Filter out empty tokens |
804 |
my @words = grep { $_ !~ /^\s*$/ } @tokens; |
901 |
my @words = grep { $_ !~ /^\s*$/ } @tokens; |