3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 2;
23 use Test::More tests => 3;
26 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
28 my $schema = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
31 subtest 'build_authorities_query_compat() tests' => sub {
37 $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
38 'Creating new query builder object for authorities'
41 my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
42 my $search_term = 'a';
43 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
44 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
45 if ( $koha_name eq 'all' ) {
46 is( $query->{query}->{bool}->{must}[0]->{wildcard}->{"_all.phrase"},
49 is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
54 $search_term = 'Donald Duck';
55 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
56 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
57 if ( $koha_name eq 'all' ) {
58 is( $query->{query}->{bool}->{must}[0]->{wildcard}->{"_all.phrase"},
60 is( $query->{query}->{bool}->{must}[1]->{wildcard}->{"_all.phrase"},
63 is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
65 is( $query->{query}->{bool}->{must}[1]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
70 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
71 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['is'], [$search_term], 'AUTH_TYPE', 'asc' );
72 if ( $koha_name eq 'all' ) {
73 is( $query->{query}->{bool}->{must}[0]->{term}->{"_all.phrase"},
76 is( $query->{query}->{bool}->{must}[0]->{term}->{$koha_to_index_name->{$koha_name}.".phrase"},
81 foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
82 my $query = $qb->build_authorities_query_compat( [ $koha_name ], undef, undef, ['start'], [$search_term], 'AUTH_TYPE', 'asc' );
83 if ( $koha_name eq 'all' ) {
84 is( $query->{query}->{bool}->{must}[0]->{prefix}->{"_all.lc_raw"},
87 is( $query->{query}->{bool}->{must}[0]->{prefix}->{$koha_to_index_name->{$koha_name}.".lc_raw"},
94 $qb->build_authorities_query_compat( [ 'tomas' ], undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
96 'Koha::Exceptions::WrongParameter',
97 'Exception thrown on invalid value in the marclist param';
100 subtest 'build query from form subtests' => sub {
103 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
104 #when searching for authorities from a record the form returns marclist with blanks for unentered terms
105 my @marclist = ('mainmainentry','mainentry','match', 'all');
106 my @values = ( undef, 'Hamilton', undef, undef);
107 my @operator = ( 'contains', 'contains', 'contains', 'contains');
109 my $query = $builder->build_authorities_query_compat( \@marclist, undef,
110 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
111 is($query->{query}->{bool}->{must}[0]->{wildcard}->{'Heading.phrase'}, "*hamilton*","Expected search is populated");
112 is( scalar @{ $query->{query}->{bool}->{must} }, 1,"Only defined search is populated");
114 @values[2] = 'Jefferson';
115 $query = $builder->build_authorities_query_compat( \@marclist, undef,
116 undef, \@operator , \@values, 'AUTH_TYPE', 'asc' );
117 is($query->{query}->{bool}->{must}[0]->{wildcard}->{'Heading.phrase'}, "*hamilton*","First index searched as expected");
118 is($query->{query}->{bool}->{must}[1]->{wildcard}->{'Match.phrase'}, "*jefferson*","Second index searched when populated");
119 is( scalar @{ $query->{query}->{bool}->{must} }, 2,"Only defined searches are populated");
124 subtest 'build_query with weighted fields tests' => sub {
127 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
128 my $db_builder = t::lib::TestBuilder->new();
130 Koha::SearchFields->search({})->delete;
133 source => 'SearchField',
142 source => 'SearchField',
151 source => 'SearchField',
159 my ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
160 undef, undef, undef, { weighted_fields => 1 });
162 my $fields = $query->{query}{query_string}{fields};
164 is(scalar(@$fields), 3, 'Search is done on 3 fields');
165 is($fields->[0], '_all', 'First search field is _all');
166 is($fields->[1], 'title^25', 'Second search field is title');
167 is($fields->[2], 'subject^15', 'Third search field is subject');
170 $schema->storage->txn_rollback;