0

I am using Apache Solr Engine to provide suggestions to the user based on the input keyword.

I have a json response in the following format.

{
    "suggest": {
        "mySuggester": {
            "A Ga": {
                "numFound": 2,
                "suggestions": [
                {
                    "term": "<b>A Ga</b>me of Thrones",
                    "weight": 0,
                    "payload": "Book"
                },
                {
                    "term": "<b>A Ga</b>teway to Heaven",
                    "weight": 0,
                    "payload": "Book"
                }]
            }
        }
    }
}

The Key A Gais user input, that is, based on this key, the suggestions are provided. Now Solr returns this JSON which is stored in my $response variable in php and the key A Ga is contained in the variable $inp.

In my code, there is a line which is,

$noOfSuggestions =  $response->suggest->mySuggester->$inp->numFound;

I am getting a cannot access empty property error at this line. Is my usage of $inp correct?

Thanks!

EDIT: This is var_dump($response);

object(Apache_Solr_Response)#60 (5) { ["_response":protected]=> object(Apache_Solr_HttpTransport_Response)#50 (5) { ["_statusCode":"Apache_Solr_HttpTransport_Response":private]=> int(200) ["_statusMessage":"Apache_Solr_HttpTransport_Response":private]=> string(2) "OK" ["_mimeType":"Apache_Solr_HttpTransport_Response":private]=> string(10) "text/plain" ["_encoding":"Apache_Solr_HttpTransport_Response":private]=> string(5) "UTF-8" ["_responseBody":"Apache_Solr_HttpTransport_Response":private]=> string(511) "{"responseHeader":{"status":0,"QTime":2},"suggest":{"mySuggester":{"A Ga":{"numFound":2,"suggestions":[{"term":"A Game of Thrones","weight":0,"payload":"Book"},{"term":"A Gateway to Heaven","weight":0,"payload":"Book"}]}}}} " } ["_isParsed":protected]=> bool(false) ["_parsedData":protected]=> NULL ["_createDocuments":protected]=> bool(true) ["_collapseSingleValueArrays":protected]=> bool(true) }
8
  • Do this and it will make easy for you. $array = json_decode( $json, true ); Commented Jun 7, 2016 at 4:54
  • You should be able to get it using $response->suggest->mySuggester->{$inp}->numFound; (note the curly braces). If not, can you post a var_dump? Commented Jun 7, 2016 at 4:56
  • @jeroen I have provided the var_dump in the EDIT. Can you tell me what is the significance of curly brace? Commented Jun 7, 2016 at 5:04
  • 1
    You need to get the json from the solr object first, $response is a lot more than the example you show on top. Commented Jun 7, 2016 at 5:53
  • 1
    Not according to your var_dump(): object(Apache_Solr_Response)... Commented Jun 7, 2016 at 6:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.