Bug 18677: Remove new issue_id param from charlostitem
[koha.git] / t / db_dependent / Koha_SearchEngine_Elasticsearch_Search.t
1 # Copyright 2015 Catalyst IT
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 => 13;
21 use t::lib::Mocks;
22
23 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
24 use Koha::SearchEngine::Elasticsearch::Indexer;
25
26
27 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
28
29 use_ok('Koha::SearchEngine::Elasticsearch::Search');
30
31 ok(
32     my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
33         { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
34     ),
35     'Creating a Koha::SearchEngine::Elasticsearch::Search object'
36 );
37
38 is( $searcher->index, 'mydb', 'Testing basic accessor' );
39
40 ok( my $query = $builder->build_query('easy'), 'Build a search query');
41
42 SKIP: {
43
44     eval { $builder->get_elasticsearch_params; };
45
46     skip 'ElasticSeatch configuration not available', 8
47         if $@;
48
49     Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'mydb' })->drop_index;
50
51     ok( my $results = $searcher->search( $query) , 'Do a search ' );
52
53     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
54
55     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
56
57     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
58
59     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
60
61     is ($searcher->max_result_window, 10000, 'By default, max_result_window is 10000');
62     $searcher->store->es->indices->put_settings(index => $searcher->store->index_name, body => {
63         'index' => {
64             'max_result_window' => 12000,
65         },
66     });
67     is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
68 }
69
70 subtest 'build_query tests' => sub {
71     plan tests => 24;
72
73     t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
74     my $query = $builder->build_query();
75     ok( defined $query->{aggregations}{homebranch},
76         'homebranch added to facets if DisplayLibraryFacets=both' );
77     ok( defined $query->{aggregations}{holdingbranch},
78         'holdingbranch added to facets if DisplayLibraryFacets=both' );
79     t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
80     $query = $builder->build_query();
81     ok( !defined $query->{aggregations}{homebranch},
82         'homebranch not added to facets if DisplayLibraryFacets=holding' );
83     ok( defined $query->{aggregations}{holdingbranch},
84         'holdingbranch added to facets if DisplayLibraryFacets=holding' );
85     t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
86     $query = $builder->build_query();
87     ok( defined $query->{aggregations}{homebranch},
88         'homebranch added to facets if DisplayLibraryFacets=home' );
89     ok( !defined $query->{aggregations}{holdingbranch},
90         'holdingbranch not added to facets if DisplayLibraryFacets=home' );
91
92     t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
93
94     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
95     is(
96         $query->{query}{query_string}{query},
97         "(donald duck)",
98         "query not altered if QueryAutoTruncate disabled"
99     );
100
101     t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
102
103     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
104     is(
105         $query->{query}{query_string}{query},
106         "(donald* duck*)",
107         "simple query is auto truncated when QueryAutoTruncate enabled"
108     );
109
110     # Ensure reserved words are not truncated
111     ( undef, $query ) = $builder->build_query_compat( undef,
112         ['donald or duck and mickey not mouse'] );
113     is(
114         $query->{query}{query_string}{query},
115         "(donald* or duck* and mickey* not mouse*)",
116         "reserved words are not affected by QueryAutoTruncate"
117     );
118
119     ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
120     is(
121         $query->{query}{query_string}{query},
122         "(donald* duck*)",
123         "query with '*' is unaltered when QueryAutoTruncate is enabled"
124     );
125
126     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
127     is(
128         $query->{query}{query_string}{query},
129         "(donald* duck* and the* mouse*)",
130         "individual words are all truncated and stopwords ignored"
131     );
132
133     ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
134     is(
135         $query->{query}{query_string}{query},
136         "(*)",
137         "query of just '*' is unaltered when QueryAutoTruncate is enabled"
138     );
139
140     ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
141     is(
142         $query->{query}{query_string}{query},
143         '("donald duck")',
144         "query with quotes is unaltered when QueryAutoTruncate is enabled"
145     );
146
147
148     ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
149     is(
150         $query->{query}{query_string}{query},
151         '("donald duck" and "the mouse")',
152         "all quoted strings are unaltered if more than one in query"
153     );
154
155     ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
156     is(
157         $query->{query}{query_string}{query},
158         '(barcode:123456*)',
159         "query of specific field is truncated"
160     );
161
162     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:"123456"'] );
163     is(
164         $query->{query}{query_string}{query},
165         '(Local-number:"123456")',
166         "query of specific field including hyphen and quoted is not truncated"
167     );
168
169     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:123456'] );
170     is(
171         $query->{query}{query_string}{query},
172         '(Local-number:123456*)',
173         "query of specific field including hyphen and not quoted is truncated"
174     );
175
176     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:123456'] );
177     is(
178         $query->{query}{query_string}{query},
179         '(Local-number.raw:123456*)',
180         "query of specific field including period and not quoted is truncated"
181     );
182
183     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:"123456"'] );
184     is(
185         $query->{query}{query_string}{query},
186         '(Local-number.raw:"123456")',
187         "query of specific field including period and quoted is not truncated"
188     );
189
190     ( undef, $query ) = $builder->build_query_compat( undef, ['J.R.R'] );
191     is(
192         $query->{query}{query_string}{query},
193         '(J.R.R*)',
194         "query including period is truncated but not split at periods"
195     );
196
197     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
198     is(
199         $query->{query}{query_string}{query},
200         '(title:"donald duck")',
201         "query of specific field is not truncated when surrouned by quotes"
202     );
203
204     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
205     is(
206         $query->{query}{query_string}{query},
207         '(title:"donald duck") AND suppress:0',
208         "query of specific field is added AND suppress:0"
209     );
210
211     my ($simple_query, $query_cgi);
212     ( undef, $query, $simple_query, $query_cgi ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
213     is(
214         $query->{query}{query_string}{query},
215         '(title:"donald duck")',
216         "query of specific field is not added AND suppress:0"
217     );
218     is($query_cgi, 'q=title%3A%22donald%20duck%22', 'query cgi');
219 };
220
221 subtest "_convert_sort_fields" => sub {
222     plan tests => 2;
223     my @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_dsc ));
224     is_deeply(
225         \@sort_by,
226         [
227             { field => 'callnum', direction => 'asc' },
228             { field => 'author',  direction => 'desc' }
229         ],
230         'sort fields should have been split correctly'
231     );
232
233     # We could expect this to pass, but direction is undef instead of 'desc'
234     @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_desc ));
235     is_deeply(
236         \@sort_by,
237         [
238             { field => 'callnum', direction => 'asc' },
239             { field => 'author',  direction => 'desc' }
240         ],
241         'sort fields should have been split correctly'
242     );
243 };