开发者

How do i use zend_search_lucene

I am beginner,I am trying to use zend_search_lucene in my sample project.i have following code in searchController.php in controllers folder

  <?php class searchController extends Zend_Controller_Action {

        public function init()
        {
            /* Initialize action controller here */
        }

        public function indexAction()
        {

        $query = $_GET['q'];

        if (is_file(APPLICATION_PATH . '/data/search/index')) { 
        $index = Zend_Search_Lucene::open(APPLICATION_PATH . '/data/search/index');  开发者_开发问答

    } else { 
        $index = Zend_Search_Lucene::create(APPLICATION_PATH . '/data/search/index');      } 
      $sql = "select empname, empaddress from addemployee";
         $dbconnection = new Default_Model_DBTable_Employee();
         $results = $dbconnection->fetchAll();   

       foreach ($results as $result) {
        $doc = new Zend_Search_Lucene_Document();

        // Store document URL to identify it in the search results
        $doc->addField(
        Zend_Search_Lucene_Field::Text('Name', $result->empname));

        // Index document title
        $doc->addField(
        Zend_Search_Lucene_Field::Text('EmployeeAddress ', $result->empaddress));

        // Add document to the index
        $index->addDocument($doc); }

    // Optimize index
 $index->optimize();
   // Search by query
    $this->view->hits = $index->find($query);
        } } ?>

Also have the following code in layout.phtml

        <h3>Search:</h3>
<form method="get" action="/quickstart/public/search">
<input type="text" name="q" value="">
<input type="submit" name="search" value="Go">
</form>

Also have the following code inviews/scripts/search/index.phtml

<?php foreach ($this->hits as $hit) {
    echo $hit->score . " ";
    echo $hit->Name . " ";
    } ?>

when i enter text in search box , search result is not coming. what i done wrong on the code? kindly help me


Well, you never actually perform a search in your code, so it's quite normal that you don't get search results. If you want to search your index, use the find method, passing a Lucene search query as parameter. If you're looking for a complete example, you could have a look at Roll Your Own Search Engine with Zend_Search_Lucene.


The basic process for creating an index using Zend_Search_Lucene is:

  1. Open the index
  2. Add each document
  3. Commit (save) the index

It looks like you're missing the last step here, by that I mean, you are not saving anything to your index.

Right after closing your foreach loop, You need to add:

$index->commit()


Its working by placing "$this->view->hits = $index->find($query);" after optimizing the index "$index->optimize();"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