Zend_Search_Lucene: is id a reserved word?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend_Search_Lucene: is id a reserved word?

Josh Yin
i got some strange result when using id as keyword name, can anybody
explain the details?
tnx.

{{{
epaulin@yinhm:tests$ phpunit ZendSearchTest
PHPUnit 2.3.5 by Sebastian Bergmann.

..F

Time: 0.0337588787079
There was 1 failure:
1) testIsIDAReservedWord(ZendSearchTest)
Stored index id expected same: <26> was not: <0>
/home/httpd/goodjobs/edu/tests/ZendSearchTest.php:76

FAILURES!!!
Tests run: 3, Failures: 1, Errors: 0, Incomplete Tests: 0.

}}}

<?php

require_once 'PHPUnit2/Framework/TestCase.php';

require_once 'Zend/Search/Lucene.php';

class ZendSearchTest extends PHPUnit2_Framework_TestCase {

        function setUp() {
                $this->indexDir = '/tmp/test-index';
                $this->linkID = 26;
                $this->title = "Google";
                $this->content = 'Google GOOGLE';
        }
       
        function testBuildIndex() {
                try {

                        $index = new Zend_Search_Lucene('/tmp/test-index', true);

                        $doc = new Zend_Search_Lucene_Document();

                        $doc->addField(Zend_Search_Lucene_Field::Keyword( 'linkID', $this->linkID ));
                        $doc->addField(Zend_Search_Lucene_Field::Text( 'title', $this->title ));
                        $doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
$this->content));
                        $index->addDocument($doc);

                        $index->commit();

                } catch (Exception $e) {
                        $this->assertEquals(true, false, $e->getMessage());
                }

                $this->assertEquals(true, true, 'Index webpage');
        }
       
        function testSearchHitIDEquilWithDocID() {

                $keywords = 'google';
                $index = new Zend_Search_Lucene('/tmp/test-index');
                $hits = $index->find($keywords);
                foreach ($hits as $hit) {
                        $doc = $hit->getDocument();
                        break;
                }
               
                $this->assertEquals($doc->linkID, $hit->linkID, 'Stored index id');
               
        }
       
        function testIsIDAReservedWord() {
               
                try {

                        $index = new Zend_Search_Lucene('/tmp/test-index', true);

                        $doc = new Zend_Search_Lucene_Document();

                        $doc->addField(Zend_Search_Lucene_Field::Keyword( 'id', $this->linkID ));
                        $doc->addField(Zend_Search_Lucene_Field::Text( 'title', $this->title ));
                        $doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
$this->content));
                        $index->addDocument($doc);

                        $index->commit();

                } catch (Exception $e) {
                        $this->assertEquals(true, false, $e->getMessage());
                }

                $keywords = 'google';
                $hits = $index->find($keywords);
                foreach ($hits as $hit) {
                        $doc = $hit->getDocument();
                        break;
                }
               
                $this->assertEquals($doc->id, $hit->id, 'Stored index id');
               
        }
       
}

?>
Reply | Threaded
Open this post in threaded view
|

Re: Zend_Search_Lucene: is id a reserved word?

Haddad Said
Hi,

I think this is because Lucene uses 'id' field internally to identify
its documents.

HaiMing.Yin wrote:

> i got some strange result when using id as keyword name, can anybody
> explain the details?
> tnx.
>
> {{{
> epaulin@yinhm:tests$ phpunit ZendSearchTest
> PHPUnit 2.3.5 by Sebastian Bergmann.
>
> ..F
>
> Time: 0.0337588787079
> There was 1 failure:
> 1) testIsIDAReservedWord(ZendSearchTest)
> Stored index id expected same: <26> was not: <0>
> /home/httpd/goodjobs/edu/tests/ZendSearchTest.php:76
>
> FAILURES!!!
> Tests run: 3, Failures: 1, Errors: 0, Incomplete Tests: 0.
>
> }}}
>
> <?php
>
> require_once 'PHPUnit2/Framework/TestCase.php';
>
> require_once 'Zend/Search/Lucene.php';
>
> class ZendSearchTest extends PHPUnit2_Framework_TestCase {
>
>     function setUp() {
>         $this->indexDir = '/tmp/test-index';
>         $this->linkID = 26;
>         $this->title = "Google";
>         $this->content = 'Google GOOGLE';
>     }
>    
>     function testBuildIndex() {
>         try {
>
>             $index = new Zend_Search_Lucene('/tmp/test-index', true);
>
>             $doc = new Zend_Search_Lucene_Document();
>
>             $doc->addField(Zend_Search_Lucene_Field::Keyword(
> 'linkID', $this->linkID ));
>             $doc->addField(Zend_Search_Lucene_Field::Text( 'title',
> $this->title ));
>             $doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
> $this->content));
>             $index->addDocument($doc);
>
>             $index->commit();
>
>         } catch (Exception $e) {
>             $this->assertEquals(true, false, $e->getMessage());
>         }
>
>         $this->assertEquals(true, true, 'Index webpage');
>     }
>    
>     function testSearchHitIDEquilWithDocID() {
>
>         $keywords = 'google';
>         $index = new Zend_Search_Lucene('/tmp/test-index');
>         $hits = $index->find($keywords);
>         foreach ($hits as $hit) {
>             $doc = $hit->getDocument();
>             break;
>         }
>        
>         $this->assertEquals($doc->linkID, $hit->linkID, 'Stored index
> id');
>        
>     }
>    
>     function testIsIDAReservedWord() {
>        
>         try {
>
>             $index = new Zend_Search_Lucene('/tmp/test-index', true);
>
>             $doc = new Zend_Search_Lucene_Document();
>
>             $doc->addField(Zend_Search_Lucene_Field::Keyword( 'id',
> $this->linkID ));
>             $doc->addField(Zend_Search_Lucene_Field::Text( 'title',
> $this->title ));
>             $doc->addField(Zend_Search_Lucene_Field::UnStored('contents',
> $this->content));
>             $index->addDocument($doc);
>
>             $index->commit();
>
>         } catch (Exception $e) {
>             $this->assertEquals(true, false, $e->getMessage());
>         }
>
>         $keywords = 'google';
>         $hits = $index->find($keywords);
>         foreach ($hits as $hit) {
>             $doc = $hit->getDocument();
>             break;
>         }
>        
>         $this->assertEquals($doc->id, $hit->id, 'Stored index id');
>        
>     }
>    
> }
>
> ?>