Lucene.NET开发——索引的合并

索引合并

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Windows.Forms;

 

using Lucene.Net.Index;

using Lucene.Net.Store;

using Lucene.Net.Analysis.Standard;

 

namespace NewBeeServer

{

   static class MergeIndex

   {

      //开始合并

      public static bool BeginMerge()

      {

        DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + "\\users\\" + UserName.userName + "\\index");

        FileInfo[] filesInfo = dirInfo.GetFiles();

        if (filesInfo.Length == 0)

        {

           CutToMain();

           return true;

        }

        else

        {

           return Merge();

        }

      }

      //合并索引

      private static bool Merge()

      {

        DirectoryInfo from = new DirectoryInfo(Application.StartupPath + "\\users\\" + UserName.userName + "\\subindex");

        DirectoryInfo to = new DirectoryInfo(Application.StartupPath + "\\users\\" + UserName.userName + "\\index");

        IndexWriter indexWriter = new IndexWriter(

           FSDirectory.Open(to),

           new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29),

           false);

        indexWriter.SetMergeFactor(100000);

        indexWriter.SetMaxFieldLength(int.MaxValue);

        indexWriter.SetMaxBufferedDocs(int.MaxValue);

        indexWriter.SetMaxMergeDocs(int.MaxValue);

        try

        {

           FSDirectory[] fs = { FSDirectory.GetDirectory(from, false) };

           indexWriter.AddIndexes(fs);

           indexWriter.Optimize();

           indexWriter.Close();

           DeleteSubindex();

           //AppendLog("已完成合并", true);

           return true;

        }

        catch (Exception ee)

        {

           //AppendLog("合并索引出错", true);

           return false;

           MessageBox.Show(ee.Message);

        }

        finally

        {

           try

           {

              if (indexWriter != null)

                 indexWriter.Close();

           }

           catch (Exception ee)

           {

 

           }

        }

      }

 

      //主索引为空,将子索引作为主索引

      private static void CutToMain()

      {

        DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + "\\users\\" + UserName.userName + "\\subindex");

        foreach (FileInfo fileInfo in dirInfo.GetFiles())

        {

           File.Move(fileInfo.FullName, Application.StartupPath + "\\users\\" + UserName.userName + "\\index\\" + fileInfo.Name);

        }

      }

 

      //删除子索引

      private static void DeleteSubindex()

      {

        //删除subindex底下的文件

        DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + "\\users\\" + UserName.userName + "\\subindex");

        foreach (FileInfo fileInfo in dirInfo.GetFiles())

        {

           try

           {

              File.Delete(fileInfo.FullName);

           }

           catch (Exception ee)

           {

              MessageBox.Show(ee.Message);

           }

        }

      }

   }

}

posted @ 2011-08-29 11:57  Erebus_NET  阅读(301)  评论(0编辑  收藏  举报