Author: rande
Date: 2010-01-12 17:05:04 +0100 (Tue, 12 Jan 2010)
New Revision: 26537

Modified:
   
plugins/sfSolrPlugin/branches/sf1.2/lib/behavior/sfLuceneDoctrineListener.class.php
   
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexer.class.php
   
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexerHandler.class.php
   plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexer.class.php
   
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexerHandler.class.php
   
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneModelIndexer.class.php
   
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLucenePropelIndexer.class.php
Log:
[sfSolrPlugin] auto commit on sfLuceneDoctrineListener, add multi documents 
management on batch mode

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/behavior/sfLuceneDoctrineListener.class.php
===================================================================
--- 
plugins/sfSolrPlugin/branches/sf1.2/lib/behavior/sfLuceneDoctrineListener.class.php
 2010-01-12 14:48:07 UTC (rev 26536)
+++ 
plugins/sfSolrPlugin/branches/sf1.2/lib/behavior/sfLuceneDoctrineListener.class.php
 2010-01-12 16:05:04 UTC (rev 26537)
@@ -38,7 +38,6 @@
         
sfContext::getInstance()->getLogger()->crit('{sfLuceneDoctrineListener::postSave}
 Error while saving document to solr : '.$e->getMessage());
       }
     }
-
   }
 
   /**
@@ -89,6 +88,7 @@
     {
 
       $instance->getIndexerFactory()->getModel($node)->delete();
+      $instance->getSearchService()->commit();
     }
   }
 
@@ -111,6 +111,7 @@
     foreach ($this->getSearchInstances($node) as $instance)
     {
       $instance->getIndexerFactory()->getModel($node)->insert();
+      $instance->getSearchService()->commit();
     }
   }
 

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexer.class.php
===================================================================
--- 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexer.class.php
   2010-01-12 14:48:07 UTC (rev 26536)
+++ 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexer.class.php
   2010-01-12 16:05:04 UTC (rev 26537)
@@ -19,21 +19,18 @@
  */
 class sfLuceneDoctrineIndexer extends sfLuceneModelIndexer
 {
-  /**
-  * Inserts the provided model into the index based off parameters in 
search.yml.
-  * @param BaseObject $this->getModel() The model to insert
-  */
-  public function insert()
+
+  public function getDocument()
   {
     if (!$this->shouldIndex())
     {
-      $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('model "%s" cancelled indexation - primary key = %s', 
$this->getModelName(), current($this->getModel()->identifier()))));
-      
-      return $this;
+
+      return false;
     }
 
+
     $old_culture = null;
-    
+
     // automatic symfony i18n detection
     if ($this->getModel()->getTable()->hasRelation('Translation'))
     {
@@ -46,18 +43,39 @@
     $doc = $this->configureDocumentFields($doc);
     //$doc = $this->configureDocumentCategories($doc);
     $doc = $this->configureDocumentMetas($doc);
-    
+
     // add document
+    $doc->setField('sfl_guid', $this->getModelGuid());
 
-    $this->addDocument($doc, $this->getModelGuid());
-    
-    $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('Inserted model "%s" from index with primary key = %s', 
$this->getModelName(), current($this->getModel()->identifier()))));
-
     // restore culture in symfony i18n detection
     if ($old_culture)
     {
       sfDoctrineRecord::setDefaultCulture($old_culture);
     }
+
+    return $doc;
+  }
+
+    /**
+  * Inserts the provided model into the index based off parameters in 
search.yml.
+   *
+  * @param BaseObject $this->getModel() The model to insert
+  */
+  public function insert()
+  {
+    $doc = $this->getDocument();
+
+    if (!$doc)
+    {
+      $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('model "%s" cancelled indexation - primary key = %s', 
$this->getModelName(), current($this->getModel()->identifier()))));
+
+      return $this;
+    }
+
+    $this->addDocument($doc, $this->getModelGuid());
+
+    $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('Inserted model "%s" from index with primary key = %s', 
$this->getModelName(), current($this->getModel()->identifier()))));
+
     return $this;
   }
 

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexerHandler.class.php
===================================================================
--- 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexerHandler.class.php
    2010-01-12 14:48:07 UTC (rev 26536)
+++ 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneDoctrineIndexerHandler.class.php
    2010-01-12 16:05:04 UTC (rev 26537)
@@ -79,11 +79,69 @@
 
     $collection = $query->limit($limit)->offset($offset)->execute();
 
+    $documents = array();
+    $pks = array();
     foreach($collection as $record)
     {
-      $this->getFactory()->getModel($record)->save();
+      $doc = $this->getFactory()->getModel($record)->getDocument();
+
+      if(!$doc)
+      {
+        $this->getSearch()->getEventDispatcher()->notify(
+          new sfEvent($this, 'application.log', array(
+            sprintf('invalid document %s [id:%s]: ', get_class($record), 
$record->identifier()),
+            'priority' => sfLogger::ALERT
+          ))
+        );
+        continue;
+      }
+      $documents[] = $doc;
+      
+      $field = $doc->getField('id');
+      
+      $pks[] = $field['value'];
+      
       unset($record);
     }
