How to get numFound value from response in Apache Solr using Perl

Aswathy Santhosh Source

I used the below code to search documents (which has a particular keyword in content) from Apache Solr

my $solrgetapi = "http://$address:$port/solr/OppsBot/select?q=content:";
my $solrgeturl = $solrgetapi.'"'.$keyword.'"';

my $browser = LWP::UserAgent->new;
my $req =  HTTP::Request->new( GET => $solrgeturl );
$req->authorization_basic( "$username", "$pass" );
my $page = $browser->request( $req );
print $page->decoded_content;

The result I get is as follows:


I want to extract the numFound value to a variable.

I came across some solutions in SolrJ like these


But I couldn't find in Perl.

I tried with these below codes also. But I couldn't get these to work. Please help.

$numFound = $page->decoded_content->{response}->{numFound};
print $page->{numFound}


answered 6 months ago daxim #1

You neglected to transform the JSON text into a data structure.

use JSON::MaybeXS qw(decode_json);
say decode_json($page->decoded_content)->{response}{numFound}
# 0

comments powered by Disqus