Little help with making this basic search function work (Zend Lucene)

I have two php files so far, test.php:

<?php
            include("../config_conn_fordon_db.php");
            include("../config_open_db.php");
            // Fix Special Characters
            mysql_query("SET NAMES 'utf8'") or die(mysql_error()); 
            mysql_query("SET CHARACTER SET 'utf8'") or die(mysql_error());

            $root = realpath($_SERVER["DOCUMENT_ROOT"]);
            set_include_path($root.'/SV/bincgi/zend/library/');
            require_once('/library/Zend/Db/Adapter/Pdo/Mysql.php');
            require_once('/library/Zend/Search/lucene.php');

            $db = Zend_Db::factory('Pdo_Mysql', array(
            'host'     => 'localhost',
            'username' => 'user',
            'password' => 'password',
            'dbname'   => 'fordon_db'
            ));
            $query="SELECT * FROM cars_db";
            $result = $db->fetchAll($query, 2);
            echo $result[0]['headline'];


            $index = Zend_Search_Lucene::create('/hej/test', true);
            $doc = new Zend_Search_Lucene_Document();

            $doc->addField(Zend_Search_Lucene_Field::Keyword('headline', $result[0]['headline']));
            $index->addDocument($doc);
            $index->commit();

?>

AND search.php:

<?php
            include("../config_conn_fordon_db.php");
            include("../config_open_db.php");
            // Fix Special Characters
            mysql_query("SET NAMES 'utf8'") or die(mysql_error()); 
            mysql_query("SET CHARACTER SET 'utf8'") or die(mysql_error());

            $root = realpath($_SERVER["DOCUMENT_ROOT"]);
            set_include_path($root.'/SV/bincgi/zend/library/');
            //require_once('/library/Zend/Db/Adapter/Pdo/Mysql.php');
            require_once('/library/Zend/Search/lucene.php');

            /*$db = Zend_Db::factory('Pdo_Mysql', array(
            'host'     => 'localhost',
            'username' => 'user',
            'password' => 'password',
            'dbname'   => 'fordon_db'
            ));
            $query="SELECT * FROM cars_db";
            $result = $db->fetchAll($query, 2);
            echo $result[0]['headline'];
            */

            $index = Zend_Search_Lucene::open('/hej/test', true);
            //$doc = new Zend_Search_Lucene_Document();
            echo "Index contains {$index->count()} documents.\n";
            $query = "";
            $hits = $index->find($query);
            echo "Search for \"$query\" returned " .count($hits). " hits.\n\n";
            echo "HEADLINE: "; echo $hits->headline;

?>

I have TWO problems here, Firstly I don't get any results at all. I am not sure if the value of 'headline' is actually inserted (or indexed) as it should be, is there anyway to check if it has indexed it proberly?

Second, I get this error message:

Notice: Trying to get property of non-object in C:\wamp\www\SV\bincgi\zend\search.php on line 30

which is the last line in search.php where I echo Headline...

What I am trying to do is to get all 'headline' fields from a mysql table and index them so that I can search them later on, but I am starting with just one right now.

Thanks

BTW: When I echo $result[0]['headline'] in test.php I get the result from the mysql table field, so it works that far!

Answers


$hits = $index->find($query);

The value returned is an array; that is why you get that error message.

The returned value is normally used in code like the following:

foreach ($hits as $hit) {
    $title    = $hit->title;
    $contents = $hit->contents;
    ...
}

Need Your Help

How to use instance variable in where section of Spock Test

spock

Spock only allows static variables to be accessed from where block.