从Zend_Lucene迁移到Solr

时间:2011-01-19 12:34:35

标签: php zend-framework search solr zend-search-lucene

在我们的项目中(基于Zend Framework),我们必须找到默认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!我怀疑索尔没有做出正确的索引。 (但在创建索引期间没有错误消息或exeptions)。另外,我会尝试在方法中仅给出Solr文本和id极点。但结果是一样的!

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

1 个答案:

答案 0 :(得分:2)

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

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