1 # Copyright 2015 Catalyst IT
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 => 15;
23 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
24 use Koha::SearchEngine::Elasticsearch::Indexer;
27 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
29 use_ok('Koha::SearchEngine::Elasticsearch::Search');
32 my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
33 { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
35 'Creating a Koha::SearchEngine::Elasticsearch::Search object'
38 is( $searcher->index, 'mydb', 'Testing basic accessor' );
40 ok( my $query = $builder->build_query('easy'), 'Build a search query');
44 eval { $builder->get_elasticsearch_params; };
46 skip 'ElasticSeatch configuration not available', 8
49 Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'mydb' })->drop_index;
51 ok( my $results = $searcher->search( $query) , 'Do a search ' );
53 ok( my $marc = $searcher->json2marc( $results->first ), 'Convert JSON to MARC');
55 is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
57 ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
59 ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
61 is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
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 => {
66 'max_result_window' => 12000,
69 is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
72 subtest 'json2marc' => sub {
74 my $leader = '00626nam a2200193 4500';
76 my $_010a = '123456789';
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
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, );
94 subtest 'build_query tests' => sub {
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' );
116 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
118 ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
120 $query->{query}{query_string}{query},
122 "query not altered if QueryAutoTruncate disabled"
125 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
127 ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
129 $query->{query}{query_string}{query},
131 "simple query is auto truncated when QueryAutoTruncate enabled"
134 # Ensure reserved words are not truncated
135 ( undef, $query ) = $builder->build_query_compat( undef,
136 ['donald or duck and mickey not mouse'] );
138 $query->{query}{query_string}{query},
139 "(donald* or duck* and mickey* not mouse*)",
140 "reserved words are not affected by QueryAutoTruncate"
143 ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
145 $query->{query}{query_string}{query},
147 "query with '*' is unaltered when QueryAutoTruncate is enabled"
150 ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
152 $query->{query}{query_string}{query},
153 "(donald* duck* and the* mouse*)",
154 "individual words are all truncated and stopwords ignored"
157 ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
159 $query->{query}{query_string}{query},
161 "query of just '*' is unaltered when QueryAutoTruncate is enabled"
164 ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
166 $query->{query}{query_string}{query},
168 "query with quotes is unaltered when QueryAutoTruncate is enabled"
172 ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
174 $query->{query}{query_string}{query},
175 '("donald duck" and "the mouse")',
176 "all quoted strings are unaltered if more than one in query"
179 ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
181 $query->{query}{query_string}{query},
183 "query of specific field is truncated"
186 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:"123456"'] );
188 $query->{query}{query_string}{query},
189 '(Local-number:"123456")',
190 "query of specific field including hyphen and quoted is not truncated"
193 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:123456'] );
195 $query->{query}{query_string}{query},
196 '(Local-number:123456*)',
197 "query of specific field including hyphen and not quoted is truncated"
200 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:123456'] );
202 $query->{query}{query_string}{query},
203 '(Local-number.raw:123456*)',
204 "query of specific field including period and not quoted is truncated"
207 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:"123456"'] );
209 $query->{query}{query_string}{query},
210 '(Local-number.raw:"123456")',
211 "query of specific field including period and quoted is not truncated"
214 ( undef, $query ) = $builder->build_query_compat( undef, ['J.R.R'] );
216 $query->{query}{query_string}{query},
218 "query including period is truncated but not split at periods"
221 ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
223 $query->{query}{query_string}{query},
224 '(title:"donald duck")',
225 "query of specific field is not truncated when surrouned by quotes"
228 ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
230 $query->{query}{query_string}{query},
231 '(title:"donald duck") AND suppress:0',
232 "query of specific field is added AND suppress:0"
235 my ($simple_query, $query_cgi);
236 ( undef, $query, $simple_query, $query_cgi ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 0 } );
238 $query->{query}{query_string}{query},
239 '(title:"donald duck")',
240 "query of specific field is not added AND suppress:0"
242 is($query_cgi, 'q=title%3A%22donald%20duck%22', 'query cgi');
245 subtest "_convert_sort_fields" => sub {
247 my @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_dsc ));
251 { field => 'callnum', direction => 'asc' },
252 { field => 'author', direction => 'desc' }
254 'sort fields should have been split correctly'
257 # We could expect this to pass, but direction is undef instead of 'desc'
258 @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_desc ));
262 { field => 'callnum', direction => 'asc' },
263 { field => 'author', direction => 'desc' }
265 'sort fields should have been split correctly'