【发布时间】:2016-08-10 12:58:25
【问题描述】:
我正在为基于 Precurio 的 Intranet 开发新模块,Precurio 是一个 Intranet PHP + MySQL 应用程序,曾经有免费版本。
本内网使用 Zend Framework 2.0,并使用 Lucene 提供搜索结果。每次我向 Intranet 添加一个新模块时,我都会添加功能来索引用户插入的模块的内容。
问题是,搜索工作正常,但前提是我从头开始重建所有索引。每当我向模块添加新内容时,它们都不会出现在搜索结果中(在完全重建索引之前存在的内容仍然显示为搜索结果)。
我很困惑,因为重建整个索引的函数只是调用一个循环添加单个项目的函数,所以据我所知,它应该是一样的。但显然,它没有。
这是我目前正在开发的模块的索引功能代码:
/**
* Adds a cabinet to the index
* @param $cabinet int | CabinetContent . you could either pass the cabinet_id or the CabinetContent object
* @return void
*/
public function indexCabinet($cabinet,$indexing = false)
{
if(!is_a($cabinet,'CabinetContent')) {
$cabinet = CabinetContents::getContent($cabinet);
}
if(Precurio_Utils::isNull($cabinet->title))
return;//cabinets without a title will be ignored.
//check if the cabinet already exists
if(!$indexing)
{
$hits = $this->index->find('id:' . $cabinet->id.' AND module:cabinet');
foreach ($hits as $hit)
{
$this->index->delete($hit->id);
}
}
try
{
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::keyword('id',$cabinet->id));
$doc->addField(Zend_Search_Lucene_Field::text('title',$cabinet->title));
if( $cabinet->identifier != '' )
$doc->addField(Zend_Search_Lucene_Field::text('identifier',$cabinet->identifier));
if( $cabinet->summary != '' )
$doc->addField(Zend_Search_Lucene_Field::text('summary',$cabinet->summary));
if( $cabinet->body != '' )
$doc->addField(Zend_Search_Lucene_Field::text('body',$cabinet->body));
$doc->addField(Zend_Search_Lucene_Field::keyword('user_id',$cabinet->user_id));
if( ($cabinet->url != '#') && ($cabinet->url != '') )
$doc->addField(Zend_Search_Lucene_Field::text('url',$cabinet->url));
if( $cabinet->keyword != '' )
$doc->addField(Zend_Search_Lucene_Field::text('keyword',$cabinet->keyword));
if( $cabinet->getFullName() != '' )
$doc->addField(Zend_Search_Lucene_Field::text('fullname',$cabinet->getFullName()));
$doc->addField(Zend_Search_Lucene_Field::unIndexed('date_added',$cabinet->getDateAdded()));
$doc->addField(Zend_Search_Lucene_Field::keyword('module','cabinet'));
}
catch(Exception $e)
{
}
$this->index->addDocument($doc);
return;
}
之前的函数似乎只在我运行 createIndexFromDB() 时才有效。这些是用于重新创建索引的函数:
private $index;
public function __construct()
{
if(!file_exists(self::PATH_INDEX))
$this->createIndexFromDb();
else
$this->index = Zend_Search_Lucene::open(self::PATH_INDEX);
}
private function createIndexFromDb()
{
set_time_limit(0);
mkdir(self::PATH_INDEX);
$this->index = Zend_Search_Lucene::create(self::PATH_INDEX);
$this->indexCabinets(); // Cabinets is the module I am developing
/* Here go a lot of indexing for other modules, like $this->indexEmployees(); but I have commented it out for simplicity during developement */
}
private function indexCabinets()
{
$table = new Zend_Db_Table(array('name'=>PrecurioTableConstants::CABINET_CONTENT,'rowClass'=>'CabinetContent'));
$select = $table->select(false);
$select->setIntegrityCheck(false);
$select->setTable($table);
$select = $select->distinct()
->from(array('a' => PrecurioTableConstants::CABINET_CONTENT))
->join(array('b' => PrecurioTableConstants::USERS),'a.user_id = b.user_id',array('first_name','last_name','profile_picture_id'))
->where('a.active=1');
$cabinets = $table->fetchAll($select);
foreach($cabinets as $cabinet)
{
$this->indexCabinet($cabinet,true);
}
}
现在,在我正在开发的模块中,插入新的橱柜内容后,我运行以下代码:
$dict = new My_Search();
$dict->indexCabinet($content_id);
$this->_redirect('/cabinet/view/index');
但它不起作用。我试图用'true'作为第二个参数调用$dict->indexCabinet,也像在搜索类中那样传递$content对象而不是$content_id...我不知道我做错了什么,也没有什么可以尝试的。
我必须明确,所有新模块都是基于 Precurio Intranet 中的原始模块,以及 My_Search 类中的添加。据我所知,在原始代码中索引新内容可能不起作用。任何帮助将不胜感激。
**编辑 10/08/2016:我在上面的代码中添加了原始类构造函数
【问题讨论】:
标签: zend-framework indexing lucene frameworks