php_solr
[ class tree: php_solr ] [ index: php_solr ] [ all elements ]

Class: SolrConnection

Source Location: /src/transport/SolrConnection.php

Class Overview


A Solr Connection.


Author(s):

Methods



Class Details

[line 52]
A Solr Connection.

To open a new connection, simply call Solr::connect().




Tags:

author:  Alexander M. Turek <rabus@users.sourceforge.net>
uses:  XMLWriter


[ Top ]


Class Methods


static method connect [line 80]

static Solr connect( [string $url = 'http://localhost:8983/solr'])

Opens a new Solr connection.



Tags:

throws:  SolrException
access:  public


Parameters:

string   $url   The URL for Solr queries.

[ Top ]

method add [line 187]

void add( SolrDocument|array(SolrDocument) $docs)

Adds one or multiple documents to the index.

Example 1: Adding a singe document to the Solr index.

  1.  require_once 'php_solr/Solr.php';
  2.  Solr::autoload('SolrSimpleDocument');
  3.  Solr::autoload('SolrSimpleField');
  4.  
  5.  $doc new SolrSimpleDocument(array(
  6.      new SolrSimpleField('id''42'),
  7.      new SolrSimpleField('name''Arthur Dent')
  8.  ));
  9.  $solr Solr::connect();
  10.  $solr->add($doc);
  11.  $solr->commit();

Example 2: Adding two documents with a single command.

  1.  require_once 'php_solr/Solr.php';
  2.  Solr::autoload('SolrSimpleDocument');
  3.  Solr::autoload('SolrSimpleField');
  4.  
  5.  $doc1 new SolrSimpleDocument(array(
  6.      new SolrSimpleField('id''42'),
  7.      new SolrSimpleField('name''Arthur Dent')
  8.  ));
  9.  $doc2 new SolrSimpleDocument(array(
  10.      new SolrSimpleField('id''24'),
  11.      new SolrSimpleField('name''Marvin')
  12.  ));
  13.  $solr Solr::connect();
  14.  $solr->add(array($doc1$doc2));
  15.  $solr->commit();

SolrSimpleDocument is only a sample implementation of a Solr document. If your documents are already mapped to objects, it is not necessary to convert them to SolrSimpleDocument instances. Instead, it is sufficient to implement SolrDocument inside your classes.

The same applies to objects that shall be mapped to fields. All you need to do is implementing SolrField.

  1.  require_once 'php_solr/Solr.php';
  2.  Solr::autoload('SolrDocument');
  3.  Solr::autoload('SolrField');
  4.  
  5.  class MyNameClass implements SolrField {
  6.      private $first_name;
  7.      private $last_name;
  8.  
  9.      ...
  10.  
  11.      public function getName ({
  12.          return 'name';
  13.      }
  14.  
  15.      public function getValue ({
  16.          return $this->first_name ' ' $this->last_name;
  17.      }
  18.  
  19.      public function getBoost ({
  20.          return null;
  21.      }
  22.  }
  23.  
  24.  class MyDocumentClass implements SolrDocument {
  25.      private $id;
  26.      private $name// a MyNameClass object
  27.  
  28.      ...
  29.  
  30.      public function getFields ({
  31.          return array(
  32.              new SolrSimpleField('id'$this->id),
  33.              $name
  34.          );
  35.      }
  36.  
  37.      public function getBoost ({
  38.          return null;
  39.      }
  40.  }




Tags:

throws:  SolrException
access:  public


Parameters:

SolrDocument|array(SolrDocument)   $docs   The document(s).

[ Top ]

method commit [line 317]

void commit( )

Commits all recently added documents.



Tags:

throws:  SolrException
access:  public


[ Top ]

method deleteById [line 234]

void deleteById( string $id)

Deletes the document with the given ID.



Tags:

since:  0.4.1
access:  public


Parameters:

string   $id   The ID of the document.

[ Top ]

method deleteByQuery [line 258]

void deleteByQuery( string $query)

Deletes the document Which match the given query.



Tags:

since:  0.4.1
access:  public


Parameters:

string   $query   The query.

[ Top ]

method getSolrSpecVersion [line 395]

string getSolrSpecVersion( )

Returns the server's Solr specification version.

Comparing Solr versions can be done with http://www.php.net/version_compare.

  1.  $solr Solr::connect();
  2.  
  3.  echo $solr->getSolrSpecVersion("\n";
  4.  
  5.  if (version_compare($solr->getSolrSpecVersion()'1.3''<')) {
  6.      echo 'The server version is older than 1.3.' "\n";
  7.  }
  8.  if (version_compare($solr->getSolrSpecVersion()'1.2''>=')) {
  9.      echo 'We have at least Solr 1.2 running.' "\n";
  10.  }

If you run the example script above against Solr 1.2, you will get the following output:

 1.2.2008.03.21.05.21.15
 The server version is older than 1.3.
 We have at least Solr 1.2 running.

The information is cached afterwards, so calling this function multiple times in a row is safe.




Tags:

since:  0.4.0
access:  public
uses:  json_decode()


[ Top ]

method optimize [line 332]

void optimize( )

Optimizes the index.



Tags:

throws:  SolrException
access:  public


[ Top ]

method ping [line 348]

boolean ping( )

Checks if the server is accesable.



Tags:

since:  0.2.1
access:  public


[ Top ]

method query [line 297]

SolrSearchResult query( SolrQuery|string $query)

Sends a query to the Solr server.

Although it is recommended to provide the query as a SolrQuery object, it is also possible to pass it as a string. So, the following two lines of code produce exactly the same result:

  1.  $result $solr->query('hello world');
  2.  $result $solr->query(new SolrQuery('hello world'));

Please refer to the SolrQuery class for more details on query parameters and the query syntax.




Tags:

see:  SolrQuery
access:  public
uses:  json_decode()


Parameters:

SolrQuery|string   $query   The query.

[ Top ]


Documentation generated on Wed, 20 May 2009 12:51:09 +0000 by phpDocumentor 1.4.2