View | Details | Raw Unified | Return to bug 18316
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-6 / +2 lines)
Lines 230-242 sub build_query_compat { Link Here
230
        join( ' ', $self->_create_query_string(@search_params) ) || (),
230
        join( ' ', $self->_create_query_string(@search_params) ) || (),
231
        $self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
231
        $self->_join_queries( $self->_convert_index_strings(@$limits) ) || () );
232
232
233
    my @weights = $params->{weight};
234
    my @w_fields = $params->{w_fields};
235
    my @fields = '_all';
233
    my @fields = '_all';
236
    if ( defined $weights[0] ) {
234
    if ( defined($params->{weighted_fields}) && $params->{weighted_fields} ) {
237
        for (my $i = 0 ; $i < (scalar @weights) ; $i++ ){
235
        push @fields, sprintf("%s^%s", $_->name, $_->weight) for Koha::SearchFields->weighted_fields;
238
            push @fields, "$w_fields[$i]^$weights[$i]";
239
        }
240
    }
236
    }
241
237
242
    # If there's no query on the left, let's remove the junk left behind
238
    # If there's no query on the left, let's remove the junk left behind
(-)a/Koha/SearchField.pm (-19 / +22 lines)
Lines 49-70 sub search_marc_maps { Link Here
49
    my ( $self ) = @_;
49
    my ( $self ) = @_;
50
50
51
    my $marc_type = lc C4::Context->preference('marcflavour');
51
    my $marc_type = lc C4::Context->preference('marcflavour');
52
    my @marc_maps = ();
53
52
54
    my $schema = Koha::Database->new->schema;
53
    my $schema = Koha::Database->new->schema;
55
    my @marc_map_fields = $schema->resultset('SearchMarcToField')->search({
54
    my $marc_map_fields = $schema->resultset('SearchMarcToField')->search(
56
        search_field_id => $self->id
55
        {
57
    });
56
            'me.search_field_id'        => $self->id,
58
57
            'search_marc_map.marc_type' => $marc_type
59
    return @marc_maps unless @marc_map_fields;
58
        },
60
59
        {
61
    foreach my $marc_field ( @marc_map_fields ) {
60
            select => [
62
        my $marc_map = Koha::SearchMarcMaps->find( $marc_field->search_marc_map_id );
61
                'search_marc_map.index_name',
63
        push @marc_maps, $marc_map if $marc_map->marc_type eq $marc_type;
62
                'search_marc_map.marc_type',
64
    }
63
                'search_marc_map.marc_field'
65
64
            ],
66
    return @marc_maps;
65
            as => [ 'index_name', 'marc_type', 'marc_field' ],
67
66
            join => 'search_marc_map'
67
        }
68
    );
69
70
    return $marc_map_fields;
68
}
71
}
69
72
70
=head3 is_mapped_biblios
73
=head3 is_mapped_biblios
Lines 76-86 my $is_mapped_biblios = $search_field->is_mapped_biblios Link Here
76
sub is_mapped_biblios {
79
sub is_mapped_biblios {
77
    my ( $self ) = @_;
80
    my ( $self ) = @_;
78
81
79
    foreach my $marc_map ( $self->search_marc_maps ) {
82
    return $self->search_marc_maps->search(
80
        return 1 if $marc_map->index_name eq 'biblios';
83
        {
81
    }
84
            index_name => 'biblios'
82
85
        }
83
    return 0;
86
    )->count ? 1 : 0;
84
}
87
}
85
88
86
=head3 type
89
=head3 type
(-)a/Koha/SearchFields.pm (-9 / +1 lines)
Lines 44-61 my (@w_fields, @weight) = Koha::SearchFields->weighted_fields(); Link Here
44
sub weighted_fields {
44
sub weighted_fields {
45
    my ($self) = @_;
45
    my ($self) = @_;
46
46
47
    my ($w_fields, $weight) = ([], []);
47
    return $self->search(
48
    my $fields = $self->search(
49
        { weight => { '>' => 0, '!=' => undef } },
48
        { weight => { '>' => 0, '!=' => undef } },
50
        { order_by => { -desc => 'weight' } }
49
        { order_by => { -desc => 'weight' } }
51
    );
50
    );
52
53
    while ( my $field = $fields->next ) {
54
        push @$w_fields, $field->name;
55
        push @$weight, $field->weight;
56
    }
57
58
    return ($w_fields, $weight);
59
}
51
}
60
52
61
=head3 type
53
=head3 type
(-)a/catalogue/search.pl (-3 / +3 lines)
Lines 456-464 my $expanded_facet = $params->{'expand'}; Link Here
456
# Define some global variables
456
# Define some global variables
457
my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type);
457
my ( $error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type);
458
458
459
my ($w_fields, $weight);
459
my $build_params;
460
unless ( $cgi->param('advsearch') ) {
460
unless ( $cgi->param('advsearch') ) {
461
    ($w_fields, $weight) = Koha::SearchFields->weighted_fields();
461
    $build_params->{weighted_fields} = 1;
462
}
462
}
463
463
464
my $builder = Koha::SearchEngine::QueryBuilder->new(
464
my $builder = Koha::SearchEngine::QueryBuilder->new(
Lines 473-479 my $searcher = Koha::SearchEngine::Search->new( Link Here
473
    $query_type
473
    $query_type
474
  )
474
  )
475
  = $builder->build_query_compat( \@operators, \@operands, \@indexes, \@limits,
475
  = $builder->build_query_compat( \@operators, \@operands, \@indexes, \@limits,
476
    \@sort_by, $scan, $lang, { w_fields => @$w_fields, weight => @$weight  } );
476
    \@sort_by, $scan, $lang, $build_params );
477
477
478
## parse the query_cgi string and put it into a form suitable for <input>s
478
## parse the query_cgi string and put it into a form suitable for <input>s
479
my @query_inputs;
479
my @query_inputs;
(-)a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t (-2 / +56 lines)
Lines 17-27 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 2;
20
use C4::Context;
21
22
use Test::More tests => 3;
21
use Test::Exception;
23
use Test::Exception;
24
use t::lib::TestBuilder;
22
25
23
use Koha::Database;
26
use Koha::Database;
24
use Koha::SearchEngine::Elasticsearch::QueryBuilder
27
use Koha::SearchEngine::Elasticsearch::QueryBuilder;
28
29
my $schema = Koha::Database->new->schema;
30
$schema->storage->txn_begin;
25
31
26
subtest 'build_authorities_query_compat() tests' => sub {
32
subtest 'build_authorities_query_compat() tests' => sub {
27
33
Lines 77-79 subtest 'build query from form subtests' => sub { Link Here
77
83
78
84
79
};
85
};
86
87
subtest 'build_query with weighted fields tests' => sub {
88
    plan tests => 4;
89
90
    my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
91
    my $db_builder = t::lib::TestBuilder->new();
92
93
    Koha::SearchFields->search({})->delete;
94
95
    $db_builder->build({
96
        source => 'SearchField',
97
        value => {
98
            name    => 'acqdate',
99
            label   => 'acqdate',
100
            weight  => undef
101
        }
102
    });
103
104
    $db_builder->build({
105
        source => 'SearchField',
106
        value => {
107
            name    => 'title',
108
            label   => 'title',
109
            weight  => 25
110
        }
111
    });
112
113
    $db_builder->build({
114
        source => 'SearchField',
115
        value => {
116
            name    => 'subject',
117
            label   => 'subject',
118
            weight  => 15
119
        }
120
    });
121
122
    my ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
123
    undef, undef, undef, { weighted_fields => 1 });
124
125
    my $fields = $query->{query}{query_string}{fields};
126
127
    is(scalar(@$fields), 3, 'Search is done on 3 fields');
128
    is($fields->[0], '_all', 'First search field is _all');
129
    is($fields->[1], 'title^25', 'Second search field is title');
130
    is($fields->[2], 'subject^15', 'Third search field is subject');
131
};
132
133
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Koha/SearchField.t (-17 / +15 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 16;
22
use Test::More tests => 15;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
use Koha::SearchFields;
25
use Koha::SearchFields;
Lines 73-83 my $smtf2 = $builder->build({ Link Here
73
});
73
});
74
74
75
my $search_field = Koha::SearchFields->find($sf->{id});
75
my $search_field = Koha::SearchFields->find($sf->{id});
76
my @marc_map = $search_field->search_marc_maps;
76
my $marc_maps = $search_field->search_marc_maps;
77
is(scalar(@marc_map), 1, 'search_marc_maps should return 1 marc map');
77
my $marc_map = $marc_maps->next;
78
is($marc_map[0]->index_name, 'biblios', 'Marc map index name is biblios');
78
is($marc_maps->count, 1, 'search_marc_maps should return 1 marc map');
79
is($marc_map[0]->marc_type, 'marc21', 'Marc map type is marc21');
79
is($marc_map->get_column('index_name'), 'biblios', 'Marc map index name is biblios');
80
is($marc_map[0]->marc_field, '410abcdef', 'Marc map field is 410abcdef');
80
is($marc_map->get_column('marc_type'), 'marc21', 'Marc map type is marc21');
81
is($marc_map->get_column('marc_field'), '410abcdef', 'Marc map field is 410abcdef');
81
82
82
ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
83
ok($search_field->is_mapped_biblios, 'Search field 1 is mapped with biblios');
83
84
Lines 188-203 $builder->build({ Link Here
188
    }
189
    }
