1

在我们的项目中(基于 Zend 框架),我们必须找到默认 Zend_Lucene 的替代品。现在我正在尝试在其中使用 PHP Solr Client 来实现 Solr。我们有 2 个表格,我们在其中获取数据:类别和优惠。

在 Zend_Lucene 中将数据添加到索引中:

/*Code above we create new index and take data from mysql
And here are the old methods:
offer - is array with query results
*/


$to_index = "{$offer["name"]} {$offer["type"]} {$offer["description"]}";

 $doc = new Zend_Search_Lucene_Document();
 $doc->addField( Zend_Search_Lucene_Field::Text('text', $to_index, "utf-8") );
 $doc->addField( Zend_Search_Lucene_Field::Keyword('cat_id', $offer["cat_id"]) );
 $doc->addField( Zend_Search_Lucene_Field::Keyword('type', "offer") );
 $doc->addField( Zend_Search_Lucene_Field::Keyword('id', $offer["id"]) );
 $doc->addField( Zend_Search_Lucene_Field::UnIndexed('created', time()) );

$this->index->addDocument($doc);

/*End of old code*/

我们对类别/有相同的方法

在 Solr 和 PHP Solr Client 中,我更改了该代码(使用默认示例 schema.xml):

$to_index = "{$category["name"]}";

$doc = new Apache_Solr_Document();
$doc->text = $to_index;
$doc->type = "category";
$doc->id = $category["id"];
$doc->created = time();

try {
     $this->solr->addDocuments($doc);
     $this->solr->commit();
     $this->solr->optimize();
}
catch ( Exception $e ) {
     echo $e->getMessage();
}

但是在索引中搜索给了我 0!我有一个怀疑,Solr 没有做出正确的索引。(但在索引创建期间没有错误消息或异常)。此外,我会尝试在方法中只给 Solr 提供文本和 id 极点。但结果是一样的!

我做错了什么?我是否正确更改了 Zend_Lucene 方法?

4

1 回答 1

2

我建议您使用 Solr 中内置的“DataImportHandler”,将数据从数据库导入到 Solr 引擎。

它会为您完成这项工作,您可以配置“full-import”将导入所有数据库和“delta-import”将仅从数据库中导入新数据。您也可以配置“删除”,以删除已删除的数据库数据。

于 2011-01-19T12:43:41.833 回答