Apache-Solr
view release on metacpan or search on metacpan
lib/Apache/Solr/JSON.pm view on Meta::CPAN
$result;
}
sub _doc2json($)
{ my ($self, $this) = @_;
my %doc;
foreach my $fieldname ($this->fieldNames)
{ my @f;
foreach my $field ($this->fields($fieldname))
{ my $update = $field->{update} || 'value';
my $boost = $field->{boost} || 1.0;
undef $boost
if $boost > 0.9999 && $boost < 1.0001;
push @f
, ! defined $boost && $update eq 'value'
? $field->{content}
: defined $boost
? +{ boost => $boost, $update => $field->{content} }
: +{ $update => $field->{content} };
}
# we have to combine multi-fields into ARRAYS
$doc{$fieldname} = @f > 1 ? \@f : $f[0];
}
\%doc;
}
sub _commit($) { my ($s, $attr) = @_; $s->simpleUpdate(commit => $attr) }
sub _optimize($) { my ($s, $attr) = @_; $s->simpleUpdate(optimize => $attr) }
sub _delete($$) { my $self = shift; $self->simpleUpdate(delete => @_) }
sub _rollback() { shift->simpleUpdate('rollback') }
sub _terms($)
{ my ($self, $terms) = @_;
my $endpoint = $self->endpoint('terms', params => $terms);
my $result = Apache::Solr::Result->new(params => $terms, endpoint => $endpoint, core => $self);
$self->request($endpoint, $result);
my $table = $result->decoded->{terms} || {};
$table = {@$table} if ref $table eq 'ARRAY'; # bug in Solr 1.4
while(my ($field, $terms) = each %$table)
{ # repack array-of-pairs into array-of-arrays-of-pair
my @pairs = @$terms;
my @terms;
push @terms, [shift @pairs, shift @pairs] while @pairs;
$result->terms($field => \@terms);
}
$result;
}
#--------------------------
sub request($$;$$)
{ my ($self, $url, $result, $body, $body_ct) = @_;
if(ref $body && ref $body ne 'SCALAR')
{ $body_ct ||= 'application/json; charset=utf-8';
$body = \$self->json->encode($body);
}
# Solr server 3.6.2 seems not to detect the JSON input from the
# body content, so requires this work-around
# https://solr.apache.org/guide/6_6/uploading-data-with-index-handlers.html#UploadingDatawithIndexHandlers-JSONUpdateConveniencePaths
$url =~ s!/update\?!/update/json?!;
$self->SUPER::request($url, $result, $body, $body_ct);
}
sub decodeResponse($)
{ my ($self, $resp) = @_;
# At least until Solr 4.0 response ct=text/plain while producing JSON
my $ct = $resp->content_type;
$ct =~ m/json/i
or error __x"Answer from solr server is not json but {type}", type => $ct;
$self->json->decode($resp->decoded_content || $resp->content);
}
sub simpleUpdate($$;$)
{ my ($self, $command, $attrs, $content) = @_;
my $sv = $self->serverVersion;
$sv ge '3.1' or error __x"Solr version too old for updates in JSON syntax";
$attrs ||= {};
my $params = [ commit => delete $attrs->{commit} ];
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 %params = (%$attrs, (!$content ? () : ref $content eq 'HASH' ? %$content : @$content));
my $doc = $self->simpleDocument($command, \%params);
$self->request($endpoint, $result, $doc);
$result;
}
sub simpleDocument($;$$)
{ my ($self, $command, $attrs, $content) = @_;
$attrs ||= {};
$content ||= {};
+{ $command => { %$attrs, %$content } }
}
sub endpoint($@)
{ my ($self, $action, %args) = @_;
my $params = $args{params} ||= [];
if(ref $params eq 'HASH') { $params->{wt} ||= 'json' }
else { $args{params} = [ wt => 'json', @$params ] }
$self->SUPER::endpoint($action, %args);
}
1;
( run in 1.542 second using v1.01-cache-2.11-cpan-39bf76dae61 )