Creating the Foundation - Zend

The next sections cover how to build the foundations of each of the layers of the search engine, from the index to the fields.

Creating the Index

Zend Framework represents an index as a Zend_Search_Lucene object. The Zend_ Search_ Lucene class allows you to create, update, delete, optimize, and add documents. Additional functionality is shown in Table.

Creating the Index

The index is created using the Zend_Search_Lucene class by using its constructor or by using the create() factory method. When you use either one of these methods, you create the physical files required for the index. The index file can grow up to 2GB on a 32-bit system, but can reach larger sizes in a 64-bit system.

After the index files are created, the files will be used to store documents that the user can search through. You’ll look into adding searchable documents later in this chapter.

Create a new controller, SearchController.php, and save it in the application/controllers directory. The controller will be used throughout this chapter, so keep it handy The first action, create Index Action, contains the functionality to create the index.

Listing SearchController::createIndexAction

<?php
/**
* Search Controller
*
*/
class SearchController extends Zend_Controller_Action
{
/**
* Create Index.
*
*/
public function createIndexAction ()
{
//Create an index.
$Index = Zend_Search_Lucene::create('../application/searchindex');
echo 'Index created';
//Suppress the view.
$this->_helper->viewRenderer->setNoRender();
}
}

The new controller action shown in Listing automatically loads the Zend /Search/ Lucene.php file behind the scenes using Zend_ Loader covered in the first chapter which allows us to instantiate a Zend_ Search_ Lucene object . You create the index by implementing the Zend_Search_Lucene factory method create(), which creates index files in the specified path you set as its first parameter. In the example, you create the index files inside the application/searchindex directory, and finish off the action by suppressing view rendering.

Open your browser and load the URL http://localhost/search/create-index. You will see the Index created text printed out on your page, which indicates that the index was properly created. To verify that everything was created successfully, open the application directory. You should see the new directory, searchindex, as well as a number of new files within that directory.

Newly created index

The newly created files will be used to add documents that the user can later search through.

Updating the Index

Updating the index can be done by initializing the Zend_Search_Lucene object and either calling its pen() factory method or setting the second parameter to the constructor as false. Use the open() methods to add new documents into the index instead of overwriting the content currently stored in it. The open() method can also be used when reading the index for searching as well.

Lets update the index. Open the SearchController.php file and create a new action, updateIndexAction, as shown in Listing.

Listing SearchController::updateindexAction

/**
* Update Our Index.
*
*/
public function updateIndexAction()
{
try{
//Update an index.
$Index = Zend_Search_Lucene::open('../application/searchindex');
}catch(Zend_Search_Exception $e){
echo $e->getMessage();
}
echo 'Index Opened for Reading/Updating';
//Suppress the view.
$this->_helper->viewRenderer->setNoRender();
}

Listing demonstrates the open() factory method that updates the index located at application/ search index. In this example we use the open() factory method to open the index file that informs Zend_ Search_ Lucene that you will update (not create) the index file. Now update the index

Now that you understand the index—how it’s created, how it’s updated, and where it’s saved—you’re now ready to add documents to the index for searching.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Zend Topics