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

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (-6 / +38 lines)
Lines 200-216 sub build_query { Link Here
200
    if ($options{whole_record}) {
200
    if ($options{whole_record}) {
201
        push @$fields, 'marc_data_array.*';
201
        push @$fields, 'marc_data_array.*';
202
    }
202
    }
203
    $res->{query} = {
203
    my $query_string = {
204
        query_string => {
205
            query            => $query,
204
            query            => $query,
206
            fuzziness        => $fuzzy_enabled ? 'auto' : '0',
205
            fuzziness        => $fuzzy_enabled ? 'auto' : '0',
207
            default_operator => 'AND',
206
            default_operator => 'AND',
208
            fields           => $fields,
207
            fields           => $fields,
209
            lenient          => JSON::true,
208
            lenient          => JSON::true,
210
            analyze_wildcard => JSON::true,
209
            analyze_wildcard => JSON::true,
211
        }
210
        };
212
    };
211
    $query_string->{type} = 'cross_fields' if C4::Context->preference('ElasticsearchCrossFields');
213
    $res->{query}->{query_string}->{type} = 'cross_fields' if C4::Context->preference('ElasticsearchCrossFields');
212
213
    $res->{query} = { bool => { must => [ { query_string => $query_string } ] } };
214
215
    $res->{query}->{bool}->{should} = $options{field_match_boost_query} if $options{field_match_boost_query};
214
216
215
    if ( $options{sort} ) {
217
    if ( $options{sort} ) {
216
        foreach my $sort ( @{ $options{sort} } ) {
218
        foreach my $sort ( @{ $options{sort} } ) {
Lines 262-267 sub build_query_compat { Link Here
262
        $lang, $params )
264
        $lang, $params )
263
      = @_;
265
      = @_;
264
266
267
265
    my $query;
268
    my $query;
266
    my $query_str = '';
269
    my $query_str = '';
267
    my $search_param_query_str = '';
270
    my $search_param_query_str = '';
Lines 272-278 sub build_query_compat { Link Here
272
    } else {
275
    } else {
273
        my @sort_params  = $self->_convert_sort_fields(@$sort_by);
276
        my @sort_params  = $self->_convert_sort_fields(@$sort_by);
274
        my @index_params = $self->_convert_index_fields(@$indexes);
277
        my @index_params = $self->_convert_index_fields(@$indexes);
275
        $limits       = $self->_fix_limit_special_cases($orig_limits);
278
        my $field_match_boost_query =
279
            C4::Context->preference('ESBoostFieldMatch')
280
            ? $self->_build_field_match_boost_query( { operands => $operands, indexes => \@index_params } )
281
            : [];
282
        $limits = $self->_fix_limit_special_cases($orig_limits);
276
        if ( $params->{suppress} ) { push @$limits, "suppress:false"; }
283
        if ( $params->{suppress} ) { push @$limits, "suppress:false"; }
277
        # Merge the indexes in with the search terms and the operands so that
284
        # Merge the indexes in with the search terms and the operands so that
278
        # each search thing is a handy unit.
285
        # each search thing is a handy unit.
Lines 314-319 sub build_query_compat { Link Here
314
        $options{is_opac} = $params->{is_opac};
321
        $options{is_opac} = $params->{is_opac};
315
        $options{weighted_fields} = $params->{weighted_fields};
322
        $options{weighted_fields} = $params->{weighted_fields};
316
        $options{whole_record} = $params->{whole_record};
323
        $options{whole_record} = $params->{whole_record};
324
        $options{field_match_boost_query} = $field_match_boost_query if @$field_match_boost_query;
317
        $query = $self->build_query( $query_str, %options );
325
        $query = $self->build_query( $query_str, %options );
318
    }
326
    }
319
327
Lines 349-354 sub build_query_compat { Link Here
349
    );
357
    );
350
}
358
}
351
359
360
=head2 _build_field_match_boost_query
361
362
    my ($query, $query_str) = $builder->_build_field_match_boost_query({ operands => \@operands, indexes => \@indexes)
363
364
This will build an array of match queries for terms and indexes passed in and return a reference to the array.
365
366
=cut
367
368
sub _build_field_match_boost_query {
369
    my ( $self, $params ) = @_;
370
    my $indexes  = $params->{indexes};
371
    my $operands = $params->{operands};
372
373
    my @boost_query;
374
    my $ea = each_array( @$operands, @$indexes );
375
    while ( my ( $operand, $index ) = $ea->() ) {
376
        next unless $operand;
377
        $index = $index->{field} if ref $index eq 'HASH';
378
        $index = 'title-cover'   if ( !$index || $index eq 'kw' || $index eq 'ti' || $index eq 'title' );
379
        push @boost_query, { match => { $index => { query => $operand } } };
380
    }
381
    return \@boost_query;
382
}
383
352
=head2 build_authorities_query
384
=head2 build_authorities_query
353
385
354
    my $query = $builder->build_authorities_query(\%search);
386
    my $query = $builder->build_authorities_query(\%search);
(-)a/installer/data/mysql/atomicupdate/bug38694.pl (+21 lines)
Line 0 Link Here
1
use Modern::Perl;
2
use Koha::Installer::Output qw(say_warning say_success say_info);
3
4
return {
5
    bug_number  => "38694",
6
    description => "Add ESBoostFieldMatch system preference",
7
    up          => sub {
8
        my ($args) = @_;
9
        my ( $dbh, $out ) = @$args{qw(dbh out)};
10
11
        # Do you stuffs here
12
        $dbh->do(
13
            q{
14
           INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
15
           ('ESBoostFieldMatch', '0', NULL, 'Add a "match" query to es when searching, will follow indexes chosen in advanced search, or use title-cover for generic keyword or title index search', 'YesNo')
16
       }
17
        );
18
19
        say_success( $out, "Added new system preference 'ESBoostFieldMatch'" );
20
    },
21
};
(-)a/installer/data/mysql/mandatory/sysprefs.sql (+1 lines)
Lines 258-263 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
258
('ERMProviderEbscoApiKey', '', '', 'API key for EBSCO', 'free'),
258
('ERMProviderEbscoApiKey', '', '', 'API key for EBSCO', 'free'),
259
('ERMProviderEbscoCustomerID', '', '', 'Customer ID for EBSCO', 'free'),
259
('ERMProviderEbscoCustomerID', '', '', 'Customer ID for EBSCO', 'free'),
260
('ERMProviders', 'local', 'local|ebsco', 'Set the providers for the ERM module', 'Choice'),
260
('ERMProviders', 'local', 'local|ebsco', 'Set the providers for the ERM module', 'Choice'),
261
('ESBoostFieldMatch', '0', NULL, 'Add a "match" query to es when searching, will follow indexes chosen in advanced search, or use title-cover for generic keyword or title index search', 'YesNo'),
261
('ESPreventAutoTruncate', 'barcode|control-number|control-number-identifier|date-of-acquisition|date-of-publication|date-time-last-modified|identifier-standard|isbn|issn|itype|lc-card-number|number-local-acquisition|other-control-number|record-control-number', NULL, 'List of searchfields (separated by | or ,) that should not be autotruncated by Elasticsearch even if QueryAutoTruncate is set to Yes', 'free'),
262
('ESPreventAutoTruncate', 'barcode|control-number|control-number-identifier|date-of-acquisition|date-of-publication|date-time-last-modified|identifier-standard|isbn|issn|itype|lc-card-number|number-local-acquisition|other-control-number|record-control-number', NULL, 'List of searchfields (separated by | or ,) that should not be autotruncated by Elasticsearch even if QueryAutoTruncate is set to Yes', 'free'),
262
('ExcludeHolidaysFromMaxPickUpDelay', '0', NULL, 'If ON, reserves max pickup delay takes into accountthe closed days.', 'YesNo'),
263
('ExcludeHolidaysFromMaxPickUpDelay', '0', NULL, 'If ON, reserves max pickup delay takes into accountthe closed days.', 'YesNo'),
263
('expandedSearchOption','0',NULL,'If ON, set advanced search to be expanded by default','YesNo'),
264
('expandedSearchOption','0',NULL,'If ON, set advanced search to be expanded by default','YesNo'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref (-1 / +8 lines)
Lines 95-100 Searching: Link Here
95
                  0: Disable
95
                  0: Disable
96
            - "the cross_fields option for Elasticsearch searches, supported in Elasticsearch 6.X and above."
96
            - "the cross_fields option for Elasticsearch searches, supported in Elasticsearch 6.X and above."
97
            - "See documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-cross-fields"
97
            - "See documentation at https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-cross-fields"
98
        -
99
            - pref: ESBoostFieldMatch
100
              default: 0
101
              choices:
102
                  1: Enable
103
                  0: Disable
104
            - "adding a second 'match' field search to the ES query to boost relevancy. Keyword and title fields will be boosted with title-cover, other fields will boost directly."
105
            - "This will not boost CCL style searches, only standard or advanced searches"
98
        -
106
        -
99
            - pref: SavedSearchFilters
107
            - pref: SavedSearchFilters
100
              default: 0
108
              default: 0
101
- 

Return to bug 38694