Jul 142014
 
Article PHP

In our previous posts about solr we have already covered the installation and configuration of this search engine. In this post we will give an overview of the Solarium library. This library makes it easy querying, updating, and managing a solr database from a PHP script.

1. Installing Composer

Composer is a dependency management tool for PHP that is becoming increasingly popular. Although it is possible to make a Solarium installation without composer, using this tool makes the installation of Solarium much easier. Composer can be installed using a single command that downloads and runs the installation script:

2. Installing Solarium

Having installed composer, create a “composer.php” file in the same directory where “composer.phar” is located, with the content:

composer.json includes a list of software packages that we want to be managed by composer. In this case, we are asking composer to manage version 3.2.0 of Solarium. Next, run “composer install” to perform the installation of solarium, together with the installation of all required dependencies of this sofware:

From the ouput generated by the “composer install” command, we can see that the symfony/event-dispatcher package has been detected as a missing dependency, and has been downloaded and installed.

3. Configuring Solarium and running the sample scripts

After solarium has been successfully installed, go to directory “vendor/solarium/solarium/examples”. In this directory there is a full set of sample scripts covering most aspects of the available functionality. Note: To be able to run the sample scripts in the directory where they are located, the “init.php” file in that directory must be edited, replacing line:

with:

The connectivity with solr can be tested running the script “1.1-check-solarium-and-ping.php”:

In our case, the output indicates that Solarium 3.2.0 has been detected, but there is an error connecting to solr. This is because in our test system we are running solr under a Tomcat container, that listens to incoming connections on port 8080, instead of the default port 8369 used by the sample Jetty container included with the solr installation package. To fix this, create a configuration file “config.php” as follows (the sample “config.dist.php” file can be copied to “config.php” and edited):

Then execute again the test script:

Now, as we can see, the connection to solr has been successful, and a status code “OK” has been returned.

4. Query Types

Looking at the sample scripts included in the installation package, we can see that the code always starts with the creation of a “client” object. Using this object, a second object of type “Query” is retrieved to perform searches and updates. For instance, to make a simple query:

In this example, a query of type SELECT is obtained calling method “createQuery” with an argument that specifies the desired type of query “$client::QUERY_SELECT”. Instead, the equivalent method “createSelect()” could have been called to obtain the same query object:

The “client” object implements a specific method for each type of action that can be carried out on the solr database:

References

Installation – SolariumWiki Packagist – Solarium

 Posted by at 8:08 am

 Leave a Reply

(required)

(required)