【问题标题】:Adding content to a Lucene index in Zend Framework在 Zend Framework 中向 Lucene 索引添加内容
【发布时间】: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


    【解决方案1】:

    我不太确定我是否确切了解了您提供的代码是如何被使用的(我对 Zend 的熟悉是在最好的情况下触手可及),但这听起来与一个非常常见的 lucene 错误一致:创建一个新的索引,而不是打开现有的索引。

    您应该使用Zend_Search_Lucene::create 创建一个全新的索引。给定位置的任何现有索引都将被删除。

    您应该使用Zend_Search_Lucene::open 打开现有索引。

    请参阅此 Zend "Building Indexes" documentation 中的前两个示例。

    【讨论】:

    • 我明天上班试试,如果成功了再告诉你。谢谢!
    • 太好了,这让我通过调整您提供的链接使其工作,在每个索引函数的开头添加以下行:“$index = Zend_Search_Lucene::open(self: :PATH_INDEX);" .尽管如此,我还是不明白为什么原始代码不起作用:我认为它也是如此,但它没有使用 $index,而是使用了类私有变量 $this->index,它在构造函数中打开了索引: $this->index = Zend_Search_Lucene::open(self::PATH_INDEX);我编辑了原始问题以添加构造函数代码
    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多