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.
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.
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.
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.
Lets update the index. Open the SearchController.php file and create a new action, updateIndexAction, as shown in Listing.
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.
Zend Related Interview Questions
|CSS3 Interview Questions||HTML Interview Questions|
|PHP Interview Questions||MySQL Interview Questions|
|Linux Interview Questions||Drupal Interview Questions|
|Magento Interview Questions||MVC Framework Interview Questions|
|CSS Interview Questions||WordPress Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
Getting Started With Zend Framework
Writing Controllers Using Zend_controller
Views, Forms, Filters, And Validators
Database Communication, Manipulation, And Display
Sending And Receiving E-mail
Web Services And Feeds
Creating A Search Engine Using Zend_search_lucene
Caching With Zend Framework
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.