Author: dr
Date: Tue Feb 26 10:22:06 2008
New Revision: 7445

Log:
- Added support for HTTP chunked encoding in responses.
- Implemented more Solr query options.
- Added the "inResult" parameter to definitions, that tells which fields should
  be in the result.

Modified:
    trunk/Search/src/handlers/solr.php
    trunk/Search/src/managers/xml_manager.php
    trunk/Search/src/structs/document_field_definition.php
    trunk/Search/tests/session_test.php
    trunk/Search/tests/testfiles/article.php
    trunk/Search/tests/testfiles/article.xml

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] Tue Feb 26 10:22:06 2008
@@ -86,6 +86,7 @@
 
         // read http header
         $line = '';
+        $chunked = false;
         while ( $line != "\r\n" )
         {
             $line = $this->getLine();
@@ -93,16 +94,43 @@
             {
                 $expectedLength = $m[1];
             }
-        }
-
-        // read http content
-        $size = 1;
+
+            if ( preg_match( '@Transfer-Encoding: chunked@', $line ) )
+            {
+                $chunked = true;
+            }
+        }
+
         $data = '';
-        while ( $size < $expectedLength )
-        {
-            $line = $this->getLine( $expectedLength );
-            $size += strlen( $line );
-            $data .= $line;
+        $chunkLength = -1;
+        // read http content with chunked encoding
+        if ( $chunked )
+        {
+            while ( $chunkLength !== 0 )
+            {
+                // fetch chunk length
+                $line = $this->getLine();
+                $chunkLength = hexdec( $line );
+
+                $size = 1;
+                while ( $size < $chunkLength )
+                {
+                    $line = $this->getLine( $chunkLength );
+                    $size += strlen( $line );
+                    $data .= $line;
+                }
+                $line = $this->getLine();
+            }
+        }
+        else // without chunked encoding
+        {
+            $size = 1;
+            while ( $size < $expectedLength )
+            {
+                $line = $this->getLine( $expectedLength );
+                $size += strlen( $line );
+                $data .= $line;
+            }
         }
         return $data;
     }
@@ -148,12 +176,31 @@
         return $data;
     }
 
-    public function search( $queryString, $defaultField, $fieldList = array() )
-    {
+    public function search( $queryWord, $defaultField, $searchFieldList = 
array(), $returnFieldList = array(), $highlightFieldList = array() )
+    {
+        if ( count( $searchFieldList ) > 0 )
+        {
+            $queryString = '';
+            foreach ( $searchFieldList as $searchField )
+            {
+                $queryString .= "$searchField:$queryWord ";
+            }
+        }
+        else
+        {
+            $queryString = $queryWord;
+        }
         $queryFlags = array( 'q' => $queryString, 'wt' => 'json', 'df' => 
$defaultField );
-        if ( count( $fieldList ) )
-        {
-            $queryFlags['fl'] = join( ' ', $fieldList );
+        if ( count( $returnFieldList ) )
+        {
+            $returnFieldList[] = 'score';
+            $queryFlags['fl'] = join( ' ', $returnFieldList );
+        }
+        if ( count( $highlightFieldList ) )
+        {
+            $queryFlags['hl'] = 'true';
+            $queryFlags['hl.snippets'] = 10;
+            $queryFlags['hl.fl'] = join( ' ', $highlightFieldList );
         }
 
         $result = $this->sendRawGetCommand( 'select', $queryFlags );
@@ -184,37 +231,43 @@
         $map = array(
             ezcSearchDocumentDefinition::STRING => '_s',
             ezcSearchDocumentDefinition::TEXT => '_t',
-            ezcSearchDocumentDefinition::HTML => '_h',
+            ezcSearchDocumentDefinition::HTML => '_t',
             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
+    private function mapFieldValue( $field, $values )
+    {
+        if ( !is_array( $values ) )
+        {
+            $values = array( $values );
+        }
+        foreach ( $values as &$value )
+        {
+            switch( $field->type )
+            {
+                case ezcSearchDocumentDefinition::DATE:
+                    if ( is_numeric( $value ) )
                     {
-                        $d = new DateTime( $value );
+                        $d = new DateTime( "@$value" );
+                        $value = $d->format( 'Y-m-d\TH:i:s\Z' );
                     }
-                    catch ( Exception $e )
+                    else
                     {
-                        throw new ezcSearchInvalidValueException( $type, 
$value );
+                        try
+                        {
+                            $d = new DateTime( $value );
+                        }
+                        catch ( Exception $e )
+                        {
+                            throw new ezcSearchInvalidValueException( $type, 
$value );
+                        }
+                        $value = $d->format( 'Y-m-d\TH:i:s\Z' );
                     }
-                    return $d->format( 'Y-m-d\TH:i:s\Z' );
-                }
-            default:
-                return $value;
-        }
+            }
+        }
+        return $values;
     }
 
     public function index( ezcSearchDocumentDefinition $definition, $document )
@@ -229,17 +282,21 @@
         $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();
+            $value = $this->mapFieldValue( $field, $document[$field->field] );
+            foreach ( $value as $fieldValue )
+            {
+                $xml->startElement( 'field' );
+                $xml->writeAttribute( 'name', $this->mapFieldType( 
$field->field, $field->type ) );
+                $xml->text( $fieldValue );
+                $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/>' );
+        $r = $this->sendRawPostCommand( 'update', array( 'wt' => 'json' ), 
'<commit/>' );
     }
 
     public function createDeleteQuery()

Modified: trunk/Search/src/managers/xml_manager.php
==============================================================================
--- trunk/Search/src/managers/xml_manager.php [iso-8859-1] (original)
+++ trunk/Search/src/managers/xml_manager.php [iso-8859-1] Tue Feb 26 10:22:06 
2008
@@ -88,7 +88,7 @@
                 throw new ezcSearchDefinitionInvalidException( 'XML', 
$documentType, $path, "Unknown type: {$type}" );
             }
             $type = $this->typeMap[$type];
-            $fields[(string) $field] = new ezcSearchDefinitionDocumentField( 
(string) $field, $type, (float) $field['boost'] );
+            $fields[(string) $field] = new ezcSearchDefinitionDocumentField( 
(string) $field, $type, (float) $field['boost'], (bool) $field['inResult'] );
         }
         $def->fields = $fields;
 