+
+
+    $search_engine =  $this->getSearch()->getSearchService();
+
+    try
+    {
+      $search_engine->deleteByMultipleIds(array_keys($documents));
+      $search_engine->commit();
+
+      $search_engine->addDocuments($documents);
+      $search_engine->commit();
+
+      $this->getSearch()->getEventDispatcher()->notify(
+         new sfEvent(
+           $this,
+           'indexer.log',
+           array('indexing ok - primary keys [%s]', implode(', ', $pks))
+         )
+      );
+    }
+    catch(Exception $e)
+    {
+       $this->getSearch()->getEventDispatcher()->notify(
+         new sfEvent(
+           $this,
+           'indexer.log',
+           array('indexing Failed indexing object - primary keys [%s]', 
implode(', ', $pks))
+         )
+      );
+       
+      $this->getSearch()->getEventDispatcher()->notify(
+        new sfEvent($this, 'application.log', array(
+          'indexing document fail : '.$e->getMessage(),
+          'priority' => sfLogger::ALERT
+        ))
+      );
+    }
+
+    
     unset($collection);
   }
 }
\ No newline at end of file

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexer.class.php
===================================================================
--- plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexer.class.php   
2010-01-12 14:48:07 UTC (rev 26536)
+++ plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexer.class.php   
2010-01-12 16:05:04 UTC (rev 26537)
@@ -66,12 +66,12 @@
     {
       $this->getConfiguration()->getEventDispatcher()->notify(
         new sfEvent($this, 'application.log', array(
-          '{sfSolrPlugin} saving document to index fail : '.$e->getMessage(),
+          'indexing document fail : '.$e->getMessage(),
           'priority' => sfLogger::ALERT
         ))
       );
     }
-    
+
     return $this;
   }
 
@@ -119,7 +119,7 @@
   */
   protected function addDocument(Apache_Solr_Document $document, $guid)
   {
-    $document->addField('sfl_guid', $guid);
+    $document->setField('sfl_guid', $guid);
 
     $timer = sfTimerManager::getTimer('Solr Search Lucene');
     $this->getSearch()->getLucene()->addDocument($document);

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexerHandler.class.php
===================================================================
--- 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexerHandler.class.php
    2010-01-12 14:48:07 UTC (rev 26536)
+++ 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneIndexerHandler.class.php
    2010-01-12 16:05:04 UTC (rev 26537)
@@ -27,6 +27,10 @@
     $this->factory = new sfLuceneIndexerFactory($this->getSearch());
   }
 
+  /**
+   *
+   * @return sfLucene
+   */
   protected function getSearch()
   {
     return $this->search;

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneModelIndexer.class.php
===================================================================
--- 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneModelIndexer.class.php  
    2010-01-12 14:48:07 UTC (rev 26536)
+++ 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLuceneModelIndexer.class.php  
    2010-01-12 16:05:04 UTC (rev 26537)
@@ -52,6 +52,13 @@
   }
 
   /**
+   * return the full document
+   *
+   * @return Apache_Solr_Document
+   */
+  abstract protected function getDocument();
+
+  /**
    * Calculates the GUID for the model
    */
   abstract protected function getModelGuid();
@@ -108,8 +115,8 @@
    */
   protected function configureDocumentMetas(Apache_Solr_Document $doc)
   {
-    $doc->addField('sfl_model', $this->getModelName());
-    $doc->addField('sfl_type', 'model');
+    $doc->setField('sfl_model', $this->getModelName());
+    $doc->setField('sfl_type', 'model');
 
     return $doc;
   }

Modified: 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLucenePropelIndexer.class.php
===================================================================
--- 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLucenePropelIndexer.class.php 
    2010-01-12 14:48:07 UTC (rev 26536)
+++ 
plugins/sfSolrPlugin/branches/sf1.2/lib/indexer/sfLucenePropelIndexer.class.php 
    2010-01-12 16:05:04 UTC (rev 26537)
@@ -32,19 +32,13 @@
     parent::__construct($search, $instance);
   }
 
-  
-  /**
-  * Inserts the provided model into the index based off parameters in 
search.yml.
-  */
-  public function insert()
+  public function getDocument()
   {
-    // should we continue with indexing?
+     // should we continue with indexing?
     if (!$this->shouldIndex())
     {
-      // indexer said to skip indexing
-      $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('Ignoring model "%s" from index with primary key = %s', 
$this->getModelName(), $this->getModel()->getPrimaryKey())));
 
-      return $this;
+      return false;
     }
 
     $old_culture = null;
@@ -63,7 +57,7 @@
     $doc = $this->configureDocumentMetas($doc);
 
     // add document to index
-    $this->addDocument($doc, $this->getModelGuid());
+    $doc->addField('sfl_guid', $this->getModelGuid());
 
     // restore culture in symfony i18n detection
     if ($old_culture)
@@ -71,6 +65,27 @@
       $this->getModel()->setCulture($old_culture);
     }
 
+    return $doc;
+  }
+  
+  /**
+  * Inserts the provided model into the index based off parameters in 
search.yml.
+  */
+  public function insert()
+  {
+    $doc = $this->getDocument();
+    
+    // should we continue with indexing?
+    if (!$doc)
+    {
+      // indexer said to skip indexing
+      $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('Ignoring model "%s" from index with primary key = %s', 
$this->getModelName(), $this->getModel()->getPrimaryKey())));
+
+      return $this;
+    }
+
+    $this->addDocument($doc, $this->getModelGuid());
+
     // notify about new record
     $this->getSearch()->getEventDispatcher()->notify(new sfEvent($this, 
'indexer.log', array('Inserted model "%s" from index with primary key = %s', 
$this->getModelName(), $this->getModel()->getPrimaryKey())));
 

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.


Reply via email to