|
Lines 48-53
use URI::Escape;
Link Here
|
| 48 |
|
48 |
|
| 49 |
use C4::Context; |
49 |
use C4::Context; |
| 50 |
use Koha::Exceptions; |
50 |
use Koha::Exceptions; |
|
|
51 |
use Koha::Caches; |
| 51 |
|
52 |
|
| 52 |
=head2 build_query |
53 |
=head2 build_query |
| 53 |
|
54 |
|
|
Lines 90-98
sub build_query {
Link Here
|
| 90 |
query => $query, |
91 |
query => $query, |
| 91 |
fuzziness => $fuzzy_enabled ? 'auto' : '0', |
92 |
fuzziness => $fuzzy_enabled ? 'auto' : '0', |
| 92 |
default_operator => 'AND', |
93 |
default_operator => 'AND', |
| 93 |
default_field => '_all', |
94 |
fields => $self->_search_fields({ is_opac => $options{is_opac}, weighted_fields => $options{weighted_fields} }), |
| 94 |
lenient => JSON::true, |
95 |
lenient => JSON::true, |
| 95 |
fields => $options{fields} || [], |
|
|
| 96 |
} |
96 |
} |
| 97 |
}; |
97 |
}; |
| 98 |
|
98 |
|
|
Lines 231-247
sub build_query_compat {
Link Here
|
| 231 |
join( ' ', $self->_create_query_string(@search_params) ) || (), |
231 |
join( ' ', $self->_create_query_string(@search_params) ) || (), |
| 232 |
$self->_join_queries( $self->_convert_index_strings(@$limits) ) || () ); |
232 |
$self->_join_queries( $self->_convert_index_strings(@$limits) ) || () ); |
| 233 |
|
233 |
|
| 234 |
my @fields = '_all'; |
|
|
| 235 |
if ( defined($params->{weighted_fields}) && $params->{weighted_fields} ) { |
| 236 |
push @fields, sprintf("%s^%s", $_->name, $_->weight) for Koha::SearchFields->weighted_fields; |
| 237 |
} |
| 238 |
|
| 239 |
# If there's no query on the left, let's remove the junk left behind |
234 |
# If there's no query on the left, let's remove the junk left behind |
| 240 |
$query_str =~ s/^ AND //; |
235 |
$query_str =~ s/^ AND //; |
| 241 |
my %options; |
236 |
my %options; |
| 242 |
$options{fields} = \@fields; |
|
|
| 243 |
$options{sort} = \@sort_params; |
237 |
$options{sort} = \@sort_params; |
| 244 |
$options{expanded_facet} = $params->{expanded_facet}; |
238 |
$options{expanded_facet} = $params->{expanded_facet}; |
|
|
239 |
$options{is_opac} = $params->{is_opac}; |
| 240 |
$options{weighted_fields} = $params->{weighted_fields}; |
| 245 |
my $query = $self->build_query( $query_str, %options ); |
241 |
my $query = $self->build_query( $query_str, %options ); |
| 246 |
|
242 |
|
| 247 |
#die Dumper($query); |
243 |
#die Dumper($query); |
|
Lines 302-323
sub build_authorities_query {
Link Here
|
| 302 |
|
298 |
|
| 303 |
foreach my $s ( @{ $search->{searches} } ) { |
299 |
foreach my $s ( @{ $search->{searches} } ) { |
| 304 |
my ( $wh, $op, $val ) = @{$s}{qw(where operator value)}; |
300 |
my ( $wh, $op, $val ) = @{$s}{qw(where operator value)}; |
| 305 |
$wh = '_all' if $wh eq ''; |
301 |
if ( $op eq 'is' || $op eq '=' || $op eq 'exact') { |
| 306 |
if ( $op eq 'is' || $op eq '=' || $op eq 'exact' ) { |
302 |
if ($wh) { |
| 307 |
|
303 |
# Match the whole field, case insensitive, UTF normalized. |
| 308 |
# look for something that matches a term completely |
304 |
push @query_parts, { term => { "$wh.ci_raw" => $val } }; |
| 309 |
# note, '=' is about numerical vals. May need special handling. |
305 |
} |
| 310 |
# Also, we lowercase our search because the ES |
306 |
else { |
| 311 |
# index lowercases its values, and term searches don't get the |
307 |
# Match the whole field for all searchable fields, case insensitive, |
| 312 |
# search analyzer applied to them. |
308 |
# UTF normalized. |
| 313 |
push @query_parts, { match_phrase => {"$wh.phrase" => lc $val} }; |
309 |
# Given that field data is "The quick brown fox" |
|
|
310 |
# "The quick brown fox" and "the quick brown fox" will match |
| 311 |
# but not "quick brown fox". |
| 312 |
push @query_parts, { |
| 313 |
multi_match => { |
| 314 |
query => $val, |
| 315 |
fields => $self->_search_fields({ subfield => 'ci_raw' }), |
| 316 |
} |
| 317 |
}; |
| 318 |
} |
| 314 |
} |
319 |
} |
| 315 |
elsif ( $op eq 'start' ) { |
320 |
elsif ( $op eq 'start') { |
| 316 |
# startswith search, uses lowercase untokenized version of heading |
321 |
# Match the prefix within a field for all searchable fields. |
| 317 |
push @query_parts, { match_phrase_prefix => {"$wh.phrase" => lc $val} }; |
322 |
# Given that field data is "The quick brown fox" |
|
|
323 |
# "The quick bro" will match, but not "quick bro" |
| 324 |
|
| 325 |
# Does not seems to be a multi prefix query |
| 326 |
# so we need to create one |
| 327 |
if ($wh) { |
| 328 |
# Match prefix of the field. |
| 329 |
push @query_parts, { prefix => {"$wh.ci_raw" => $val} }; |
| 330 |
} |
| 331 |
else { |
| 332 |
my @prefix_queries; |
| 333 |
foreach my $field (@{$self->_search_fields()}) { |
| 334 |
push @prefix_queries, { |
| 335 |
prefix => { "$field.ci_raw" => $val } |
| 336 |
}; |
| 337 |
} |
| 338 |
push @query_parts, { |
| 339 |
'bool' => { |
| 340 |
'should' => \@prefix_queries, |
| 341 |
'minimum_should_match' => 1 |
| 342 |
} |
| 343 |
}; |
| 344 |
} |
| 318 |
} |
345 |
} |
| 319 |
else { |
346 |
else { |
| 320 |
# regular wordlist stuff |
347 |
# Query all searchable fields. |
|
|
348 |
# Given that field data is "The quick brown fox" |
| 349 |
# a search containing any of the words will match, regardless |
| 350 |
# of order. |
| 351 |
|
| 321 |
my @tokens = $self->_split_query( $val ); |
352 |
my @tokens = $self->_split_query( $val ); |
| 322 |
foreach my $token ( @tokens ) { |
353 |
foreach my $token ( @tokens ) { |
| 323 |
$token = $self->_truncate_terms( |
354 |
$token = $self->_truncate_terms( |
|
Lines 325-360
sub build_authorities_query {
Link Here
|
| 325 |
); |
356 |
); |
| 326 |
} |
357 |
} |
| 327 |
my $query = $self->_join_queries( @tokens ); |
358 |
my $query = $self->_join_queries( @tokens ); |
| 328 |
push @query_parts, { query_string => { default_field => $wh, query => $query } }; |
359 |
|
|
|
360 |
if ($wh) { |
| 361 |
push @query_parts, { query_string => { default_field => $wh, query => $query } }; |
| 362 |
} |
| 363 |
else { |
| 364 |
push @query_parts, { |
| 365 |
query_string => { |
| 366 |
query => $query, |
| 367 |
fields => $self->_search_fields(), |
| 368 |
} |
| 369 |
}; |
| 370 |
} |
| 329 |
} |
371 |
} |
| 330 |
} |
372 |
} |
| 331 |
|
373 |
|
| 332 |
# Merge the query parts appropriately |
374 |
# Merge the query parts appropriately |
| 333 |
# 'should' behaves like 'or' |
375 |
# 'should' behaves like 'or' |
| 334 |
# 'must' behaves like 'and' |
376 |
# 'must' behaves like 'and' |
| 335 |
# Zebra results seem to match must so using that here |
377 |
# Zebra behaviour seem to match must so using that here |
| 336 |
my $query = { query => |
378 |
my $elastic_query = {}; |
| 337 |
{ bool => |
379 |
$elastic_query->{bool}->{must} = \@query_parts; |
| 338 |
{ must => \@query_parts } |
380 |
|
| 339 |
} |
381 |
# Filter by authtypecode if set |
| 340 |
}; |
382 |
if ($search->{authtypecode}) { |
| 341 |
|
383 |
$elastic_query->{bool}->{filter} = { |
| 342 |
my %s; |
384 |
term => { |
| 343 |
if ( exists $search->{sort} ) { |
385 |
"authtype.raw" => $search->{authtypecode} |
| 344 |
foreach my $k ( keys %{ $search->{sort} } ) { |
386 |
} |
| 345 |
my $f = $self->_sort_field($k); |
387 |
}; |
| 346 |
$s{$f} = $search->{sort}{$k}; |
|
|
| 347 |
} |
| 348 |
$search->{sort} = \%s; |
| 349 |
} |
388 |
} |
| 350 |
|
389 |
|
| 351 |
# add the sort stuff |
390 |
my $query = { |
| 352 |
$query->{sort} = [ $search->{sort} ] if exists $search->{sort}; |
391 |
query => $elastic_query |
|
|
392 |
}; |
| 393 |
|
| 394 |
# Add the sort stuff |
| 395 |
$query->{sort} = [ $search->{sort} ] if exists $search->{sort}; |
| 353 |
|
396 |
|
| 354 |
return $query; |
397 |
return $query; |
| 355 |
} |
398 |
} |
| 356 |
|
399 |
|
| 357 |
|
|
|
| 358 |
=head2 build_authorities_query_compat |
400 |
=head2 build_authorities_query_compat |
| 359 |
|
401 |
|
| 360 |
my ($query) = |
402 |
my ($query) = |
|
Lines 452-459
sub build_authorities_query_compat {
Link Here
|
| 452 |
|
494 |
|
| 453 |
my %sort; |
495 |
my %sort; |
| 454 |
my $sort_field = |
496 |
my $sort_field = |
| 455 |
( $orderby =~ /^heading/ ) ? 'heading' |
497 |
( $orderby =~ /^heading/ ) ? 'heading__sort' |
| 456 |
: ( $orderby =~ /^auth/ ) ? 'local-number' |
498 |
: ( $orderby =~ /^auth/ ) ? 'local-number__sort' |
| 457 |
: undef; |
499 |
: undef; |
| 458 |
if ($sort_field) { |
500 |
if ($sort_field) { |
| 459 |
my $sort_order = ( $orderby =~ /asc$/ ) ? 'asc' : 'desc'; |
501 |
my $sort_order = ( $orderby =~ /asc$/ ) ? 'asc' : 'desc'; |
|
Lines 520-526
types.
Link Here
|
| 520 |
=cut |
562 |
=cut |
| 521 |
|
563 |
|
| 522 |
our %index_field_convert = ( |
564 |
our %index_field_convert = ( |
| 523 |
'kw' => '_all', |
565 |
'kw' => '', |
| 524 |
'ab' => 'abstract', |
566 |
'ab' => 'abstract', |
| 525 |
'au' => 'author', |
567 |
'au' => 'author', |
| 526 |
'lcn' => 'local-classification', |
568 |
'lcn' => 'local-classification', |
|
Lines 614-620
sub _convert_index_fields {
Link Here
|
| 614 |
# If a field starts with mc- we save it as it's used (and removed) later |
656 |
# If a field starts with mc- we save it as it's used (and removed) later |
| 615 |
# when joining things, to indicate we make it an 'OR' join. |
657 |
# when joining things, to indicate we make it an 'OR' join. |
| 616 |
# (Sorry, this got a bit ugly after special cases were found.) |
658 |
# (Sorry, this got a bit ugly after special cases were found.) |
| 617 |
grep { $_->{field} } map { |
659 |
map { |
| 618 |
# Lower case all field names |
660 |
# Lower case all field names |
| 619 |
my ( $f, $t ) = map(lc, split /,/); |
661 |
my ( $f, $t ) = map(lc, split /,/); |
| 620 |
my $mc = ''; |
662 |
my $mc = ''; |
|
Lines 627-633
sub _convert_index_fields {
Link Here
|
| 627 |
type => $index_type_convert{ $t // '__default' } |
669 |
type => $index_type_convert{ $t // '__default' } |
| 628 |
}; |
670 |
}; |
| 629 |
$r->{field} = ($mc . $r->{field}) if $mc && $r->{field}; |
671 |
$r->{field} = ($mc . $r->{field}) if $mc && $r->{field}; |
| 630 |
$r; |
672 |
$r->{field} ? $r : undef; |
| 631 |
} @indexes; |
673 |
} @indexes; |
| 632 |
} |
674 |
} |
| 633 |
|
675 |
|
|
Lines 656-663
sub _convert_index_strings {
Link Here
|
| 656 |
push @res, $s; |
698 |
push @res, $s; |
| 657 |
next; |
699 |
next; |
| 658 |
} |
700 |
} |
| 659 |
push @res, $conv->{field} . ":" |
701 |
push @res, ($conv->{field} ? $conv->{field} . ':' : '') |
| 660 |
. $self->_modify_string_by_type( %$conv, operand => $term ); |
702 |
. $self->_modify_string_by_type( %$conv, operand => $term ); |
| 661 |
} |
703 |
} |
| 662 |
return @res; |
704 |
return @res; |
| 663 |
} |
705 |
} |
|
Lines 945-948
sub _split_query {
Link Here
|
| 945 |
return @tokens; |
987 |
return @tokens; |
| 946 |
} |
988 |
} |
| 947 |
|
989 |
|
|
|
990 |
=head2 _search_fields |
| 991 |
my $weighted_fields = $self->_search_fields({ |
| 992 |
is_opac => 0, |
| 993 |
weighted_fields => 1, |
| 994 |
subfield => 'raw' |
| 995 |
}); |
| 996 |
|
| 997 |
Generate a list of searchable fields to be used for Elasticsearch queries |
| 998 |
applied to multiple fields. |
| 999 |
|
| 1000 |
Returns an arrayref of field names for either OPAC or Staff client, with |
| 1001 |
possible weights and subfield appended to each field name depending on the |
| 1002 |
options provided. |
| 1003 |
|
| 1004 |
=over 4 |
| 1005 |
|
| 1006 |
=item C<$params> |
| 1007 |
|
| 1008 |
Hashref with options. The parameter C<is_opac> indicates whether the searchable |
| 1009 |
fields for OPAC or Staff client should be retrieved. If C<weighted_fields> is set |
| 1010 |
fields weights will be applied on returned fields. C<subfield> can be used to |
| 1011 |
provide a subfield that will be appended to fields as "C<field_name>.C<subfield>". |
| 1012 |
|
| 1013 |
=back |
| 1014 |
|
| 1015 |
=cut |
| 1016 |
|
| 1017 |
sub _search_fields { |
| 1018 |
my ($self, $params) = @_; |
| 1019 |
$params //= { |
| 1020 |
is_opac => 0, |
| 1021 |
weighted_fields => 0, |
| 1022 |
# This is a hack for authorities build_authorities_query |
| 1023 |
# can hopefully be removed in the future |
| 1024 |
subfield => undef, |
| 1025 |
}; |
| 1026 |
|
| 1027 |
my $cache = Koha::Caches->get_instance(); |
| 1028 |
my $cache_key = 'elasticsearch_search_fields' . ($params->{is_opac} ? '_opac' : '_staff_client'); |
| 1029 |
my $search_fields = $cache->get_from_cache($cache_key, { unsafe => 1 }); |
| 1030 |
|
| 1031 |
if (!$search_fields) { |
| 1032 |
# The reason we don't use Koha::SearchFields->search here is we don't |
| 1033 |
# want or need resultset wrapped as Koha::SearchField object. |
| 1034 |
# It does not make any sense in this context and would cause |
| 1035 |
# unnecessary overhead sice we are only querying for data |
| 1036 |
# Also would not work, or produce strange results, with the "columns" |
| 1037 |
# option. |
| 1038 |
my $schema = Koha::Database->schema; |
| 1039 |
my $result = $schema->resultset('SearchField')->search( |
| 1040 |
{ |
| 1041 |
$params->{is_opac} ? ( |
| 1042 |
'opac' => 1, |
| 1043 |
) : ( |
| 1044 |
'staff_client' => 1 |
| 1045 |
), |
| 1046 |
'search_marc_map.index_name' => $self->index, |
| 1047 |
'search_marc_map.marc_type' => C4::Context->preference('marcflavour'), |
| 1048 |
'search_marc_to_fields.search' => 1, |
| 1049 |
}, |
| 1050 |
{ |
| 1051 |
columns => [qw/name weight/], |
| 1052 |
collapse => 1, |
| 1053 |
join => {search_marc_to_fields => 'search_marc_map'}, |
| 1054 |
} |
| 1055 |
); |
| 1056 |
my @search_fields; |
| 1057 |
while (my $search_field = $result->next) { |
| 1058 |
push @search_fields, [ |
| 1059 |
$search_field->name, |
| 1060 |
$search_field->weight ? $search_field->weight : () |
| 1061 |
]; |
| 1062 |
} |
| 1063 |
$search_fields = \@search_fields; |
| 1064 |
$cache->set_in_cache($cache_key, $search_fields); |
| 1065 |
} |
| 1066 |
if ($params->{subfield}) { |
| 1067 |
my $subfield = $params->{subfield}; |
| 1068 |
$search_fields = [ |
| 1069 |
map { |
| 1070 |
# Copy values to avoid mutating cached |
| 1071 |
# data (since unsafe is used) |
| 1072 |
my ($field, $weight) = @{$_}; |
| 1073 |
["${field}.${subfield}", $weight]; |
| 1074 |
} @{$search_fields} |
| 1075 |
]; |
| 1076 |
} |
| 1077 |
|
| 1078 |
if ($params->{weighted_fields}) { |
| 1079 |
return [map { join('^', @{$_}) } @{$search_fields}]; |
| 1080 |
} |
| 1081 |
else { |
| 1082 |
# Exclude weight from field |
| 1083 |
return [map { $_->[0] } @{$search_fields}]; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 948 |
1; |
1087 |
1; |