Apache-Solr
view release on metacpan or search on metacpan
lib/Apache/Solr/JSON.pm view on Meta::CPAN
# Copyrights 2012-2025 by [Mark Overmeer].
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.03.
# This code is part of distribution Apache-Solr. Meta-POD processed with
# OODoc into POD and HTML manual-pages. See README.md
# Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
package Apache::Solr::JSON;{
our $VERSION = '1.11';
}
use base 'Apache::Solr';
use warnings;
use strict;
use Log::Report qw(solr);
use Apache::Solr::Result ();
use HTTP::Request ();
use JSON ();
use Scalar::Util qw(blessed);
sub init($)
{ my ($self, $args) = @_;
$args->{format} ||= 'JSON';
$self->SUPER::init($args);
$self->{ASJ_json} = $args->{json} || JSON->new->utf8;
$self;
}
#---------------
sub json() {shift->{ASJ_json}}
#--------------------------
sub _select($$)
{ my ($self, $args, $params) = @_;
# select may be called more than once, but do not add wt each time
# again.
my $endpoint = $self->endpoint('select', params => $params);
my $result = Apache::Solr::Result->new(%$args, params => $params, endpoint => $endpoint, core => $self);
$self->request($endpoint, $result);
if(my $dec = $result->decoded)
{ # JSON uses different names!
my $r = $dec->{result} = delete $dec->{response};
$r->{doc} = delete $r->{docs};
}
$result;
}
sub _extract($$$)
{ my ($self, $params, $data, $ct) = @_;
my $endpoint = $self->endpoint('update/extract', params => $params);
my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
$self->request($endpoint, $result, $data, $ct);
$result;
}
sub _add($$$)
{ my ($self, $docs, $attrs, $params) = @_;
$attrs ||= {};
$params ||= [];
my $sv = $self->serverVersion;
$sv ge '3.1' or error __x"Solr version too old for updates in JSON syntax";
# We cannot create HASHes with twice the same key in Perl, so cannot
# produce the syntax for adding multiple documents. Try to save it.
delete $attrs->{boost}
if $attrs->{boost} && $attrs->{boost}==1.0;
$params = +{ @$params } if ref $params eq 'ARRAY';
exists $attrs->{$_} && ($params->{$_} = delete $attrs->{$_})
for qw/commit commitWithin overwrite boost/;
my $endpoint = $self->endpoint(($sv lt '4.0' ? 'update/json' : 'update'), params => $params);
my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
my $add;
if(@$docs==1)
{ $add = +{ add => +{ %$attrs, doc => $self->_doc2json($docs->[0]) } }
}
elsif(keys %$attrs)
{ # in combination with attributes only
( run in 0.472 second using v1.01-cache-2.11-cpan-39bf76dae61 )