Solr PHP Client Example

Define the index FieldS in Solr schema.xml

Before you can index data with solr, you need to define the types of the data. You can do this with the file "schema.xml", which can be found in the /conf folder of your solr installation.

Solr indexes so called "documents" which consists of an array of fields. If you want to add new documents to the index with PHP, you create a $doc-object with the PHP Service Class and fill the fields of the object with its value. Then you send the $doc-object to the solr server. The index will be updated if the $doc-object fits the defined index-schema in the schemal.xml file.

With the following example I will explain the structure of the solr schema.xml:

erläutert:

Example:

<schema name="ayalon" version="1.1">
  ...
  <fields>
  <field name="id" type="string" indexed="true" stored="true" required="true" />
  <field name="title" type="text" indexed="true" stored="true"/>
  <field name="titleSort" type="string" indexed="true" stored="false"/>
  <field name="text" type="text" indexed="true" stored="true"/>
  <field name="category" type="text" indexed="true" stored="true" multiValued="true"/>
  ...
  </fields>
 
  <uniqueKey>id</uniqueKey>
  ...
  <copyField source="title" dest="titleSort"/>
  ...
</schema>

Explanation:

- Every schema need an unique (non-ambiguous) key (uniqueKey). With this key, all records are referenced.

- textSort is a copy of the field "text". It's a tokenized field in contrary to the "title" field. This is necessary to make a field sortable.

- category is a value array (multiValue-field). With multivalue fields you can implement facets. 

Exercises

  1. Create a new schema.xml file according to the fields above. Use the existing schema.xml of your solr installation. Restart the solr server to use the new schema.xml
  2. Index the 2 documents with the code below. You you changed the schema.xml correctly, you will be able to add these documents to the solr index.
  3. Check the search results of solr under http://localhost:8983/solr/admin
  4. Implement a search snippet highlighting with the tags "<strong>" and display the snippet (Hint: http://wiki.apache.org/solr/HighlightingParameters)

PHP Client

The Php.client for solr can be checked out of the svn repository with:

svn checkout -r6 http://solr-php-client.googlecode.com/svn/trunk/ SolrPhpClient


Download directly:

Code

<?php
  
  require_once( 'SolrPHPClient/Apache/Solr/Service.php' );
  
  // 
  // 
  // Try to connect to the named server, port, and url
  // 
  $solr = new Apache_Solr_Service( 'localhost', '8983', '/solr' );
  
  if ( ! $solr->ping() ) {
    echo 'Solr service not responding.';
    exit;
  }
  
  //
  //
  // Create two documents
  //
  $docs = array(
    'doc_no1' => array(
      'id' => 1,
      'title' => 'Alphabet',
      'text' => 'The quick brown fox jumps over the lazy dog',
      'category' => array( 'orange', 'pear' ),
    ),
    'doc_no2' => array(
      'id' => 2,
      'title' => 'Letters',
      'text' => 'Jackdaws love my big Sphinx of Quartz',
      'category' => array( 'apple', 'pear' ),
    ),
  );
    
  $documents = array();
  
  foreach ( $docs as $item => $fields ) {
    
    $part = new Apache_Solr_Document();
    
    foreach ( $fields as $key => $value ) {
      if ( is_array( $value ) ) {
        foreach ( $value as $data ) {
          $part->setMultiValue( $key, $data );
        }
      }
      else {
        $part->$key = $value;
      }
    }
    
    $documents[] = $part;
  }
    
  //
  //
  // Load the documents into the index
  // 
  try {
    $solr->addDocuments( $documents );
    $solr->commit();
    $solr->optimize();
  }
  catch ( Exception $e ) {
    echo $e->getMessage();
  }
  
  //
  // 
  // Run some queries. 
  //
  $offset = 0;
  $limit = 10;
  
  $queries = array(
    'id: 1 OR id: 2',
    'category: pear',
    'title: Letters'
  );

  foreach ( $queries as $query ) {
    $response = $solr->search( $query, $offset, $limit );
    
    if ( $response->getHttpStatus() == 200 ) { 
      // print_r( $response->getRawResponse() );
      
      if ( $response->response->numFound > 0 ) {
        echo "$query <br />";

        foreach ( $response->response->docs as $doc ) { 
          echo "$doc->id $doc->title <br />";
        }
        
        echo '<br />';
      }
    }
    else {
      echo $response->getHttpStatusMessage();
    }
  }


?>

 

Solutions

 

Schlagwörter: