join( ' ', $self->_create_query_string(@search_params) ) || (),
$self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
- my @weights = $params->{weight};
- my @w_fields = $params->{w_fields};
my @fields = '_all';
- if ( defined $weights[0] ) {
- for (my $i = 0 ; $i < (scalar @weights) ; $i++ ){
- push @fields, "$w_fields[$i]^$weights[$i]";
- }
+ if ( defined($params->{weighted_fields}) && $params->{weighted_fields} ) {
+ push @fields, sprintf("%s^%s", $_->name, $_->weight) for Koha::SearchFields->weighted_fields;
}
# If there's no query on the left, let's remove the junk left behind
my ( $self ) = @_;
my $marc_type = lc C4::Context->preference('marcflavour');
- my @marc_maps = ();
my $schema = Koha::Database->new->schema;
- my @marc_map_fields = $schema->resultset('SearchMarcToField')->search({
- search_field_id => $self->id
- });
-
- return @marc_maps unless @marc_map_fields;
-
- foreach my $marc_field ( @marc_map_fields ) {
- my $marc_map = Koha::SearchMarcMaps->find( $marc_field->search_marc_map_id );
- push @marc_maps, $marc_map if $marc_map->marc_type eq $marc_type;
- }
-
- return @marc_maps;
-
+ my $marc_map_fields = $schema->resultset('SearchMarcToField')->search(
+ {
+ 'me.search_field_id' => $self->id,
+ 'search_marc_map.marc_type' => $marc_type
+ },
+ {
+ select => [
+ 'search_marc_map.index_name',
+ 'search_marc_map.marc_type',
+ 'search_marc_map.marc_field'
+ ],
+ as => [ 'index_name', 'marc_type', 'marc_field' ],
+ join => 'search_marc_map'
+ }
+ );
+
+ return $marc_map_fields;
}
=head3 is_mapped_biblios
sub is_mapped_biblios {
my ( $self ) = @_;
- foreach my $marc_map ( $self->search_marc_maps ) {
- return 1 if $marc_map->index_name eq 'biblios';
- }
-
- return 0;
+ return $self->search_marc_maps->search(
+ {
+ index_name => 'biblios'
+ }
+ )->count ? 1 : 0;
}
=head3 type
sub weighted_fields {
my ($self) = @_;
- my ($w_fields, $weight) = ([], []);
- my $fields = $self->search(
+ return $self->search(
{ weight => { '>' => 0, '!=' => undef } },
{ order_by => { -desc => 'weight' } }
);
-
- while ( my $field = $fields->next ) {
- push @$w_fields, $field->name;
- push @$weight, $field->weight;
- }
-
- return ($w_fields, $weight);
}
=head3 type
# Define some global variables
my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type);
-my ($w_fields, $weight);
+my $build_params;
unless ( $cgi->param('advsearch') ) {
- ($w_fields, $weight) = Koha::SearchFields->weighted_fields();
+ $build_params->{weighted_fields} = 1;
}
my $builder = Koha::SearchEngine::QueryBuilder->new(
$query_type
)
= $builder->build_query_compat( \@operators, \@operands, \@indexes, \@limits,
- \@sort_by, $scan, $lang, { w_fields => @$w_fields, weight => @$weight } );
+ \@sort_by, $scan, $lang, $build_params );
## parse the query_cgi string and put it into a form suitable for <input>s
my @query_inputs;
use Modern::Perl;
use Test::More tests => 2;
+use C4::Context;
use Test::Exception;
+use Test::More tests => 3;
use Koha::Database;
-use Koha::SearchEngine::Elasticsearch::QueryBuilder
+use Koha::SearchEngine::Elasticsearch::QueryBuilder;
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
subtest 'build_authorities_query_compat() tests' => sub {
plan tests => 37;
};
+
+subtest 'build_query with weighted fields tests' => sub {
+ plan tests => 4;
+
+ my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
+ my $db_builder = t::lib::TestBuilder->new();
+
+ Koha::SearchFields->search({})->delete;
+
+ $db_builder->build({
+ source => 'SearchField',
+ value => {
+ name => 'acqdate',
+ label => 'acqdate',
+ weight => undef
+ }
+ });
+
+ $db_builder->build({
+ source => 'SearchField',
+ value => {
+ name => 'title',
+ label => 'title',
+ weight => 25
+ }
+ });
+
+ $db_builder->build({
+ source => 'SearchField',
+ value => {
+ name => 'subject',
+ label => 'subject',
+ weight => 15
+ }
+ });
+
+ my ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
+ undef, undef, undef, { weighted_fields => 1 });
+
+ my $fields = $query->{query}{query_string}{fields};
+
+ is(scalar(@$fields), 3, 'Search is done on 3 fields');
+ is($fields->[0], '_all', 'First search field is _all');
+ is($fields->[1], 'title^25', 'Second search field is title');
+ is($fields->[2], 'subject^15', 'Third search field is subject');
+};
+
+$schema->storage->txn_rollback;
use Modern::Perl;
-use Test::More tests => 16;
+use Test::More tests => 15;
use Koha::Database;
use Koha::SearchFields;
});
my $search_field = Koha::SearchFields->find($sf->{id});
-my @marc_map = $search_field->search_marc_maps;
-is(scalar(@marc_map), 1, 'search_marc_maps should return 1 marc map');
-is($marc_map[0]->index_name, 'biblios', 'Marc map index name is biblios');
-is($marc_map[0]->marc_type, 'marc21', 'Marc map type is marc21');
-is($marc_map[0]->marc_field, '410abcdef', 'Marc map field is 410abcdef');
+my $marc_maps = $search_field->search_marc_maps;
+my $marc_map = $marc_maps->next;
+is($marc_maps->count, 1, 'search_marc_maps should return 1 marc map');
+is($marc_map->get_column('index_name'), 'biblios', 'Marc map index name is biblios');
+is($marc_map->get_column('marc_type'), 'marc21', 'Marc map type is marc21');
+is($marc_map->get_column('marc_field'), '410abcdef', 'Marc map field is 410abcdef');
ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
}
});
-my ($w_fields, $weight) = Koha::SearchFields->weighted_fields();
-is(scalar(@$w_fields), 3, 'weighted_fields should return 3 weighted fields.');
-is(scalar(@$weight), 3, 'weighted_fields should return 3 weight.');
+my @w_fields = Koha::SearchFields->weighted_fields();
+is(scalar(@w_fields), 3, 'weighted_fields should return 3 weighted fields.');
-is($w_fields->[0], 'title', 'First field is title.');
-is($w_fields->[1], 'subject', 'Second field is subject.');
-is($w_fields->[2], 'author', 'Third field is author.');
-
-is($weight->[0], 25, 'Title weight is 25.');
-is($weight->[1], 15, 'Subject weight is 15.');
-is($weight->[2], 5, 'Author weight is 5.');
+is($w_fields[0]->name, 'title', 'First field is title.');
+is($w_fields[0]->weight, 25, 'Title weight is 25.');
+is($w_fields[1]->name, 'subject', 'Second field is subject.');
+is($w_fields[1]->weight, 15, 'Subject weight is 15.');
+is($w_fields[2]->name, 'author', 'Third field is author.');
+is($w_fields[2]->weight, 5, 'Author weight is 5.');
$schema->storage->txn_rollback;
\ No newline at end of file