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:

{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"q":"content:\"ABC\""}},
"response":{"numFound":0,"start":0,"docs":[]
}}

I want to extract the numFound value to a variable.

I came across some solutions in SolrJ like these

queryResponse.getResults().getNumFound();

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}
apacheperlhttpsearchsolr

Answers

answered 4 days 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