尝试使用Zend_Search_Lucene搜索MySql数据库并显示结果。运行以下代码,无法弄清楚为什么我没有得到任何信息到我的索引。 PHP,MySql和Zend相当新,但我上周花了很多时间试图解决这个问题,并且已经耗尽了我能找到的所有资源。无论如何,我回应了我的数据库中出现的内容,以确保我的查询工作似乎没问题。它还可以很好地创建索引文件,但是当我使用Luke Toolbox查看它时,我只得到一个用gooseggs填充的字段列表。为了确保我正确使用它,我发现了一些使用Zend_Feed的代码并通过Lukes运行并获得了各种结果。也可以在我的结果测试页面上从相同的代码获得结果,但在使用下面的代码时得到0。似乎我无法获取索引的数据库信息,尽管计数告诉我已经索引了5个文档(这是我在数据库表中的行数)并且我没有收到任何错误。库脚本只是一个自动加载器脚本,我设置了我已经确认也可以工作的数据库连接。虽然我很可能错过了显而易见的事情或者证明了我真正的新手,但是我会非常感激任何帮助。谢谢。
<?php>
require_once('scripts/library.php');
require_once ('Zend/Search/Lucene.php');
$index = Zend_Search_Lucene::create('./docindex');
$sql = ('SELECT * FROM news');
foreach ($db->query($sql) as $row){
echo $row ['author'];
echo $row['headline'];
echo $row ['source'];
}
foreach ($row as $document){
$document = new Zend_Search_Lucene_Document ();
$document->addField(Zend_Search_Lucene_Field::Text ('author', $docAuthor));
$document->addField(Zend_Search_Lucene_Field::Text ('headline', $docHeadline));
$document->addField(Zend_Search_Lucene_Field::Text ('source', $docSource));
$document->addField(Zend_Search_Lucene_Field::Unstored ('contents', $docStory));
$index->addDocument($document);
}
$index->commit();
echo $index->count()." documents have been indexed.\n";
?>
答案 0 :(得分:1)
看起来你有一个额外的foreach()
并没有真正做任何事情,我看不到你的数据变量被分配到哪里,试试这个应该非常接近:
<?php
require_once('scripts/library.php');
require_once ('Zend/Search/Lucene.php');
$index = Zend_Search_Lucene::create('./docindex');
$sql = ('SELECT * FROM news');
foreach ($db->query($sql) as $row) {
echo $row ['author'];
echo $row['headline'];
echo $row ['source'];
$document = new Zend_Search_Lucene_Document ();
//you use an unindexed field for the id because you want the
//id to be included in the search results but not searchable
$document->addField(Zend_Search_Lucene_Field::unIndexed('id', $row['id']));
$document->addField(Zend_Search_Lucene_Field::Text('author', $row ['author']));
$document->addField(Zend_Search_Lucene_Field::Text('headline', $row['headline']));
$document->addField(Zend_Search_Lucene_Field::Text('source', $row ['source']));
$document->addField(Zend_Search_Lucene_Field::Unstored('contents', $row['docStory']));
$index->addDocument($document);
}
$index->commit();
echo $index->count() . " documents have been indexed.\n";
?>