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 204-210
sub build_query_compat {
Link Here
|
204 |
my @sort_params = $self->_convert_sort_fields(@$sort_by); |
204 |
my @sort_params = $self->_convert_sort_fields(@$sort_by); |
205 |
my @index_params = $self->_convert_index_fields(@$indexes); |
205 |
my @index_params = $self->_convert_index_fields(@$indexes); |
206 |
my $limits = $self->_fix_limit_special_cases($orig_limits); |
206 |
my $limits = $self->_fix_limit_special_cases($orig_limits); |
207 |
if ( $params->{suppress} ) { push @$limits, "suppress:0"; } |
207 |
if ( $params->{suppress} ) { push @$limits, "Suppress:0"; } |
208 |
|
208 |
|
209 |
# Merge the indexes in with the search terms and the operands so that |
209 |
# Merge the indexes in with the search terms and the operands so that |
210 |
# each search thing is a handy unit. |
210 |
# each search thing is a handy unit. |
Lines 220-225
sub build_query_compat {
Link Here
|
220 |
}; |
220 |
}; |
221 |
} |
221 |
} |
222 |
|
222 |
|
|
|
223 |
|
223 |
# We build a string query from limits and the queries. An alternative |
224 |
# We build a string query from limits and the queries. An alternative |
224 |
# would be to pass them separately into build_query and let it build |
225 |
# would be to pass them separately into build_query and let it build |
225 |
# them into a structured ES query itself. Maybe later, though that'd be |
226 |
# them into a structured ES query itself. Maybe later, though that'd be |
Lines 477-492
sub _convert_sort_fields {
Link Here
|
477 |
|
478 |
|
478 |
# Turn the sorting into something we care about. |
479 |
# Turn the sorting into something we care about. |
479 |
my %sort_field_convert = ( |
480 |
my %sort_field_convert = ( |
480 |
acqdate => 'acqdate', |
481 |
acqdate => 'date-of-acquisition', |
481 |
author => 'author', |
482 |
author => 'author', |
482 |
call_number => 'callnum', |
483 |
call_number => 'local-classification', |
483 |
popularity => 'issues', |
484 |
popularity => 'issues', |
484 |
relevance => undef, # default |
485 |
relevance => undef, # default |
485 |
title => 'title', |
486 |
title => 'title', |
486 |
pubdate => 'pubdate', |
487 |
pubdate => 'date-of-publication', |
|
|
488 |
); |
489 |
my %sort_order_convert = ( |
490 |
dsc => 'desc', |
491 |
asc => 'asc', |
492 |
az => 'asc', |
493 |
za => 'desc', |
487 |
); |
494 |
); |
488 |
my %sort_order_convert = |
|
|
489 |
( qw( dsc desc ), qw( asc asc ), qw( az asc ), qw( za desc ) ); |
490 |
|
495 |
|
491 |
# Convert the fields and orders, drop anything we don't know about. |
496 |
# Convert the fields and orders, drop anything we don't know about. |
492 |
grep { $_->{field} } map { |
497 |
grep { $_->{field} } map { |
Lines 513-533
types.
Link Here
|
513 |
=cut |
518 |
=cut |
514 |
|
519 |
|
515 |
our %index_field_convert = ( |
520 |
our %index_field_convert = ( |
516 |
'kw' => '_all', |
521 |
'kw' => '_all', |
517 |
'ti' => 'title', |
522 |
'ab' => 'abstract', |
518 |
'au' => 'author', |
523 |
'au' => 'author', |
519 |
'su' => 'subject', |
524 |
'lcn' => 'local-classification', |
520 |
'nb' => 'isbn', |
525 |
'callnum' => 'local-classification', |
521 |
'se' => 'title-series', |
526 |
'record-type' => 'rtype', |
522 |
'callnum' => 'callnum', |
527 |
'mc-rtype' => 'rtype', |
523 |
'itype' => 'itype', |
528 |
'mus' => 'rtype', |
524 |
'ln' => 'ln', |
529 |
'ctype' => 'content-type', |
525 |
'branch' => 'homebranch', |
530 |
'lc-card' => 'lc-card-number', |
526 |
'fic' => 'lf', |
531 |
'sn' => 'local-number', |
527 |
'mus' => 'rtype', |
532 |
'yr' => 'date-of-publication', |
528 |
'aud' => 'ta', |
533 |
'pubdate' => 'date-of-publication', |
529 |
'hi' => 'Host-Item-Number', |
534 |
'acqdate' => 'date-of-acquisition', |
|
|
535 |
'date/time-last-modified' => 'date-time-last-modified', |
536 |
'dtlm' => 'date/time-last-modified', |
537 |
'diss' => 'dissertation-information', |
538 |
'ean' => 'ean', |
539 |
'nb' => 'isbn', |
540 |
'isbn' => 'isbn', |
541 |
'ns' => 'issn', |
542 |
'issn' => 'issn', |
543 |
'music-number' => 'identifier-publisher-for-music', |
544 |
'number-music-publisher' => 'identifier-publisher-for-music', |
545 |
'music' => 'identifier-publisher-for-music', |
546 |
'ident' => 'identifier-standard', |
547 |
'name' => 'name', |
548 |
'cpn' => 'corporate-name', |
549 |
'cfn' => 'conference-name', |
550 |
'pn' => 'personal-name', |
551 |
'pb' => 'publisher', |
552 |
'pv' => 'provider', |
553 |
'nt' => 'note', |
554 |
'notes' => 'note', |
555 |
'rcn' => 'record-control-number', |
556 |
'su' => 'subject', |
557 |
'su-to' => 'subject', |
558 |
#'su-geo' => 'subject', |
559 |
'su-ut' => 'subject', |
560 |
'ti' => 'title', |
561 |
'se' => 'title-series', |
562 |
'ut' => 'title-uniform', |
563 |
'an' => 'authority-number', |
564 |
'koha-auth-number' => 'authority-number', |
565 |
'at' => 'authtype', |
566 |
'he' => 'heading', |
567 |
'rank' => 'relevance', |
568 |
'phr' => 'st-phrase', |
569 |
'wrdl' => 'st-word-list', |
570 |
'rt' => 'right-truncation', |
571 |
'rtrn' => 'right-truncation', |
572 |
'ltrn' => 'left-truncation', |
573 |
'rltrn' => 'left-and-right', |
574 |
'mc-itemtype' => 'itemtype', |
575 |
'mc-ccode' => 'ccode', |
576 |
'branch' => 'homebranch', |
577 |
'mc-loc' => 'location', |
578 |
'stocknumber' => 'number-local-acquisition', |
579 |
'inv' => 'number-local-acquisition', |
580 |
'bc' => 'barcode', |
581 |
'mc-itype' => 'itype', |
582 |
'aub' => 'author-personal-bibliography', |
583 |
'auo' => 'author-in-order', |
584 |
'ff8-22' => 'ta', |
585 |
'aud' => 'ta', |
586 |
'audience' => 'ta', |
587 |
'frequency-code' => 'ff8-18', |
588 |
'illustration-code' => 'ff8-18-21', |
589 |
'regularity-code' => 'ff8-19', |
590 |
'type-of-serial' => 'ff8-21', |
591 |
'format' => 'ff8-23', |
592 |
'conference-code' => 'ff8-29', |
593 |
'festschrift-indicator' => 'ff8-30', |
594 |
'index-indicator' => 'ff8-31', |
595 |
'fiction' => 'lf', |
596 |
'fic' => 'lf', |
597 |
'literature-code' => 'lf', |
598 |
'biography' => 'bio', |
599 |
'ff8-34' => 'bio', |
600 |
'biography-code' => 'bio', |
601 |
'l-format' => 'ff7-01-02', |
602 |
'lex' => 'lexile-number', |
603 |
'video-mt' => 'video-mt', |
604 |
'graphic-type' => 'graphics-type', |
605 |
'graphic-support' => 'graphics-support', |
606 |
'item' => 'item', |
607 |
'hi' => 'host-item-number', |
608 |
'itu' => 'index-term-uncontrolled', |
609 |
'itg' => 'index-term-genre', |
530 |
); |
610 |
); |
|
|
611 |
my $field_name_pattern = '[\w\-]+'; |
612 |
my $multi_field_pattern = "(?:\\.$field_name_pattern)*"; |
531 |
|
613 |
|
532 |
sub _convert_index_fields { |
614 |
sub _convert_index_fields { |
533 |
my ( $self, @indexes ) = @_; |
615 |
my ( $self, @indexes ) = @_; |
Lines 540-553
sub _convert_index_fields {
Link Here
|
540 |
# when joining things, to indicate we make it an 'OR' join. |
622 |
# when joining things, to indicate we make it an 'OR' join. |
541 |
# (Sorry, this got a bit ugly after special cases were found.) |
623 |
# (Sorry, this got a bit ugly after special cases were found.) |
542 |
grep { $_->{field} } map { |
624 |
grep { $_->{field} } map { |
543 |
my ( $f, $t ) = split /,/; |
625 |
# Lower case all field names |
|
|
626 |
my ( $f, $t ) = map(lc, split /,/); |
544 |
my $mc = ''; |
627 |
my $mc = ''; |
545 |
if ($f =~ /^mc-/) { |
628 |
if ($f =~ /^mc-/) { |
546 |
$mc = 'mc-'; |
629 |
$mc = 'mc-'; |
547 |
$f =~ s/^mc-//; |
630 |
$f =~ s/^mc-//; |
548 |
} |
631 |
} |
549 |
my $r = { |
632 |
my $r = { |
550 |
field => $index_field_convert{$f}, |
633 |
field => exists $index_field_convert{$f} ? $index_field_convert{$f} : $f, |
551 |
type => $index_type_convert{ $t // '__default' } |
634 |
type => $index_type_convert{ $t // '__default' } |
552 |
}; |
635 |
}; |
553 |
$r->{field} = ($mc . $r->{field}) if $mc && $r->{field}; |
636 |
$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. |
682 |
will have to wait for a real query parser. |
600 |
|
683 |
|
601 |
=cut |
684 |
=cut |
602 |
|
|
|
603 |
sub _convert_index_strings_freeform { |
685 |
sub _convert_index_strings_freeform { |
604 |
my ( $self, $search ) = @_; |
686 |
my ( $self, $search ) = @_; |
605 |
while ( my ( $zeb, $es ) = each %index_field_convert ) { |
687 |
# @TODO: Currenty will alter also fields contained within quotes: |
606 |
$search =~ s/\b$zeb(?:,[\w\-]*)?:/$es:/g; |
688 |
# `searching for "stuff cn:123"` for example will become |
607 |
} |
689 |
# `searching for "stuff local-number:123" |
|
|
690 |
# |
691 |
# Fixing this is tricky, one possibility: |
692 |
# https://stackoverflow.com/questions/19193876/perl-regex-to-match-a-string-that-is-not-enclosed-in-quotes |
693 |
# Still not perfect, and will not handle escaped quotes within quotes and assumes balanced quotes. |
694 |
# |
695 |
# Another, not so elegant, solution could be to replace all quoted content with placeholders, and put |
696 |
# them back when processing is done. |
697 |
|
698 |
# Lower case field names |
699 |
$search =~ s/($field_name_pattern)(?:,[\w-]*)?($multi_field_pattern):/\L$1\E$2:/og; |
700 |
# Resolve possible field aliases |
701 |
$search =~ s/($field_name_pattern)($multi_field_pattern):/(exists $index_field_convert{$1} ? $index_field_convert{$1} : $1)."$2:"/oge; |
608 |
return $search; |
702 |
return $search; |
609 |
} |
703 |
} |
610 |
|
704 |
|
Lines 793-804
operands and double quoted strings.
Link Here
|
793 |
|
887 |
|
794 |
=cut |
888 |
=cut |
795 |
|
889 |
|
|
|
890 |
my $tokenize_split_re = qr/((?:${field_name_pattern}${multi_field_pattern}:)?"[^"]+"|\s+)/; |
796 |
sub _truncate_terms { |
891 |
sub _truncate_terms { |
797 |
my ( $self, $query ) = @_; |
892 |
my ( $self, $query ) = @_; |
798 |
|
893 |
|
799 |
# '"donald duck" title:"the mouse" and peter" get split into |
894 |
# '"donald duck" title:"the mouse" and peter" get split into |
800 |
# ['', '"donald duck"', '', ' ', '', 'title:"the mouse"', '', ' ', 'and', ' ', 'pete'] |
895 |
# ['', '"donald duck"', '', ' ', '', 'title:"the mouse"', '', ' ', 'and', ' ', 'pete'] |
801 |
my @tokens = split /((?:[\w\-.]+:)?"[^"]+"|\s+)/, $query; |
896 |
my @tokens = split $tokenize_split_re, $query; |
802 |
|
897 |
|
803 |
# Filter out empty tokens |
898 |
# Filter out empty tokens |
804 |
my @words = grep { $_ !~ /^\s*$/ } @tokens; |
899 |
my @words = grep { $_ !~ /^\s*$/ } @tokens; |