Question

I am using a library (Solarium for Solr), and the results returned by it is in a format that I am not familiar with. The chunk of code below shows the output of print_r($resultset) on the results $resultset returned by the library's function.

I would like it to be in a format (maybe array?) where I massage the data further, and convert it into the final format JSON. How can I do that? json_encode($resultset) just gave me an empty JSON string {}.

Data in original format

Solarium_Result_Select Object
(
    [_numfound:protected] => 
    [_documents:protected] => 
    [_components:protected] => 
    [_status:protected] => 
    [_queryTime:protected] => 
    [_parsed:protected] => 
    [_response:protected] => Solarium_Client_Response Object
        (
            [_headers:protected] => Array
                (
                    [0] => HTTP/1.1 200 OK
                    [1] => Server: Apache-Coyote/1.1
                    [2] => Content-Type: text/plain;charset=UTF-8
                    [3] => Date: Fri, 17 Aug 2012 17:21:14 GMT
                    [4] => Connection: close
                )

            [_body:protected] => {"responseHeader":{"status":0,"QTime":0,"params":{"fl":"*,score","start":"0","q":"*:*","wt":"json","rows":"2"}},"response":{"numFound":232,"start":0,"maxScore":1.0,"docs":[{"id":"1045","title":"White dress","description":"Ohhh its white omg fav color","score":1.0},{"id":"1046","title":"Darker tones","description":"Man this looks scary, the ring? Ultra fast transfer speeds: Armor A80's outstanding read/write speed is coupled with the latest USB 3.0 interface and is backwards compatible with USB 2.0. The USB 3.0 interface features a 5 Gbps/sec bandwidth which is 10x the bandwidth of a USB 2.0 interface. It saves users precious time backing up to and from the hard drive when transferring mass data files. Water, pressure, vibration & dust proof.","score":1.0}]}}
            [_statusCode:protected] => 200
            [_statusMessage:protected] => OK
        )

    [_data:protected] => 
    [_query:protected] => Solarium_Query_Select Object
        (
            [_options:protected] => Array
                (
                    [handler] => select
                    [resultclass] => Solarium_Result_Select
                    [documentclass] => Solarium_Document_ReadOnly
                    [query] => *:*
                    [start] => 0
                    [rows] => 2
                    [fields] => *,score
                )

            [_componentTypes:protected] => Array
                (
                    [facetset] => Array
                        (
                            [component] => Solarium_Query_Select_Component_FacetSet
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_FacetSet
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_FacetSet
                        )

                    [dismax] => Array
                        (
                            [component] => Solarium_Query_Select_Component_DisMax
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_DisMax
                            [responseparser] => 
                        )

                    [morelikethis] => Array
                        (
                            [component] => Solarium_Query_Select_Component_MoreLikeThis
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_MoreLikeThis
                        )

                    [highlighting] => Array
                        (
                            [component] => Solarium_Query_Select_Component_Highlighting
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Highlighting
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_Highlighting
                        )

                    [grouping] => Array
                        (
                            [component] => Solarium_Query_Select_Component_Grouping
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Grouping
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_Grouping
                        )

                    [spellcheck] => Array
                        (
                            [component] => Solarium_Query_Select_Component_Spellcheck
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Spellcheck
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_Spellcheck
                        )

                    [distributedsearch] => Array
                        (
                            [component] => Solarium_Query_Select_Component_DistributedSearch
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_DistributedSearch
                            [responseparser] => 
                        )

                    [stats] => Array
                        (
                            [component] => Solarium_Query_Select_Component_Stats
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Stats
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_Stats
                        )

                    [debug] => Array
                        (
                            [component] => Solarium_Query_Select_Component_Debug
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Debug
                            [responseparser] => Solarium_Client_ResponseParser_Select_Component_Debug
                        )

                )

            [_fields:protected] => Array
                (
                    [*] => 1
                    [score] => 1
                )

            [_sorts:protected] => Array
                (
                )

            [_filterQueries:protected] => Array
                (
                )

            [_components:protected] => Array
                (
                )

            [_helper:protected] => 
            [_params:protected] => Array
                (
                )

        )

    [_client:protected] => Solarium_Client Object
        (
            [_options:protected] => Array
                (
                    [adapter] => Solarium_Client_Adapter_Http
                    [adapteroptions] => Array
                        (
                            [host] => 127.0.0.1
                            [port] => 8080
                            [path] => /solr/
                        )

                )

            [_queryTypes:protected] => Array
                (
                    [select] => Array
                        (
                            [query] => Solarium_Query_Select
                            [requestbuilder] => Solarium_Client_RequestBuilder_Select
                            [responseparser] => Solarium_Client_ResponseParser_Select
                        )

                    [update] => Array
                        (
                            [query] => Solarium_Query_Update
                            [requestbuilder] => Solarium_Client_RequestBuilder_Update
                            [responseparser] => Solarium_Client_ResponseParser_Update
                        )

                    [ping] => Array
                        (
                            [query] => Solarium_Query_Ping
                            [requestbuilder] => Solarium_Client_RequestBuilder_Ping
                            [responseparser] => Solarium_Client_ResponseParser_Ping
                        )

                    [mlt] => Array
                        (
                            [query] => Solarium_Query_MoreLikeThis
                            [requestbuilder] => Solarium_Client_RequestBuilder_MoreLikeThis
                            [responseparser] => Solarium_Client_ResponseParser_MoreLikeThis
                        )

                    [analysis-document] => Array
                        (
                            [query] => Solarium_Query_Analysis_Document
                            [requestbuilder] => Solarium_Client_RequestBuilder_Analysis_Document
                            [responseparser] => Solarium_Client_ResponseParser_Analysis_Document
                        )

                    [analysis-field] => Array
                        (
                            [query] => Solarium_Query_Analysis_Field
                            [requestbuilder] => Solarium_Client_RequestBuilder_Analysis_Field
                            [responseparser] => Solarium_Client_ResponseParser_Analysis_Field
                        )

                    [terms] => Array
                        (
                            [query] => Solarium_Query_Terms
                            [requestbuilder] => Solarium_Client_RequestBuilder_Terms
                            [responseparser] => Solarium_Client_ResponseParser_Terms
                        )

                    [suggester] => Array
                        (
                            [query] => Solarium_Query_Suggester
                            [requestbuilder] => Solarium_Client_RequestBuilder_Suggester
                            [responseparser] => Solarium_Client_ResponseParser_Suggester
                        )

                )

            [_pluginTypes:protected] => Array
                (
                    [loadbalancer] => Solarium_Plugin_Loadbalancer
                    [postbigrequest] => Solarium_Plugin_PostBigRequest
                    [customizerequest] => Solarium_Plugin_CustomizeRequest
                    [parallelexecution] => Solarium_Plugin_ParallelExecution
                    [bufferedadd] => Solarium_Plugin_BufferedAdd
                    [prefetchiterator] => Solarium_Plugin_PrefetchIterator
                )

            [_pluginInstances:protected] => Array
                (
                )

            [_adapter:protected] => Solarium_Client_Adapter_Http Object
                (
                    [_options:protected] => Array
                        (
                            [host] => 127.0.0.1
                            [port] => 8080
                            [path] => /solr
                            [core] => 
                            [timeout] => 5
                        )

                )

            [_requestBuilders:protected] => 
        )

)