189
});
190
});
190
191
191
my ($w_fields, $weight) = Koha::SearchFields->weighted_fields();
192
my @w_fields = Koha::SearchFields->weighted_fields();
192
is(scalar(@$w_fields), 3, 'weighted_fields should return 3 weighted fields.');
193
is(scalar(@w_fields), 3, 'weighted_fields should return 3 weighted fields.');
193
is(scalar(@$weight), 3, 'weighted_fields should return 3 weight.');
194
194
195
is($w_fields->[0], 'title', 'First field is title.');
195
is($w_fields[0]->name, 'title', 'First field is title.');
196
is($w_fields->[1], 'subject', 'Second field is subject.');
196
is($w_fields[0]->weight, 25, 'Title weight is 25.');
197
is($w_fields->[2], 'author', 'Third field is author.');
197
is($w_fields[1]->name, 'subject', 'Second field is subject.');
198
198
is($w_fields[1]->weight, 15, 'Subject weight is 15.');
199
is($weight->[0], 25, 'Title weight is 25.');
199
is($w_fields[2]->name, 'author', 'Third field is author.');
200
is($weight->[1], 15, 'Subject weight is 15.');
200
is($w_fields[2]->weight, 5, 'Author weight is 5.');
201
is($weight->[2], 5, 'Author weight is 5.');
202
201
203
$schema->storage->txn_rollback;
202
$schema->storage->txn_rollback;
204
- 

Return to bug 18316