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 => 13;
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 is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
55 ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
57 ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
59 is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
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 => {
64 'max_result_window' => 12000,
67 is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
70 subtest 'build_query tests' => sub {
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' );
92 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '' );
94 ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
96 $query->{query}{query_string}{query},
98 "query not altered if QueryAutoTruncate disabled"
101 t::lib::Mocks::mock_preference( 'QueryAutoTruncate', '1' );
103 ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck'] );
105 $query->{query}{query_string}{query},
107 "simple query is auto truncated when QueryAutoTruncate enabled"
110 # Ensure reserved words are not truncated
111 ( undef, $query ) = $builder->build_query_compat( undef,
112 ['donald or duck and mickey not mouse'] );
114 $query->{query}{query_string}{query},
115 "(donald* or duck* and mickey* not mouse*)",
116 "reserved words are not affected by QueryAutoTruncate"
119 ( undef, $query ) = $builder->build_query_compat( undef, ['donald* duck*'] );
121 $query->{query}{query_string}{query},
123 "query with '*' is unaltered when QueryAutoTruncate is enabled"
126 ( undef, $query ) = $builder->build_query_compat( undef, ['donald duck and the mouse'] );
128 $query->{query}{query_string}{query},
129 "(donald* duck* and the* mouse*)",
130 "individual words are all truncated and stopwords ignored"
133 ( undef, $query ) = $builder->build_query_compat( undef, ['*'] );
135 $query->{query}{query_string}{query},
137 "query of just '*' is unaltered when QueryAutoTruncate is enabled"
140 ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck"'] );
142 $query->{query}{query_string}{query},
144 "query with quotes is unaltered when QueryAutoTruncate is enabled"
148 ( undef, $query ) = $builder->build_query_compat( undef, ['"donald duck" and "the mouse"'] );
150 $query->{query}{query_string}{query},
151 '("donald duck" and "the mouse")',
152 "all quoted strings are unaltered if more than one in query"
155 ( undef, $query ) = $builder->build_query_compat( undef, ['barcode:123456'] );
157 $query->{query}{query_string}{query},
159 "query of specific field is truncated"
162 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:"123456"'] );
164 $query->{query}{query_string}{query},
165 '(Local-number:"123456")',
166 "query of specific field including hyphen and quoted is not truncated"
169 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number:123456'] );
171 $query->{query}{query_string}{query},
172 '(Local-number:123456*)',
173 "query of specific field including hyphen and not quoted is truncated"
176 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:123456'] );
178 $query->{query}{query_string}{query},
179 '(Local-number.raw:123456*)',
180 "query of specific field including period and not quoted is truncated"
183 ( undef, $query ) = $builder->build_query_compat( undef, ['Local-number.raw:"123456"'] );
185 $query->{query}{query_string}{query},
186 '(Local-number.raw:"123456")',
187 "query of specific field including period and quoted is not truncated"
190 ( undef, $query ) = $builder->build_query_compat( undef, ['J.R.R'] );
192 $query->{query}{query_string}{query},
194 "query including period is truncated but not split at periods"
197 ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'] );
199 $query->{query}{query_string}{query},
200 '(title:"donald duck")',
201 "query of specific field is not truncated when surrouned by quotes"
204 ( undef, $query ) = $builder->build_query_compat( undef, ['title:"donald duck"'], undef, undef, undef, undef, undef, { suppress => 1 } );
206 $query->{query}{query_string}{query},
207 '(title:"donald duck") AND suppress:0',
208 "query of specific field is added AND suppress:0"
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 } );
214 $query->{query}{query_string}{query},
215 '(title:"donald duck")',
216 "query of specific field is not added AND suppress:0"
218 is($query_cgi, 'q=title%3A%22donald%20duck%22', 'query cgi');
221 subtest "_convert_sort_fields" => sub {
223 my @sort_by = $builder->_convert_sort_fields(qw( call_number_asc author_dsc ));
227 { field => 'callnum', direction => 'asc' },
228 { field => 'author', direction => 'desc' }
230 'sort fields should have been split correctly'
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 ));
238 { field => 'callnum', direction => 'asc' },
239 { field => 'author', direction => 'desc' }
241 'sort fields should have been split correctly'