我如何使用zend_search_lucene

时间:2011-06-23 13:04:26

标签: zend-framework

我是初学者,我正在尝试在我的示例项目中使用zend_search_lucene。我在控制器文件夹中的searchController.php中有以下代码

  <?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);
        } } ?>

在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>

还有以下代码inviews / scripts / search / index.phtml

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

当我在搜索框中输入文字时,搜索结果不会到来。我在代码上做错了什么?请帮助我

3 个答案:

答案 0 :(得分:2)

嗯,你实际上从未在代码中执行搜索,所以你没有获得搜索结果是很正常的。如果要搜索索引,请使用find方法,将Lucene搜索查询作为参数传递。如果您正在寻找完整的示例,可以查看Roll Your Own Search Engine with Zend_Search_Lucene

答案 1 :(得分:1)

使用Zend_Search_Lucene创建索引的基本过程是:

  1. 打开索引
  2. 添加每个文档
  3. 提交(保存)索引
  4. 看起来你错过了这里的最后一步,我的意思是,你没有在索引中保存任何东西。

    关闭foreach循环后,您需要添加:

    $index->commit()
    

答案 2 :(得分:0)

通过放置“$ this-&gt; view-&gt; hits = $ index-&gt; find($ query);”在优化索引“$ index-&gt; optimize();”之后

相关问题