Bug 18316: (follow-up) Koha::SearchField::search_marc_maps return a result set -...
[koha.git] / t / db_dependent / Koha / SearchEngine / Elasticsearch / QueryBuilder.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use C4::Context;
22 use Test::Exception;
23 use Test::More tests => 3;
24
25 use Koha::Database;
26 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
27
28 my $schema = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
30
31 subtest 'build_authorities_query_compat() tests' => sub {
32     plan tests => 37;
33
34     my $qb;
35
36     ok(
37         $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
38         'Creating new query builder object for authorities'
39     );
40
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"},
47                 "*a*");
48         } else {
49             is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
50                 "*a*");
51         }
52     }
53
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"},
59                 "*donald*");
60             is( $query->{query}->{bool}->{must}[1]->{wildcard}->{"_all.phrase"},
61                 "*duck*");
62         } else {
63             is( $query->{query}->{bool}->{must}[0]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
64                 "*donald*");
65             is( $query->{query}->{bool}->{must}[1]->{wildcard}->{$koha_to_index_name->{$koha_name}.".phrase"},
66                 "*duck*");
67         }
68     }
69
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"},
74                 "donald duck");
75         } else {
76             is( $query->{query}->{bool}->{must}[0]->{term}->{$koha_to_index_name->{$koha_name}.".phrase"},
77                 "donald duck");
78         }
79     }
80
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"},
85                 "donald duck");
86         } else {
87             is( $query->{query}->{bool}->{must}[0]->{prefix}->{$koha_to_index_name->{$koha_name}.".lc_raw"},
88                 "donald duck");
89         }
90     }
91
92     # Failing case
93     throws_ok {
94         $qb->build_authorities_query_compat( [ 'tomas' ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
95     }
96     'Koha::Exceptions::WrongParameter',
97         'Exception thrown on invalid value in the marclist param';
98 };
99
100 subtest 'build query from form subtests' => sub {
101     plan tests => 5;
102
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');
108
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");
113
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");
120
121
122 };
123
124 subtest 'build_query with weighted fields tests' => sub {
125     plan tests => 4;
126
127     my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
128     my $db_builder = t::lib::TestBuilder->new();
129
130     Koha::SearchFields->search({})->delete;
131
132     $db_builder->build({
133         source => 'SearchField',
134         value => {
135             name    => 'acqdate',
136             label   => 'acqdate',
137             weight  => undef
138         }
139     });
140
141     $db_builder->build({
142         source => 'SearchField',
143         value => {
144             name    => 'title',
145             label   => 'title',
146             weight  => 25
147         }
148     });
149
150     $db_builder->build({
151         source => 'SearchField',
152         value => {
153             name    => 'subject',
154             label   => 'subject',
155             weight  => 15
156         }
157     });
158
159     my ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef,
160     undef, undef, undef, { weighted_fields => 1 });
161
162     my $fields = $query->{query}{query_string}{fields};
163
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');
168 };
169
170 $schema->storage->txn_rollback;