Author: dr
Date: Thu Feb 21 16:27:06 2008
New Revision: 7423
Log:
- Added simple indexing.
Added:
trunk/Search/tests/session_test.php (with props)
trunk/Search/tests/testfiles/
trunk/Search/tests/testfiles/article.php (with props)
trunk/Search/tests/testfiles/article.xml
Modified:
trunk/Search/design/class_diagram.png
trunk/Search/src/handlers/solr.php
trunk/Search/src/interfaces/index_handler.php
trunk/Search/src/search_autoload.php
trunk/Search/src/search_session.php
trunk/Search/tests/handlers/solr_test.php
trunk/Search/tests/suite.php
Modified: trunk/Search/design/class_diagram.png
==============================================================================
Binary files - no diff available.
Modified: trunk/Search/src/handlers/solr.php
==============================================================================
--- trunk/Search/src/handlers/solr.php [iso-8859-1] (original)
+++ trunk/Search/src/handlers/solr.php [iso-8859-1] Thu Feb 21 16:27:06 2008
@@ -179,8 +179,67 @@
{
}
- public function index( $document )
- {
+ private function mapFieldType( $name, $type )
+ {
+ $map = array(
+ ezcSearchDocumentDefinition::STRING => '_s',
+ ezcSearchDocumentDefinition::TEXT => '_t',
+ ezcSearchDocumentDefinition::HTML => '_h',
+ ezcSearchDocumentDefinition::DATE => '_dt',
+ );
+ return $name . $map[$type];
+ }
+
+ private function mapFieldValue( $type, $value )
+ {
+ switch( $type )
+ {
+ case ezcSearchDocumentDefinition::DATE:
+ if ( is_numeric( $value ) )
+ {
+ $d = new DateTime( "@$value" );
+ return $d->format( 'Y-m-d\TH:i:s\Z' );
+ }
+ else
+ {
+ try
+ {
+ $d = new DateTime( $value );
+ }
+ catch ( Exception $e )
+ {
+ throw new ezcSearchInvalidValueException( $type,
$value );
+ }
+ return $d->format( 'Y-m-d\TH:i:s\Z' );
+ }
+ default:
+ return $value;
+ }
+ }
+
+ public function index( ezcSearchDocumentDefinition $definition, $document )
+ {
+ $xml = new XmlWriter();
+ $xml->openMemory();
+ $xml->startElement( 'add' );
+ $xml->startElement( 'doc' );
+ $xml->startElement( 'field' );
+ $xml->writeAttribute( 'name', 'id' );
+ $xml->text( $document[$definition->idProperty] );
+ $xml->endElement();
+ foreach ( $definition->fields as $field )
+ {
+ $xml->startElement( 'field' );
+ $xml->writeAttribute( 'name', $this->mapFieldType( $field->field,
$field->type ) );
+ $xml->text( $this->mapFieldValue( $field->type,
$document[$field->field] ) );
+ $xml->endElement();
+ }
+ $xml->endElement();
+ $xml->endElement();
+ $doc = $xml->outputMemory( true );
+
+ $r = $this->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
$doc );
+// $r = $this->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
'<commit/>' );
}
public function createDeleteQuery()
Modified: trunk/Search/src/interfaces/index_handler.php
==============================================================================
--- trunk/Search/src/interfaces/index_handler.php [iso-8859-1] (original)
+++ trunk/Search/src/interfaces/index_handler.php [iso-8859-1] Thu Feb 21
16:27:06 2008
@@ -16,7 +16,7 @@
*/
interface ezcSearchIndexHandler
{
- public function index( $document );
+ public function index( ezcSearchDocumentDefinition $definition, $document
);
public function createDeleteQuery();
public function delete( ezcSearchDeleteQuery $query );
}
Modified: trunk/Search/src/search_autoload.php
==============================================================================
--- trunk/Search/src/search_autoload.php [iso-8859-1] (original)
+++ trunk/Search/src/search_autoload.php [iso-8859-1] Thu Feb 21 16:27:06 2008
@@ -21,7 +21,9 @@
'ezcSearchQuery' =>
'Search/abstraction/query.php',
'ezcSearchCodeManager' =>
'Search/managers/code_manager.php',
'ezcSearchDefinitionDocumentField' =>
'Search/structs/document_field_definition.php',
+ 'ezcSearchDeleteHandler' =>
'Search/handlers/delete_handler.php',
'ezcSearchDocumentDefinition' =>
'Search/structs/document_definition.php',
+ 'ezcSearchFindHandler' =>
'Search/handlers/find_handler.php',
'ezcSearchFindQuery' =>
'Search/abstraction/query_find.php',
'ezcSearchQueryInsert' =>
'Search/abstraction/query_index.php',
'ezcSearchResult' =>
'Search/search_result.php',
Modified: trunk/Search/src/search_session.php
==============================================================================
--- trunk/Search/src/search_session.php [iso-8859-1] (original)
+++ trunk/Search/src/search_session.php [iso-8859-1] Thu Feb 21 16:27:06 2008
@@ -42,9 +42,6 @@
{
$this->properties['handler'] = $handler;
$this->properties['definitionManager'] = $manager;
- $this->properties['findHandler'] = new ezcPersistentFindHandler(
$this );
- $this->properties['indexHandler'] = new
ezcPersistentIndexHandler( $this );
- $this->properties['deleteHandler'] = new
ezcPersistentDeleteHandler( $this );
}
/**
@@ -152,7 +149,14 @@
*/
public function index( $document )
{
- return $this->indexHandler->index( $document );
+ $class = get_class( $document );
+ $def = $this->definitionManager->fetchDefinition( $class );
+ $state = $document->getState();
+ if ( $state[$def->idProperty] == null )
+ {
+ $document->setState( array( $def->idProperty => uniqid() ) );
+ }
+ $this->handler->index( $def, $document->getState() );
}
/**
@@ -165,8 +169,8 @@
*/
public function update( $document )
{
- $this->indexHandler->delete( $document );
- return $this->indexHandler->index( $document );
+ $this->delete( $document );
+ return $this->index( $document );
}
/**
Modified: trunk/Search/tests/handlers/solr_test.php
==============================================================================
--- trunk/Search/tests/handlers/solr_test.php [iso-8859-1] (original)
+++ trunk/Search/tests/handlers/solr_test.php [iso-8859-1] Thu Feb 21 16:27:06
2008
@@ -31,6 +31,10 @@
{
self::markTestSkipped( 'Solr is not running.' );
}
+ $this->solr->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
+ '<delete><query>timestamp:[* TO *]</query></delete>' );
+ $this->solr->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
+ '<commit/>' );
}
function testUnableToConnect()
Added: trunk/Search/tests/session_test.php
==============================================================================
--- trunk/Search/tests/session_test.php (added)
+++ trunk/Search/tests/session_test.php [iso-8859-1] Thu Feb 21 16:27:06 2008
@@ -1,0 +1,82 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package Search
+ * @subpackage Tests
+ */
+
+require 'testfiles/article.php';
+
+/**
+ * Test the handler classes.
+ *
+ * @package Search
+ * @subpackage Tests
+ */
+class ezcSearchSessionTest extends ezcTestCase
+{
+ public static function suite()
+ {
+ return new PHPUnit_Framework_TestSuite( "ezcSearchSessionTest" );
+ }
+
+ public function setUp()
+ {
+ $this->backend = new ezcSearchSolrHandler();
+ $this->testFilesDir = dirname( __FILE__ ) . '/testfiles/';
+ $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
+ '<delete><query>timestamp:[* TO *]</query></delete>' );
+ $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
+ '<commit/>' );
+ }
+
+ public function testCreateSession()
+ {
+ $session = new ezcSearchSession( $this->backend, new
ezcSearchXmlManager( $this->testFilesDir ) );
+ }
+
+ public function testIndexDocument1()
+ {
+ $a = new Article( null, 'Test Article', 'This is an article to test',
'the body of the article', time() );
+
+ $session = new ezcSearchSession( $this->backend, new
ezcSearchXmlManager( $this->testFilesDir ) );
+ $session->index( $a );
+ $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
'<commit/>' );
+
+ $r = $this->backend->search( 'Article', 'title_t' );
+ self::assertEquals( 1, $r->resultCount );
+ }
+
+ public function testIndexDocument2()
+ {
+ $a = new Article( null, 'Test Article', 'This is an article to test',
'the body of the article', time() );
+
+ $session = new ezcSearchSession( $this->backend, new
ezcSearchXmlManager( $this->testFilesDir ) );
+ for ( $i = 0; $i < 100; $i++ )
+ {
+ $session->index( $a );
+ }
+ $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
'<commit/>' );
+
+ $r = $this->backend->search( 'Article', 'title_t' );
+ self::assertEquals( 1, $r->resultCount );
+ }
+
+ public function testIndexDocument3()
+ {
+ $d = file_get_contents(
'/tmp/ezcomponents-2007.2.1/WorkflowEventLogTiein/ezcWorkflowEventLogListener.html'
);
+ $a = new Article( null, 'Test Article', 'This is an article to test',
$d, time() );
+
+ $session = new ezcSearchSession( $this->backend, new
ezcSearchXmlManager( $this->testFilesDir ) );
+ $session->index( $a );
+ $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ),
'<commit/>' );
+
+ $r = $this->backend->search( 'Article', 'title_t' );
+ self::assertEquals( 1, $r->resultCount );
+ }
+}
+
+?>
Propchange: trunk/Search/tests/session_test.php
------------------------------------------------------------------------------
svn:eol-style = native
Modified: trunk/Search/tests/suite.php
==============================================================================
--- trunk/Search/tests/suite.php [iso-8859-1] (original)
+++ trunk/Search/tests/suite.php [iso-8859-1] Thu Feb 21 16:27:06 2008
@@ -11,8 +11,9 @@
/**
* Including the tests
*/
-require_once 'handlers/solr_test.php';
-require_once 'managers/xml_test.php';
+require 'handlers/solr_test.php';
+require 'managers/xml_test.php';
+require 'session_test.php';
/**
* @package Search
@@ -27,6 +28,7 @@
$this->addTest( ezcSearchXmlDefinitionManager::suite() );
$this->addTest( ezcSearchHandlerSolrTest::suite() );
+ $this->addTest( ezcSearchSessionTest::suite() );
}
public static function suite()
Added: trunk/Search/tests/testfiles/article.php
==============================================================================
--- trunk/Search/tests/testfiles/article.php (added)
+++ trunk/Search/tests/testfiles/article.php [iso-8859-1] Thu Feb 21 16:27:06
2008
@@ -1,0 +1,38 @@
+<?php
+class Article
+{
+ public $id;
+ private $title;
+ private $summary;
+ private $body;
+ private $published;
+
+
+ function __construct( $id, $title, $summary, $body, $published )
+ {
+ $this->id = $id;
+ $this->title = $title;
+ $this->summary = $summary;
+ $this->body = $body;
+ $this->published = $published;
+ }
+
+ function getState()
+ {
+ return array(
+ 'id' => $this->id,
+ 'title' => $this->title,
+ 'summary' => $this->summary,
+ 'body' => $this->body,
+ 'published' => $this->published,
+ );
+ }
+
+ function setState( $state )
+ {
+ foreach ( $state as $key => $value )
+ {
+ $this->$key = $value;
+ }
+ }
+}
Propchange: trunk/Search/tests/testfiles/article.php
------------------------------------------------------------------------------
svn:eol-style = native
Added: trunk/Search/tests/testfiles/article.xml
==============================================================================
--- trunk/Search/tests/testfiles/article.xml (added)
+++ trunk/Search/tests/testfiles/article.xml [iso-8859-1] Thu Feb 21 16:27:06
2008
@@ -1,0 +1,8 @@
+<?xml version="1.0"?>
+<document>
+ <field type="id">id</field>
+ <field type="text" boost="2">title</field>
+ <field type="text">summary</field>
+ <field type="html">body</field>
+ <field type="date">published</field>
+</document>
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components