Solr
  1. Solr
  2. SOLR-1967

New Native PHP Response Writer Class

    Details

      Description

      Hi Solr users,

      If you are using Apache Solr via PHP, I have some good news for you.

      There is a new response writer for the PHP native extension, currently available as a plugin.

      This new feature adds a new response writer class to the org.apache.solr.request package.

      This class is used by the PHP Native Solr Client driver to prepare the query response from Solr.

      This response writer allows you to configure the way the data is serialized for the PHP client.

      You can use your own class name and you can also control how the properties are serialized as well.

      The formatting of the response data is very similar to the way it is currently done by the PECL extension on the client side.

      The only difference now is that this serialization is happening on the server side instead.

      You will find this new response writer particularly useful when dealing with responses for

      • highlighting
      • admin threads responses
      • more like this responses

      to mention just a few

      You can pass the "objectClassName" request parameter to specify the class name to be used for serializing objects.

      Please note that the class must be available on the client side to avoid a PHP_Incomplete_Object error during the unserialization process.

      You can also pass in the "objectPropertiesStorageMode" request parameter with either a 0 (independent properties) or a 1 (combined properties).

      These parameters can also be passed as a named list when loading the response writer in the solrconfig.xml file

      Having this control allows you to create custom objects which gives the flexibility of implementing custom __get methods, ArrayAccess, Traversable and Iterator interfaces on the PHP client side.

      Until this class in incorporated into Solr, you simply have to copy the jar file containing this plugin into your lib directory under $SOLR_HOME

      The jar file is available here and so is the source code.

      Then set up the configuration as shown below and then restart your servelet container

      Below is an example configuration in solrconfig.xml

      <code>
      <queryResponseWriter name="phpnative" class="org.apache.solr.request.PHPNativeResponseWriter">
      <!-- You can choose a different class for your objects. Just make sure the class is available in the client -->
      <str name="objectClassName">SolrObject</str>
      <!--
      0 means OBJECT_PROPERTIES_STORAGE_MODE_INDEPENDENT
      1 means OBJECT_PROPERTIES_STORAGE_MODE_COMBINED

      In independed mode, each property is a separate property
      In combined mode, all the properites are merged into a _properties array.
      The combined mode allows you to create custom __getters and you could also implement ArrayAccess, Iterator and Traversable
      -->
      <int name="objectPropertiesStorageMode">0</int>
      </queryResponseWriter

      <code>

      Below is an example implementation on the PHP client side.

      Support for specifying custom response writers will be available starting from the 0.9.11 version of the PECL extension for Solr currently available here

      http://pecl.php.net/package/solr

      Here is an example of how to use the new response writer with the PHP client.

      <code>
      <?php

      class SolrClass
      {
      public $_properties = array();

      public function __get($property_name) {

      if (property_exists($this, $property_name))

      { return $this->$property_name; }

      else if (isset($_properties[$property_name]))

      { return $_properties[$property_name]; }

      return null;
      }
      }

      $options = array
      (
      'hostname' => 'localhost',
      'port' => 8983,
      'path' => '/solr/'
      );

      $client = new SolrClient($options);

      $client->setResponseWriter("phpnative");

      $response = $client->ping();

      $query = new SolrQuery();

      $query->setQuery(":");

      $query->set("objectClassName", "SolrClass");
      $query->set("objectPropertiesStorageMode", 1);

      $response = $client->query($query);

      $resp = $response->getResponse();

      ?>
      <code>

      Documentation of the changes to the PECL extension are available here

      http://docs.php.net/manual/en/solrclient.construct.php
      http://docs.php.net/manual/en/solrclient.setresponsewriter.php

      Please contact me at iekpo@php.net, if you have any questions or comments.

      1. phpnativeresponsewriter.jar
        10 kB
        Israel Ekpo
      2. phpnative.tar.gz
        8 kB
        Israel Ekpo

        Activity

        Hide
        Israel Ekpo added a comment -

        Attaching the source code and the jar file.

        Until the response writer is integrated into Solr, to use this response writer you will have to place the jar file phpnativeresponsewriter.jar in your lib directory and then modify your solrconfig.xml file to load the response writer class.

        If you are using a custom php class on the client, please ensure that the class name you have specified is also available during the unserialize event to avoid incomplete php objects.

        You have the option of determining how the object properties are prepared.

        The properties can be combined into an array or encoded as individual properties.

        The former allows you to do custom handling of the objects as well as implementing ArrayAccess, Iterator and Traversable on the custom object.

        Show
        Israel Ekpo added a comment - Attaching the source code and the jar file. Until the response writer is integrated into Solr, to use this response writer you will have to place the jar file phpnativeresponsewriter.jar in your lib directory and then modify your solrconfig.xml file to load the response writer class. If you are using a custom php class on the client, please ensure that the class name you have specified is also available during the unserialize event to avoid incomplete php objects. You have the option of determining how the object properties are prepared. The properties can be combined into an array or encoded as individual properties. The former allows you to do custom handling of the objects as well as implementing ArrayAccess, Iterator and Traversable on the custom object.
        Hide
        JimmyZhuo added a comment -

        SolrClass was wrong,
        It should be:

        class SolrClass
        {
        public $_properties = array();

        public function __get($property_name) {

        if (property_exists($this, $property_name))

        { return $this->$property_name; }

        else if (isset($this->_properties[$property_name]))

        { return $this->_properties[$property_name]; }

        return null;
        }
        }

        Show
        JimmyZhuo added a comment - SolrClass was wrong, It should be: class SolrClass { public $_properties = array(); public function __get($property_name) { if (property_exists($this, $property_name)) { return $this->$property_name; } else if (isset($this->_properties [$property_name] )) { return $this->_properties[$property_name]; } return null; } }
        Hide
        JimmyZhuo added a comment -

        Hello
        found a bug. when I use phpnative, it outputs an warning. but I use xml, then it works.

        Warning: SolrResponse::getResponse(): Error unserializing raw response. in /home/carlos/topons-dev/magento/test.php on line 0

        the object data was:

        SolrQueryResponse Object
        (
        [http_status:protected] => 200
        [parser_mode:protected] => 0
        [success:protected] => 1
        [response_writer:protected] => phpnative
        [http_status_message:protected] =>
        [http_request_url:protected] => http://localhost:8080/solr/select/?version=2.2&indent=on&wt=phpnative
        [http_raw_request_headers:protected] => POST /solr/select/?version=2.2&indent=on&wt=phpnative HTTP/1.1
        User-Agent: PHP Solr Client 0.9.11
        Host: localhost:8080
        Accept: /
        Accept-Charset: utf-8
        Keep-Alive: 300
        Connection: keep-alive
        Content-Type: application/x-www-form-urlencoded;charset=UTF-8
        Content-Length: 6

        [http_raw_request:protected] => q=slim
        [http_raw_response_headers:protected] => HTTP/1.1 200 OK
        Server: Apache-Coyote/1.1
        Content-Type: text/x-php-serialized;charset=UTF-8
        Content-Length: 3279
        Date: Fri, 06 Aug 2010 07:29:01 GMT

        [http_raw_response:protected] => O:10:"SolrObject":2:{s:8:"response";O:10:"SolrObject":3:{s:8:"numFound";s:2:"17";s:5:"start";s:1:"0";s:4:"docs";a:10:{i:0;O:10:"SolrObject":5:{s:2:"id";s:4:"7251";s:4:"name";s:26:"Slim decorating brown belt";s:11:"description";s:0:"";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:1;O:10:"SolrObject":5:{s:2:"id";s:4:"7874";s:4:"name";s:24:"Black Slim Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:2;O:10:"SolrObject":5:{s:2:"id";s:3:"547";s:4:"name";s:46:"Earphone Remote Control for PSP Slim 2000 3000";s:11:"description";s:324:"Listen to your favorite music on your Sony PSP Slim 2000 / 3000 with this high quality stereo earphone. It comes with a remote control with functions for play, pause, FF, FR, volume, hold. A Highly fashionable style and it聮s unique for the function of microphone which allows you talk with your friends when you play games. ";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:3;O:10:"SolrObject":5:{s:2:"id";s:4:"7875";s:4:"name";s:31:"Newport Navy Slim-Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:4;O:10:"SolrObject":5:{s:2:"id";s:4:"7888";s:4:"name";s:32:"New Chocolate Slim-Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:5;O:10:"SolrObject":5:{s:2:"id";s:4:"7890";s:4:"name";s:24:"White Slim-Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:6;O:10:"SolrObject":5:{s:2:"id";s:3:"401";s:4:"name";s:58:"Slim Vertu F1 Mobile Phone with Red Ox Leather On the Back";s:11:"description";s:120:"GSM 900/1800MHz, Special Vertu shape keys, red ox leather on the back, incoming call blacklist, E-Book, bluetooth 2.0
        ";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:7;O:10:"SolrObject":5:{s:2:"id";s:4:"2716";s:4:"name";s:52:"Free Shipping Fine Slim Custom-Fit Andrew Polo Shirt";s:11:"description";s:180:"Our luxurious, long-sleeved dress shirt is expertly tailored for a trim, modern fit from exquisite 120s-quality two-ply cotton poplin in a smooth, silky finish for a polished look.";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:8;O:10:"SolrObject":5:{s:2:"id";s:4:"2718";s:4:"name";s:51:"Free Shipping Fine Slim Custom-Fit Plaid Polo Shirt";s:11:"description";s:274:"Our luxurious, long-sleeved sport shirt is expertly tailored for a trim, modern fit from exquisite 120s-quality two-ply poplin, printed with a micro-plaid pattern for a polished look that is the pinnacle of refined style. * Medium-spread button-down collar. Applied placket.";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}i:9;O:10:"SolrObject":5:{s:2:"id";s:4:"2720";s:4:"name";s:52:"Free Shipping Fine Slim Custom-Fit Estate Polo Shirt";s:11:"description";s:193:"Our luxurious, long-sleeved dress shirt is expertly tailored for a trim, modern fit from ultra-silky 120s-quality two-ply cotton poplin printed in a multi-stripe design for polished refinement.";s:5:"links";a:1:

        {i:0;s:8:"url_path";}

        s:5:"score";d:0.0;}}}s:14:"responseHeader";O:10:"SolrObject":3:{s:6:"status";i:0;s:5:"QTime";i:1;s:6:"params";O:10:"SolrObject":4:

        {s:6:"indent";s:2:"on";s:1:"q";s:4:"slim";s:2:"wt";s:9:"phpnative";s:7:"version";s:3:"2.2";}

        }}
        [http_digested_response:protected] =>
        )

        Show
        JimmyZhuo added a comment - Hello found a bug. when I use phpnative, it outputs an warning. but I use xml, then it works. Warning: SolrResponse::getResponse(): Error unserializing raw response. in /home/carlos/topons-dev/magento/test.php on line 0 the object data was: SolrQueryResponse Object ( [http_status:protected] => 200 [parser_mode:protected] => 0 [success:protected] => 1 [response_writer:protected] => phpnative [http_status_message:protected] => [http_request_url:protected] => http://localhost:8080/solr/select/?version=2.2&indent=on&wt=phpnative [http_raw_request_headers:protected] => POST /solr/select/?version=2.2&indent=on&wt=phpnative HTTP/1.1 User-Agent: PHP Solr Client 0.9.11 Host: localhost:8080 Accept: / Accept-Charset: utf-8 Keep-Alive: 300 Connection: keep-alive Content-Type: application/x-www-form-urlencoded;charset=UTF-8 Content-Length: 6 [http_raw_request:protected] => q=slim [http_raw_response_headers:protected] => HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/x-php-serialized;charset=UTF-8 Content-Length: 3279 Date: Fri, 06 Aug 2010 07:29:01 GMT [http_raw_response:protected] => O:10:"SolrObject":2:{s:8:"response";O:10:"SolrObject":3:{s:8:"numFound";s:2:"17";s:5:"start";s:1:"0";s:4:"docs";a:10:{i:0;O:10:"SolrObject":5:{s:2:"id";s:4:"7251";s:4:"name";s:26:"Slim decorating brown belt";s:11:"description";s:0:"";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:1;O:10:"SolrObject":5:{s:2:"id";s:4:"7874";s:4:"name";s:24:"Black Slim Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:2;O:10:"SolrObject":5:{s:2:"id";s:3:"547";s:4:"name";s:46:"Earphone Remote Control for PSP Slim 2000 3000";s:11:"description";s:324:"Listen to your favorite music on your Sony PSP Slim 2000 / 3000 with this high quality stereo earphone. It comes with a remote control with functions for play, pause, FF, FR, volume, hold. A Highly fashionable style and it聮s unique for the function of microphone which allows you talk with your friends when you play games. ";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:3;O:10:"SolrObject":5:{s:2:"id";s:4:"7875";s:4:"name";s:31:"Newport Navy Slim-Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:4;O:10:"SolrObject":5:{s:2:"id";s:4:"7888";s:4:"name";s:32:"New Chocolate Slim-Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:5;O:10:"SolrObject":5:{s:2:"id";s:4:"7890";s:4:"name";s:24:"White Slim-Fit Mesh Polo";s:11:"description";s:0:"";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:6;O:10:"SolrObject":5:{s:2:"id";s:3:"401";s:4:"name";s:58:"Slim Vertu F1 Mobile Phone with Red Ox Leather On the Back";s:11:"description";s:120:"GSM 900/1800MHz, Special Vertu shape keys, red ox leather on the back, incoming call blacklist, E-Book, bluetooth 2.0 ";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:7;O:10:"SolrObject":5:{s:2:"id";s:4:"2716";s:4:"name";s:52:"Free Shipping Fine Slim Custom-Fit Andrew Polo Shirt";s:11:"description";s:180:"Our luxurious, long-sleeved dress shirt is expertly tailored for a trim, modern fit from exquisite 120s-quality two-ply cotton poplin in a smooth, silky finish for a polished look.";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:8;O:10:"SolrObject":5:{s:2:"id";s:4:"2718";s:4:"name";s:51:"Free Shipping Fine Slim Custom-Fit Plaid Polo Shirt";s:11:"description";s:274:"Our luxurious, long-sleeved sport shirt is expertly tailored for a trim, modern fit from exquisite 120s-quality two-ply poplin, printed with a micro-plaid pattern for a polished look that is the pinnacle of refined style. * Medium-spread button-down collar. Applied placket.";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}i:9;O:10:"SolrObject":5:{s:2:"id";s:4:"2720";s:4:"name";s:52:"Free Shipping Fine Slim Custom-Fit Estate Polo Shirt";s:11:"description";s:193:"Our luxurious, long-sleeved dress shirt is expertly tailored for a trim, modern fit from ultra-silky 120s-quality two-ply cotton poplin printed in a multi-stripe design for polished refinement.";s:5:"links";a:1: {i:0;s:8:"url_path";} s:5:"score";d:0.0;}}}s:14:"responseHeader";O:10:"SolrObject":3:{s:6:"status";i:0;s:5:"QTime";i:1;s:6:"params";O:10:"SolrObject":4: {s:6:"indent";s:2:"on";s:1:"q";s:4:"slim";s:2:"wt";s:9:"phpnative";s:7:"version";s:3:"2.2";} }} [http_digested_response:protected] => )
        Hide
        Peter Wolanin added a comment -

        To my mind, the PHP response writer should just be removed. PHP has had a number of security issues around unserializing data, and in most languages, unserializing potentially untrusted data may be an security vulnerability.

        Show
        Peter Wolanin added a comment - To my mind, the PHP response writer should just be removed. PHP has had a number of security issues around unserializing data, and in most languages, unserializing potentially untrusted data may be an security vulnerability.
        Hide
        Israel Ekpo added a comment -

        If you are getting errors when unserializing the response, it means you have control or special characters in the unserialized data that is causing this issue. Try striping out any extra characters before indexing the data.

        This will get rid of this error.

        Show
        Israel Ekpo added a comment - If you are getting errors when unserializing the response, it means you have control or special characters in the unserialized data that is causing this issue. Try striping out any extra characters before indexing the data. This will get rid of this error.
        Hide
        Mark Miller added a comment -

        Any committer interested in this for 3.1? I don't think I'll be dusting off my PHP skills for this release...

        Show
        Mark Miller added a comment - Any committer interested in this for 3.1? I don't think I'll be dusting off my PHP skills for this release...
        Hide
        Robert Muir added a comment -
        Show
        Robert Muir added a comment - Moving out all unassigned issues set to 3.1 per this email: http://www.lucidimagination.com/search/document/f026cc56081b5a51/unassigned_jira_issues_set_for_3_1
        Hide
        Robert Muir added a comment -

        Bulk move 3.2 -> 3.3

        Show
        Robert Muir added a comment - Bulk move 3.2 -> 3.3
        Hide
        Eric Pugh added a comment -

        Seems like this could be closed as a won't fix. One of the most popular clients for PHP, solr-php-client (http://code.google.com/p/solr-php-client/), doesn't use the PHP writer at all! And isn't going to: http://code.google.com/p/solr-php-client/issues/detail?id=6#c1

        I'd echo Peter Wolanin's comment that having lots of "writers" that don't get tested/updated isn't a good thing.

        Show
        Eric Pugh added a comment - Seems like this could be closed as a won't fix. One of the most popular clients for PHP, solr-php-client ( http://code.google.com/p/solr-php-client/ ), doesn't use the PHP writer at all! And isn't going to: http://code.google.com/p/solr-php-client/issues/detail?id=6#c1 I'd echo Peter Wolanin's comment that having lots of "writers" that don't get tested/updated isn't a good thing.
        Hide
        Israel Ekpo added a comment -

        The latest version of the PECL extension now supports JSON response writer which should be easier to use without additional configuration.

        Show
        Israel Ekpo added a comment - The latest version of the PECL extension now supports JSON response writer which should be easier to use without additional configuration.
        Hide
        Israel Ekpo added a comment -

        To use the 'json' response writer in lieu of phpnative, see documentation for SolrClient::__construct()

        http://www.php.net/manual/en/solrclient.construct.php

        Show
        Israel Ekpo added a comment - To use the 'json' response writer in lieu of phpnative, see documentation for SolrClient::__construct() http://www.php.net/manual/en/solrclient.construct.php
        Hide
        shenzhuxi added a comment -

        Why not just?
        $code = file_get_contents('http://localhost:8983/solr/select?q=iPod&wt=php');
        eval("\$result = " . $code . ";");
        print_r($result);

        Show
        shenzhuxi added a comment - Why not just? $code = file_get_contents('http://localhost:8983/solr/select?q=iPod&wt=php'); eval("\$result = " . $code . ";"); print_r($result);
        Hide
        shenzhuxi added a comment -

        @Peter Wolanin

        Why "wt=php" is not secure? Any example?
        Is "wt=phps" secure? If not, why?

        Show
        shenzhuxi added a comment - @Peter Wolanin Why "wt=php" is not secure? Any example? Is "wt=phps" secure? If not, why?

          People

          • Assignee:
            Unassigned
            Reporter:
            Israel Ekpo
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development