Blog/ Apache solr Installation and database indexing

Apache solr Installation

- Install jdk first http://www.oracle.com/technetwork/java/javase/downloads/index.html

- Download apache solr from: http://lucene.apache.org/solr/

- Extract it anywhere. Open command prompt. Go to the directory folder for the following path: apache-solr-3.6.2/example/

- Now to start solr, execute: java –jar start.jar

- Solr has inbuilt data for testing purposes. Using this command "java -jar post.jar *.xml", we can add the data to solr indexing to test if its working fine or not. The instructions are given here: http://lucene.apache.org/solr/api-3_6_2/doc-files/tutorial.html

 

- We can check if solr is working fine or not with this link: http://localhost:8983/solr/admin/ping

 

- Now press ctrl+c to stop apache solr


We learnt about integration of an existing xml file and configuration of solr. Now we will try database integration with MySQL. 

- For this, we need the MySQL connector which will establish connection between apache solr and mysql: http://www.mysql.com/downloads/connector/j/

- Extract the zip file and copy the .jar file to apache-solr-3.6.2/example/lib directory

- Now we need to add “data Import” request handler that will create a url to import data from the database. We need to add a request handler to solrconfig.xml file, and also need to define a file where we will define which table and fields needs to be indexed. The handler code is as follows:

Apache Solr installation

More details are available at http://wiki.apache.org/solr/DataImportHandler

- Now the data-config.xml file will be something like this: More details on this is also available on the same page - http://wiki.apache.org/solr/DataImportHandler


    
      data-config.xml
      

- Next is the schema.xml file where you need to describe about the fieldtype you are using and the field information. You will find more details on schema.xml here: http://wiki.apache.org/solr/SchemaXml The files we discussed will be placed at E:/apache-solr-3.6.2/example/solr/conf. Please take a backup of the original file first

- Check if the information added to the file is right or not by executing the ping.- http://localhost:8983/solr/admin/ping

- Now we need to import data of configured database by using this link: http://localhost:8983/solr/dataimport?command=full-import

 


How to integrate it with php?

- Get the php client library from: http://code.google.com/p/solr-php-client/

- Then we need to create a php file which will make a call using that library. The code of that php file will be like this:

require_once( 'SolrPHPClient/Apache/Solr/Service.php' );
  
$solr = new Apache_Solr_Service( 'localhost', '8983', 'solr/');
$ping = $solr->ping();
  
if ( ! $ping ) {
 echo 'Solr service not responding';
 exit;
}

$offset = 0;
$limit = 10;
  
  $queries = array(
    'test string',
    'sort:post_date desc'
  );

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

        foreach ( $response->response->docs as $doc ) { 
          echo "$doc->id --- $doc->post_title";
        }
      }
    }
    else {
      echo $response->getHttpStatusMessage();
    }
  }

This code will ping to apache server. If the ping is successful, it will try to search a string "test string" in your default  search field that you set in solrconfig.xml. The $offset and $limit will define the total number of records. If it finds the result, it will print the id and title of record. Otherwise, it prints the error message.

 
Ready to get started?REQUEST A QUOTE