Scarab commit: svn commit: r10147 - trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java

[email protected]
Newsgroups gmane.comp.java.scarab.cvs
Message-ID <[email protected]>
Author: dabbous
Date: 2006-06-05 16:07:58-0700
New Revision: 10147

Modified:
   trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java

Log:
fixed SCB1724

Now the lucene search indexes are checked on startup of Scarab. If the
search index directory is empty or contains only the segments file, 
we assume, that the search index has not been created yet, or it has 
previously been deleted on purpose.

In that case the search index is rebuilt in a background thread.

The manual search index rebuild still works as before.


Modified: trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java
Url: http://scarab.tigris.org/source/browse/scarab/trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java?view=diff&rev=10147&p1=trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java&p2=trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java&r1=10146&r2=10147
==============================================================================
--- trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java	(original)
+++ trunk/src/java/org/tigris/scarab/util/word/LuceneSearchIndex.java	2006-06-05 16:07:58-0700
@@ -71,11 +71,16 @@
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.torque.util.Criteria;
+import org.apache.turbine.RunData;
+import org.apache.turbine.TemplateContext;
+import org.tigris.scarab.actions.admin.UpdateSearchIndex.UpdateThread;
 import org.tigris.scarab.om.Attachment;
 import org.tigris.scarab.om.AttachmentPeer;
 import org.tigris.scarab.om.AttributeValue;
 import org.tigris.scarab.om.AttributeValuePeer;
 import org.tigris.scarab.om.IssuePeer;
+import org.tigris.scarab.tools.ScarabLocalizationTool;
+import org.tigris.scarab.tools.ScarabRequestTool;
 import org.tigris.scarab.tools.localization.L10NKeySet;
 import org.tigris.scarab.tools.localization.L10NMessage;
 import org.tigris.scarab.tools.localization.Localizable;
@@ -111,6 +116,8 @@
 
     /** the words and boolean operators */
     private List attachmentQueryText;
+    
+    static private ThreadGroup tg = null;
 
     /**
      * Ctor.  Sets up an index directory if one does not yet exist in the
@@ -671,7 +678,8 @@
         boolean createIndex = false;
         if (indexDir.exists()) 
         {
-            if (indexDir.listFiles().length == 0) 
+            int length = indexDir.listFiles().length;
+            if ( length < 3) 
             {
                 createIndex = true;
             }       
@@ -687,19 +695,7 @@
             Log.get().info("Creating index at '" + path + '\'');
             synchronized (getClass())
             {
-                IndexWriter indexer = null;
-                try
-                {
-                    indexer = 
-                        new IndexWriter(path, new PorterStemAnalyzer(), true);
-                }
-                finally
-                {
-                    if (indexer != null) 
-                    {
-                        indexer.close();                           
-                    }
-                }
+                doCreateIndex();
             }
         }        
 
@@ -731,4 +727,59 @@
         attachmentQueryText = new ArrayList(2);
     }
     
+    public void doCreateIndex()
+            throws Exception
+    {
+        synchronized (this)
+        {
+
+            try
+            {
+                if(tg == null)
+                {
+                    tg = new ThreadGroup("UpdateIndex");
+                }
+                Thread updateThread = new Thread(tg, new UpdateThread());
+                updateThread.start();
+            }
+            catch (Exception e)
+            {
+                Log.get().warn("Could not start SearchIndex initialization:",e); 
+            }
+
+        }
+
+    }
+
+    public class UpdateThread implements Runnable
+    {
+        public void run()
+        {
+            try
+            {
+                IndexWriter indexWriter = null;
+                try
+                {
+                    indexWriter = new IndexWriter(path, new PorterStemAnalyzer(), true);
+                }
+                finally
+                {
+                    if (indexWriter != null) 
+                    {
+                        indexWriter.close();                           
+                    }
+                }
+
+                Log.get().info("Update index started!");
+                SearchIndex indexer = SearchFactory.getInstance();
+                indexer.updateIndex();            
+                Log.get().info("Update index completed!");
+
+            }
+            catch (Exception e)
+            {
+                Log.get().error("Update index failed:", e);
+            }
+        }
+    }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.