When data sets get large and MySQL database querying to search become too load heavy and slow, full indexing is required. Several solutions are available but in this article I will be demonstrating the Apache foundations Solr Java Lucene implementation. For this a Java build will be required. Linux or Mac is less of a problem but for windows I use the Apache Tomcat server.

<schema name="example" version="1.1">
  <fields>
  <field name="id" type="string" indexed="true" stored="true" required="true" />
  <field name="name" type="text" indexed="true" stored="true"/>
  <field name="nameSort" type="string" indexed="true" stored="false"/>
  <field name="cat" type="text" indexed="true" stored="true" multiValued="true"/>
  </fields>
  <uniqueKey>id</uniqueKey>
  <copyField source="name" dest="nameSort"/>
</schema>

Above is an example of a default Solr schema. In this example the information is stored as id: primary key, name: text index, category: sortable, category: array of categories.

Obviously you will change these values to match the data that you wish to store.

Now for the PHP bit:

we need to include the Solr Service file as follows:

require_once 'Apache/Solr/Service.php';

To create a connection to the Solr service we need a host, port and default root, which can be called as follows:

$solr = new Apache_Solr_Service(
        'localhost',
        '8983,
        '/solr'
);

To test this connection we can use a ping function.

if(!$solr->ping())
{
    //Service not responding
    exit;
}

In this example I am assuming that we have an array ($foo) which contains an array of news objects returned from the database. We must now loop over each object and index it.

$documents = array();

foreach($foo as $item => $content)
{
    $document = new Apache_Solr_Document();

    if(is_array($content))
    {
        foreach($content as $sub_item => $sub_data)
        {
             $document->setMultiValue($sub_item, $sub_data);
        }
    }
    else
    {
        $document->$item = $content;
    }

    $documents[] = $document
}

try
{
    $solr->addDocuments();
    $solr->commit();
    $solr->optimize();
}
catch(Exception $e)
{
    echo $e->getMessage();
}

And finally to actually search. For this we will have an array ($terms) which is an exploded array of terms from a search string.

foreach($terms as $term)
{
    $response = $solr->search($query, 0, 10);

    if($response->response->numFound > 0)
    {
        foreach($response->response->docs as $res)
        {
            //Output the object
        }
    }
    else
    {
        echo 'no results found, sorry!'
    }
}