1 package Koha::SearchEngine::Solr::Search;
3 # This file is part of Koha.
5 # Copyright 2012 BibLibre
6 # Copyright 2012 KohaAloha
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 with 'Koha::SearchEngine::SearchRole';
24 use Data::Dump qw(dump);
27 use Data::SearchEngine::Solr;
29 use Data::SearchEngine::Query;
30 use Koha::SearchEngine::Solr;
34 isa => 'Koha::SearchEngine::Solr',
35 default => sub { Koha::SearchEngine::Solr->new },
40 my ( $self, $q, $filters, $params ) = @_;
44 my $page = defined $params->{page} ? $params->{page} : 1;
45 my $count = defined $params->{count} ? $params->{count} : 999999999;
46 my $sort = defined $params->{sort} ? $params->{sort} : 'score desc';
47 my $facets = defined $params->{facets} ? $params->{facets} : 0;
49 # Construct fl from $params->{fl}
50 # If "recordid" or "id" not exist, we push them
55 grep ( /^recordid$/, @{$params->{fl}} ) ? () : "recordid",
56 grep ( /^id$/, @{$params->{fl}} ) ? () : "id"
58 : ( "recordid", "id" );
61 $recordtype = ref($filters->{recordtype}) eq 'ARRAY'
62 ? $filters->{recordtype}[0]
63 : $filters->{recordtype}
64 if defined $filters && defined $filters->{recordtype};
67 $self->searchengine->options->{"facet"} = 'true';
68 $self->searchengine->options->{"facet.mincount"} = 1;
69 $self->searchengine->options->{"facet.limit"} = 10; # TODO create a new systempreference C4::Context->preference("numFacetsDisplay")
70 my @facetable_indexes = map { 'str_' . $_->{code} } @{$self->searchengine->config->facetable_indexes};
71 $self->searchengine->options->{"facet.field"} = \@facetable_indexes;
73 $self->searchengine->options->{sort} = $sort;
74 $self->searchengine->options->{fl} = $fl;
77 $self->searchengine->options->{fq} = [
80 ref($filters->{$idx}) eq 'ARRAY'
86 utf8::decode($filter_str);
87 my $quotes_existed = ( $filter_str =~ m/^".*"$/ );
88 $filter_str =~ s/^"(.*)"$/$1/; #remove quote around value if exist
89 $filter_str =~ s/[^\\]\K"/\\"/g;
90 $filter_str = qq{"$filter_str"} # Add quote around value if not exist
91 if not $filter_str =~ /^".*"$/
94 } @{ $filters->{$idx} } )
96 : "$idx:$filters->{$idx}";
100 my $sq = Data::SearchEngine::Query->new(
107 my $results = eval { $self->searchengine->search( $sq ) };
109 # Get error if exists
113 $err =~ s#^[^\n]*\n##; # Delete first line
114 if ( $err =~ "400 URL must be absolute" ) {
115 $err = "Your system preference 'SolrAPI' is not set correctly";
117 elsif ( not $err =~ 'Connection refused' ) {
118 my $document = XMLin( $err );
119 $err = "$$document{body}{h2} : $$document{body}{pre}";
121 $results->{error} = $err;