从 Zend_Lucene 迁移到 Solr
在我们的项目(基于 Zend Framework)中,我们必须找到默认 Zend_Lucene 的替代品。现在我正在尝试使用 PHP Solr Client 来实现 Solr。 我们有两个表,用于获取数据:类别和优惠。
在 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 客户端中使用的方法相同
,我更改了该代码(使用默认示例 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 方法?
In our project (based on Zend Framework) we have to find a replacement for default Zend_Lucene. Now I'm trying to implement Solr with PHP Solr Client in it.
We have 2 tables, where we take the data: categories and offers.
In Zend_Lucene addition data to index go that way:
/*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*/
The same methods we have for categories/
In Solr and PHP Solr Client, I've changed that code (using default example 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();
}
But search in index gives me 0! I have a suspicion, that Solr doesn't make proper index. (But there are no bug messages or exeptions during index creation). Also I'd try to give Solr only text and id poles in methods. But result was the same!
What I'm doing wrong? Did I change the Zend_Lucene methods correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 Solr 中内置的“DataImportHandler”,将数据从数据库导入到 Solr 引擎。
它将为您完成这项工作,您可以配置“完全导入”,它将导入所有数据库和“增量导入”,它将仅从数据库导入新数据。您也可以配置“删除”,以删除已删除的数据库数据。
I would recommend you to use the "DataImportHandler" built in Solr, to import data from a DataBase to the Solr engine.
It will do the job for you, and you can configure "full-import" which will import all the database and "delta-import" which will just import the new data from the database. You can configure "delete" too, to remove deleted database data.