ploebach
ploebach

Reputation: 1

Problems getting MySql data into an index using Zend_Search_Lucene

Trying to use Zend_Search_Lucene to search a MySql database and display the results. Running the following code and can't figure out why I'm not getting any info into my index. Quite new with PHP, MySql and Zend but I've spent the last week trying to figure this out on my own and have exhausted every resource I could find. Anyway, I echoed what's coming out of my database to make sure my query worked which seems to be OK. It also creates the index files just fine but when I've taken a look at it with the Luke Toolbox I get nothing but a list of the fields created filled with gooseggs. Just to make sure I'm using that correctly I found some code using Zend_Feed and ran that through Lukes and got all kinds of results. Also able to get results from that same code on my result test page but getting 0 when using the code below. Seems I can't get the database info indexed although the count tells me that 5 documents have been indexed (which is the number of rows I have in my database table) and I'm not getting any errors. The library script is just an autoloader script and where I set up my db connection which I've confirmed works as well. Although it's likely I'm missing the obvious or demonstrating what a novice I truly am, any help would be much appreciated.Thanks.

<?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";
?>

Upvotes: 0

Views: 446

Answers (1)

RockyFord
RockyFord

Reputation: 8519

It looks like you had an extra foreach() that wasn't really doing anything and I couldn't see where your data variables were assigned, try this it should be pretty close:

<?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";
?>

Upvotes: 1

Related Questions