e7d3b2ae62e8e64bbcc2ab41beab7ec4c64e443d
[koha-ffzg.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 $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
28 $se->mock( 'get_elasticsearch_mappings', sub {
29     my ($self) = @_;
30
31     my %all_mappings;
32
33     my $mappings = {
34         data => {
35             properties => {
36                 title => {
37                     type => 'text'
38                 },
39                 title__sort => {
40                     type => 'text'
41                 },
42                 subject => {
43                     type => 'text'
44                 },
45                 itemnumber => {
46                     type => 'integer'
47                 },
48                 sortablenumber => {
49                     type => 'integer'
50                 },
51                 sortablenumber__sort => {
52                     type => 'integer'
53                 }
54             }
55         }
56     };
57     $all_mappings{$self->index} = $mappings;
58
59     my $sort_fields = {
60         $self->index => {
61             title => 1,
62             subject => 0,
63             itemnumber => 0,
64             sortablenumber => 1
65         }
66     };
67     $self->sort_fields($sort_fields->{$self->index});
68
69     return $all_mappings{$self->index};
70 });
71
72 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
73
74 use_ok('Koha::SearchEngine::Elasticsearch::Search');
75
76 ok(
77     my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
78         { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
79     ),
80     'Creating a Koha::SearchEngine::Elasticsearch::Search object'
81 );
82
83 is( $searcher->index, 'mydb', 'Testing basic accessor' );
84
85 ok( my $query = $builder->build_query('easy'), 'Build a search query');
86
87 SKIP: {
88
89     eval { $builder->get_elasticsearch_params; };
90
91     skip 'Elasticsearch configuration not available', 8
92         if $@;
93
94     Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'mydb' })->drop_index;
95     Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'mydb' })->create_index;
96
97     ok( my $results = $searcher->search( $query) , 'Do a search ' );
98
99     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
100
101     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
102
103     my ( undef, $scan_query ) = $builder->build_query_compat( undef, ['easy'], [], undef, undef, 1 );
104     ok ((undef, $results) = $searcher->search_compat( $scan_query, undef, [], [], 20, 0, undef, undef, undef, 1 ), 'Test search_compat scan query' );
105     my $expected = {
106         biblioserver => {
107             hits => 0,
108             RECORDS => []
109         }
110     };
111     is_deeply($results, $expected, 'Scan query results ok');
112
113     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
114
115     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
116
117     is ($searcher->max_result_window, 10000, 'By default, max_result_window is 10000');
118
119     $searcher->get_elasticsearch()->indices->put_settings(
120         index => $searcher->index_name,
121         body => {
122             'index' => {
123                 'max_result_window' => 12000,
124             },
125         }
126     );
127     is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
128 }