swiftlet has asked for the wisdom of the Perl Monks concerning the following question:

I couldn't find any example to retrieve WebService::Solr facet value on perldoc or Google. Currently, I am using a dirty way, "Dumper and Regex" to retrieve it. Any idea what's the official proper way to do it?
my %options = ( rows=> 100, "facet"=> "on", "facet.field"=> "author_id", ); use Data::Dumper; my $response = $solr->search("\*:\*", \%options); my $data = Dumper(\$response); my %facet; if($data =~ m#"facet_fields":{\s*"author_id":\[\s*([^\]]+)\]},#i){ my $authors = $1; while($authors =~ m#"([^"]+)",(\d+),#gi){ $facet{$1} = $2; } }

Replies are listed 'Best First'.
Re: How to retrieve WebService::Solr facet value properly?
by hippo (Archbishop) on Aug 07, 2020 at 05:48 UTC

    According to the fine documentation the WebService::Solr::Response object has a facet_counts method. Have you tried that?


    🦛

      Thanks for your information, I have just tried it. Seem like that's the official way to get facet count, but I don't like they put facet field and facet count in one array, maybe my "Dumper +Regex" is safer.

      my $facet_counts = $response->facet_counts; my $autId = $facet_counts->{facet_fields}{author_id}; print Dumper(\$autId), "<br>"; for(my $i=0; $i < int(@{$autId}); $i++){ print @{$autId}[$i], ": ", @{$autId}[++$i], "<br>\n"; };
      Output:

      $VAR1 = \[ '9291', 131, '3389', 88, '622', 85, '123715', 81, '5091', 6 +3 ]; 9291: 131 3389: 88 622: 85 123715: 81 5091: 63
        I don't like they put facet field and facet count in one array,

        So, just hashify it yourself (although you'll lose the sorting, of course):

        my $autId = $facet_counts->{facet_fields}{author_id}; my %easy_hash = @$autId;
        maybe my "Dumper +Regex" is safer.

        Almost certainly not. Take the robust approach and then post-process the data as desired.


        🦛

Re: How to retrieve WebService::Solr facet value properly?
by NetWallah (Canon) on Aug 07, 2020 at 02:08 UTC
    Based on the module synopsis, you should be able to do:
    for my $doc ( $response->docs ) { print $doc->value_for( 'author_id' ); }

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

      value_for('author_id') can only retrieve the value author_id of a doc, but not the facet value.

      for instance, value_for('author_id') = stephenking, the facet value of author_id maybe: "stephenking" = 123