PHP Code

public function action_search() {

    Solarium_Autoloader::register();
    $client = new Solarium_Client();

    $query = $client
                ->createSelect()
                ->setRows(2);

    $resultset = $client->select($query);

    echo "<pre>";
    print_r($resultset);

    $result = json_encode($resultset);
    echo $result;
Was it helpful?

Solution

Based on the Solarium docs, I'd try something like this:

/*...*/
$resultset = $client->select($query);

$results = array();
foreach($resultset as $document) {
    $item = array();
    foreach($document as $field => $value) {
        $item[$field] = $value;
    }
    $results[] = $item;
}
echo json_encode($results);

OTHER TIPS

$resultset = $client->select($query);

$result_array = json_decode($resultset->getResponse()->getBody())->response->docs;
$result = json_encode($result_array);
echo $result;

Result will be

[{"id":"1045","title":"White dress","description":"Ohhh its white omg fav color","score":1.0},{"id":"1046","title":"Darker tones","description":"Man this looks scary, the ring? Ultra fast transfer speeds: Armor A80's outstanding read/write speed is coupled with the latest USB 3.0 interface and is backwards compatible with USB 2.0. The USB 3.0 interface features a 5 Gbps/sec bandwidth which is 10x the bandwidth of a USB 2.0 interface. It saves users precious time backing up to and from the hard drive when transferring mass data files. Water, pressure, vibration & dust proof.","score":1.0}]

The response object has a lot of fields and functions, you usually want to return the response,

echo json_encode($resultset->getData());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top