Modified: trunk/Search/src/structs/document_field_definition.php
==============================================================================
--- trunk/Search/src/structs/document_field_definition.php [iso-8859-1] 
(original)
+++ trunk/Search/src/structs/document_field_definition.php [iso-8859-1] Tue Feb 
26 10:22:06 2008
@@ -4,12 +4,14 @@
     public $type;
     public $field;
     public $boost;
+    public $inResult;
 
-    public function __construct( $field, $type, $boost )
+    public function __construct( $field, $type, $boost, $inResult = true )
     {
         $this->field = $field;
         $this->type = $type;
         $this->boost = $boost;
+        $this->inResult = $inResult;
     }
 
 }

Modified: trunk/Search/tests/session_test.php
==============================================================================
--- trunk/Search/tests/session_test.php [iso-8859-1] (original)
+++ trunk/Search/tests/session_test.php [iso-8859-1] Tue Feb 26 10:22:06 2008
@@ -74,14 +74,14 @@
 
     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() );
+        $d = file_get_contents( dirname( __FILE__ ) . 
'/../../../docs/guidelines/implementation.txt' );
+        $a = new Article( null, 'Test Article', 'This is Rethans an article to 
test', $d, time(), array( 'Derick Rethans', 'Legolas Rethans' ) );
 
         $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' );
+        $r = $this->backend->search( 'Rethans', 'author_t', array( 
'summary_t', 'title_t', 'body_t' ), array( 'author_t', 'title_t', 'score', 
'summary_t', 'published_dt' ), array( 'author_t', 'title_t', 'score', 
'summary_t', 'published_dt' ) );
         self::assertEquals( 1, $r->resultCount );
     }
 }

Modified: trunk/Search/tests/testfiles/article.php
==============================================================================
--- trunk/Search/tests/testfiles/article.php [iso-8859-1] (original)
+++ trunk/Search/tests/testfiles/article.php [iso-8859-1] Tue Feb 26 10:22:06 
2008
@@ -6,15 +6,17 @@
     private $summary;
     private $body;
     private $published;
+    private $author;
 
 
-    function __construct( $id, $title, $summary, $body, $published )
+    function __construct( $id, $title, $summary, $body, $published, $author = 
null )
     {
         $this->id = $id;
         $this->title = $title;
         $this->summary = $summary;
         $this->body = $body;
         $this->published = $published;
+        $this->author = $author;
     }
 
     function getState()
@@ -25,6 +27,7 @@
             'summary' => $this->summary,
             'body' => $this->body,
             'published' => $this->published,
+            'author' => $this->author,
         );
     }
 

Modified: trunk/Search/tests/testfiles/article.xml
==============================================================================
--- trunk/Search/tests/testfiles/article.xml [iso-8859-1] (original)
+++ trunk/Search/tests/testfiles/article.xml [iso-8859-1] Tue Feb 26 10:22:06 
2008
@@ -3,6 +3,7 @@
     <field type="id">id</field>
     <field type="text" boost="2">title</field>
     <field type="text">summary</field>
-    <field type="html">body</field>
+    <field inResult="false" type="html">body</field>
     <field type="date">published</field>
+    <field type="text" multi="true">author</field>
 </document>


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to