Bug 19502: Retrieve index.max_result_window from ES
[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 => 15;
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     ok( my $marc = $searcher->json2marc( $results->first ), 'Convert JSON to MARC');
54
55     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
56
57     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
58
59     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
60
61     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
62
63     is ($searcher->max_result_window, 10000, 'By default, max_result_window is 10000');
64     $searcher->store->es->indices->put_settings(index => $searcher->store->index_name, body => {
65         'index' => {
66             'max_result_window' => 12000,
67         },
68     });
69     is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
70 }
71
72 subtest 'json2marc' => sub {
73     plan tests => 4;
74     my $leader = '00626nam a2200193   4500';
75     my $_001 = 42;
76     my $_010a = '123456789';
77     my $_010d = 145;
78     my $_200a = 'a title';
79     my $json = [ # It's not a JSON, see the POD of json2marc
80         [ 'LDR', undef, undef, '_', $leader ],
81         [ '001', undef, undef, '_', $_001 ],
82         [ '010', ' ', ' ', 'a', $_010a, 'd', $_010d ],
83         [ '200', '1', ' ', 'a', $_200a, ], # Yes UNIMARC but we don't mind here
84     ];
85
86     my $marc = $searcher->json2marc( $json );
87     is( $marc->leader, $leader, );
88     is( $marc->field('001')->data, $_001, );
89     is( $marc->subfield('010', 'a'), $_010a, );
90     is( $marc->subfield('200', 'a'), $_200a, );
91
92 };
93
94 subtest 'build_query tests' => sub {
95     plan tests => 23;
96
97     t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
98     my $query = $builder->build_query();
99     ok( defined $query->{aggregations}{homebranch},
100         'homebranch added to facets if DisplayLibraryFacets=both' );
101     ok( defined $query->{aggregations}{holdingbranch},
102         'holdingbranch added to facets if DisplayLibraryFacets=both' );
103     t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
104     $query = $builder->build_query();
105     ok( !defined $query->{aggregations}{homebranch},
106         'homebranch not added to facets if DisplayLibraryFacets=holding' );
107     ok( defined $query->{aggregations}{holdingbranch},
108         'holdingbranch added to facets if DisplayLibraryFacets=holding' );
109     t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
110     $query = $builder->build_query();
111     ok( defined $query->{aggregations}{homebranch},
112         'homebranch added to facets if DisplayLibraryFacets=home' );
113     ok( !defined $query->{aggregations}{holdingbranch},
114         'holdingbranch not added to facets if DisplayLibraryFacets=home' );
115
116     t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
117
118     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
119     is(
120         $query->{query}{query_string}{query},
121         "(donald duck)",
122         "query not altered if QueryAutoTruncate disabled"
123     );
124
125     t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
126
127     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
128     is(
129         $query->{query}{query_string}{query},
130         "(donald* duck*)",
131         "simple query is auto truncated when QueryAutoTruncate enabled"
132     );
133
134     # Ensure reserved words are not truncated
135     ( undef, $query ) = $builder->build_query_compat( undef,
136         ['donald or duck and mickey not mouse'] );
137     is(
138         $query->{query}{query_string}{query},
139         "(donald* or duck* and mickey* not mouse*)",
140         "reserved words are not affected by QueryAutoTruncate"
141     );
142
143     ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
144     is(
145         $query->{query}{query_string}{query},
146         "(donald* duck*)",
147         "query with '*' is unaltered when QueryAutoTruncate is enabled"
148     );
149
150     ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
151     is(
152         $query->{query}{query_string}{query},
153         "(donald* duck* and the* mouse*)",
154         "individual words are all truncated and stopwords ignored"
155     );
156
157     ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
158     is(
159         $query->{query}{query_string}{query},
160         "(*)",
161         "query of just '*' is unaltered when QueryAutoTruncate is enabled"
162     );
163
164     ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
165     is(
166         $query->{query}{query_string}{query},
167         '("donald duck")',
168         "query with quotes is unaltered when QueryAutoTruncate is enabled"
169     );
170
171
172     ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
173     is(
174         $query->{query}{query_string}{query},
175         '("donald duck" and "the mouse")',
176         "all quoted strings are unaltered if more than one in query"
177     );
178
179     ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
180     is(
181         $query->{query}{query_string}{query},
182         '(barcode:123456*)',
183         "query of specific field is truncated"
184     );
185
186     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:"123456"'] );
187     is(
188         $query->{query}{query_string}{query},
189         '(Local-number:"123456")',
190         "query of specific field including hyphen and quoted is not truncated"
191     );
192
193     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:123456'] );
194     is(
195         $query->{query}{query_string}{query},
196         '(Local-number:123456*)',
197         "query of specific field including hyphen and not quoted is truncated"
198     );
199
200     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:123456'] );
201     is(
202         $query->{query}{query_string}{query},
203         '(Local-number.raw:123456*)',
204         "query of specific field including period and not quoted is truncated"
205     );
206
207     ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:"123456"'] );
208     is(
209         $query->{query}{query_string}{query},
210         '(Local-number.raw:"123456")',
211         "query of specific field including period and quoted is not truncated"
212     );
213
214     ( undef, $query ) = $builder->build_query_compat( undef, ['J.R.R'] );
215     is(
216         $query->{query}{query_string}{query},
217         '(J.R.R*)',
218         "query including period is truncated but not split at periods"
219     );
220
221     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
222     is(
223         $query->{query}{query_string}{query},
224         '(title:"donald duck")',
225         "query of specific field is not truncated when surrouned by quotes"
226     );
227
228     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
229     is(
230         $query->{query}{query_string}{query},
231         '(title:"donald duck") AND suppress:0',
232         "query of specific field is added AND suppress:0"
233     );
234
235     ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
236     is(
237         $query->{query}{query_string}{query},
238         '(title:"donald duck")',
239         "query of specific field is not added AND suppress:0"
240     );
241 };
242
243 subtest "_convert_sort_fields" => sub {
244     plan tests => 2;
245     my @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_dsc ));
246     is_deeply(
247         \@sort_by,
248         [
249             { field => 'callnum', direction => 'asc' },
250             { field => 'author',  direction => 'desc' }
251         ],
252         'sort fields should have been split correctly'
253     );
254
255     # We could expect this to pass, but direction is undef instead of 'desc'
256     @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_desc ));
257     is_deeply(
258         \@sort_by,
259         [
260             { field => 'callnum', direction => 'asc' },
261             { field => 'author',  direction => 'desc' }
262         ],
263         'sort fields should have been split correctly'
264     );
265 };