From 3ccfabc683650cfbf45f7e7e6db77e6bebcfaf0f Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Tue, 21 May 2019 11:34:53 +0200
Subject: [PATCH] FEAT #10525 TIME 0:30 Optimize + delete index

---
 .../attachment/models/AttachmentModelAbstract.php  | 14 ++++++++++----
 src/app/convert/controllers/FullTextController.php | 14 +++++++++++---
 src/app/convert/scripts/FullTextScript.php         |  1 -
 3 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/src/app/attachment/models/AttachmentModelAbstract.php b/src/app/attachment/models/AttachmentModelAbstract.php
index 3fb327f1946..5b00484a2c0 100755
--- a/src/app/attachment/models/AttachmentModelAbstract.php
+++ b/src/app/attachment/models/AttachmentModelAbstract.php
@@ -25,20 +25,26 @@ abstract class AttachmentModelAbstract
         ValidatorModel::notEmpty($aArgs, ['select']);
         ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy']);
         ValidatorModel::intType($aArgs, ['limit']);
+        ValidatorModel::boolType($aArgs, ['isVersion']);
+
+        if (!empty($aArgs['isVersion'])) {
+            $table = 'res_version_attachments';
+        } else {
+            $table = 'res_attachments';
+        }
 
-        $aResources = DatabaseModel::select([
+        $attachments = DatabaseModel::select([
             'select'    => $aArgs['select'],
-            'table'     => empty($aArgs['table']) ? ['res_attachments'] : $aArgs['table'],
+            'table'     => [$table],
             'where'     => empty($aArgs['where']) ? [] : $aArgs['where'],
             'data'      => empty($aArgs['data']) ? [] : $aArgs['data'],
             'order_by'  => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'],
             'limit'     => empty($aArgs['limit']) ? 0 : $aArgs['limit']
         ]);
 
-        return $aResources;
+        return $attachments;
     }
 
-
     public static function getOnView(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['select']);
diff --git a/src/app/convert/controllers/FullTextController.php b/src/app/convert/controllers/FullTextController.php
index c18695a5c91..5d6189d2afc 100644
--- a/src/app/convert/controllers/FullTextController.php
+++ b/src/app/convert/controllers/FullTextController.php
@@ -82,6 +82,12 @@ class FullTextController
             \Zend_Search_Lucene_Analysis_Analyzer::setDefault(new \Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
             $index->setMaxBufferedDocs(1000);
 
+            $term = new \Zend_Search_Lucene_Index_Term((integer)$args['resId'], 'Id');
+            $terms = $index->termDocs($term);
+            foreach ($terms as $value) {
+                $index->delete($value);
+            }
+
             $doc = new \Zend_Search_Lucene_Document();
 
             $doc->addField(\Zend_Search_Lucene_Field::UnIndexed('Id', (integer)$args['resId']));
@@ -89,7 +95,9 @@ class FullTextController
 
             $index->addDocument($doc);
             $index->commit();
-            $index->optimize();
+            if ((integer)$args['resId'] % 100 === 0) {
+                $index->optimize(); // Optimize every 100 documents
+            }
         } catch (\Exception $e) {
             return ['errors' => 'Full Text index failed : ' . $e];
         }
@@ -148,9 +156,9 @@ class FullTextController
         } else {
             $resIds = AttachmentModel::get([
                 'select'    => ['res_id'],
-                'table'     => [$args['collId'] == 'attachments_coll' ? 'res_attachments' : 'res_version_attachments'],
+                'isVersion' => $args['collId'] == 'attachments_version_coll',
                 'where'     => ['attachment_type <> ?', 'status NOT IN (?)', 'fulltext_result = ?'],
-                'data'      => ['print_folder', ['DEL','OBS','TMP'],'ERROR'],
+                'data'      => ['print_folder', ['DEL','OBS','TMP'], 'ERROR'],
             ]);
         }
         return $resIds;
diff --git a/src/app/convert/scripts/FullTextScript.php b/src/app/convert/scripts/FullTextScript.php
index df1a626d1b1..0069d6b78f3 100644
--- a/src/app/convert/scripts/FullTextScript.php
+++ b/src/app/convert/scripts/FullTextScript.php
@@ -42,7 +42,6 @@ class FullTextScript
     {
         $customId = '';
         $resId    = '';
-        $userId   = '';
         $collId   = '';
         $mode     = '';
 
-- 
GitLab