Uploaded image for project: 'Solr'
  1. Solr
  2. SOLR-1967

New Native PHP Response Writer Class




      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

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

      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>


      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


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


      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);


      $response = $client->ping();

      $query = new SolrQuery();


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

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

      $resp = $response->getResponse();


      Documentation of the changes to the PECL extension are available here


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


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



            Unassigned Unassigned
            iekpo Israel Ekpo
            0 Vote for this issue
            2 Start watching this issue