Beispiel #1
0
        private void StartLuceneIndexCreateProcess()
        {
            string luceneIndexStoragePath = @ConfigurationManager.AppSettings["LuceneIndexStoragePath"];
            bool   folderExists           = System.IO.Directory.Exists(luceneIndexStoragePath);

            if (!folderExists)
            {
                System.IO.Directory.CreateDirectory(luceneIndexStoragePath);
            }

            analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
            Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(luceneIndexStoragePath));
            writer = new Lucene.Net.Index.IndexWriter(directory, analyzer, true, Lucene.Net.Index.IndexWriter.MaxFieldLength.LIMITED);
            try
            {
                // We will populate below list to create Lucene index.
                List <string> actorsList = new List <string>();
                actorsList.Add("Johnny Depp");
                actorsList.Add("Robert Downey Jr.");
                actorsList.Add("Johnny Depp");
                actorsList.Add("Tom Cruise");
                actorsList.Add("Brad Pitt");
                actorsList.Add("Tom Hanks");
                actorsList.Add("Denzel Washington");
                actorsList.Add("Russell Crowe");
                actorsList.Add("Kate Winslet");
                actorsList.Add("Christian Bale");
                actorsList.Add("Hugh Jackman");
                actorsList.Add("Will Smith");
                actorsList.Add("Sean Connery");

                foreach (var item in actorsList)
                {
                    Console.WriteLine(item);
                    writer.AddDocument(CreateDocument(item.ToString()));
                }
            }
            catch
            {
                Lucene.Net.Index.IndexWriter.Unlock(directory);
                throw;
            }
            finally
            {
                writer.Optimize();
                analyzer.Close();
                writer.Dispose();
                analyzer.Dispose();
            }
        }
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         // free managed resources
         if (analyzer != null)
         {
             analyzer.Dispose();
         }
         if (searcher != null)
         {
             searcher.Dispose();
         }
     }
     // free native resources if there are any.
 }
        public List <LuceneData> Search(string searchText, string searchField)
        {
            List <LuceneData> output = new List <LuceneData>();

            string searchQuery = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(searchText.Trim()))
                {
                    throw new Exception("You forgot to enter something to search for...");
                }

                searchQuery = searchText;
            }
            catch
            {
                throw;
            }

            try
            {
                // Open the IndexReader with readOnly=true.
                // This makes a big difference when multiple threads are sharing the same reader,
                // as it removes certain sources of thread contention.
                searcher = new Lucene.Net.Search.IndexSearcher(_directory, true);
                analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                output   = (GetSearchResultByField(searchQuery, searchField, searcher, analyzer));
            }
            catch
            {
                throw;
            }
            finally
            {
                analyzer.Close();
                analyzer.Dispose();
                searcher.Dispose();
            }

            return(output);
        }
Beispiel #4
0
 public void SaveAndDispose()
 {
     //if (doc != null)
     //{
     //    doc = null;
     //}
     if (analyzer != null)
     {
         analyzer.Dispose();
         analyzer = null;
     }
     if (writer != null)
     {
         writer.Optimize();
         writer.Commit();
         writer.Dispose();
         writer = null;
     }
     if (direc != null)
     {
         direc.Dispose();
         direc = null;
     }
 }
Beispiel #5
0
        public void LuceneDispose()
        {
            if (searcher != null)
            {
                searcher.Dispose();
                searcher = null;
            }
            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }
            if (analyzer != null)
            {
                analyzer.Dispose();
                analyzer = null;
            }

            if (direc != null)
            {
                direc.Dispose();
                direc = null;
            }
        }
Beispiel #6
0
 public virtual void Dispose()
 {
     _analyzer.Close();
     _analyzer.Dispose();
 }
Beispiel #7
0
        public WorkerThreadResult RunMT(bW1Arg arg)
        {
            WorkerThreadResult result = new WorkerThreadResult();

            string indexPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "LuceneData");

            Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(indexPath);
            Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            Worker workerObject = new Worker();

            workerObject.Writer      = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
            workerObject.ConStackLog = ErrLog;
            workerObject.SetRAMBufferSizeMB();

            List <FileMeta> LstMsgFile = MyIO.GetFileMsgFromTo(MyConfig.GetMailFolder(), arg.DFrom, arg.DTo);

            if (arg.IsReIndex)
            {
                workerObject.DeleteAllDocuments();
            }
            else
            {
                //Xoa bo nhung item trung lap giua list moi tao va du lieu da index
                using (Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(directory))
                {
                    for (int i = LstMsgFile.Count - 1; i >= 0; i--)
                    {
                        if (MyLucene.CheckDocExist(searcher, LstMsgFile[i]))
                        {
                            LstMsgFile.RemoveAt(i);
                            result.IncreaseNumDupFiles();
                        }
                    }
                }
            }

            //Chay song song
            ParallelLoopResult pResult = Parallel.ForEach(LstMsgFile, (currentFileMeta) => workerObject.DoWork(currentFileMeta));

            StringBuilder sb = new StringBuilder();

            if (pResult.IsCompleted)
            {
                workerObject.DisposeWriter();
                analyzer.Dispose();
                directory.Dispose();

                result.FileDone = LstMsgFile;

                int i = 0;
                foreach (var a in ErrLog)
                {
                    string item;
                    ErrLog.TryPop(out item);
                    i++;
                    sb.Append(i.ToString());
                    sb.Append(". ");
                    sb.Append(item);
                    sb.Append(Environment.NewLine);
                }
                result.ErrLog = sb.ToString();
            }
            return(result);
        }
Beispiel #8
0
        private void BtnFind_Click(object sender, EventArgs e)
        {
            int iMaxResult = 1000;

            if (CheckTextBoxNotNull(TxtMaxResult))
            {
                Int32.TryParse(TxtMaxResult.Text, out iMaxResult);
            }
            if (iMaxResult < 1)
            {
                iMaxResult = 1000;
            }
            TxtMaxResult.Text = iMaxResult.ToString();

            BooleanQuery allQuery = new BooleanQuery();

            TermRangeQuery queryFromTo = null;
            string         sFrom       = DateTools.DateToString(DPickerFrom.Value, DateTools.Resolution.MINUTE);
            string         sTo         = DateTools.DateToString(DPickerTo.Value, DateTools.Resolution.MINUTE);

            if (RdBtnSentOn.Checked) //Sent
            {
                if (DPickerFrom.Checked && DPickerTo.Checked)
                {
                    queryFromTo = new TermRangeQuery("SentOn", sFrom, sTo, true, true);
                }
                else if (DPickerFrom.Checked && !DPickerTo.Checked)
                {
                    queryFromTo = new TermRangeQuery("SentOn", sFrom, null, true, true);
                }
                else if (!DPickerFrom.Checked && DPickerTo.Checked)
                {
                    queryFromTo = new TermRangeQuery("SentOn", null, sTo, true, true);
                }
            }
            else
            {
                if (DPickerFrom.Checked && DPickerTo.Checked)
                {
                    queryFromTo = new TermRangeQuery("ReceivedOn", sFrom, sTo, true, true);
                }
                else if (DPickerFrom.Checked && !DPickerTo.Checked)
                {
                    queryFromTo = new TermRangeQuery("ReceivedOn", sFrom, null, true, true);
                }
                else if (!DPickerFrom.Checked && DPickerTo.Checked)
                {
                    queryFromTo = new TermRangeQuery("ReceivedOn", null, sTo, true, true);
                }
            }

            if (queryFromTo != null)
            {
                allQuery.Add(queryFromTo, Occur.MUST);
            }

            Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = MyLucene.GetAnalyzer();
            if (CheckTextBoxNotNull(TxtBody))
            {
                Query querySubject = MyLucene.GetTextQuery("Subject", TxtBody.Text, analyzer);
                if (ChkSubject.Checked)
                {
                    allQuery.Add(querySubject, Occur.MUST);
                }
                else
                {
                    BooleanQuery query2    = new BooleanQuery();
                    Query        queryBody = MyLucene.GetTextQuery("Body", TxtBody.Text, analyzer);
                    query2.Add(querySubject, Occur.SHOULD);
                    query2.Add(queryBody, Occur.SHOULD);
                    allQuery.Add(query2, Occur.MUST);
                }
            }

            if (CheckTextBoxNotNull(TxtReceiver))
            {
                BooleanQuery qReceiver = new BooleanQuery();
                Query        qTo       = MyLucene.GetTextQuery("To", TxtReceiver.Text, analyzer);
                Query        qCC       = MyLucene.GetTextQuery("CC", TxtReceiver.Text, analyzer);
                qReceiver.Add(qTo, Occur.SHOULD);
                qReceiver.Add(qCC, Occur.SHOULD);
                allQuery.Add(qReceiver, Occur.MUST);
            }

            if (CheckTextBoxNotNull(TxtSender))
            {
                BooleanQuery query3       = new BooleanQuery();
                Query        qSender      = MyLucene.GetTextQuery("SenderName", TxtSender.Text, analyzer);
                Query        qSenderEmail = MyLucene.GetTextQuery("SenderEmailAddress", TxtSender.Text, analyzer);
                query3.Add(qSender, Occur.SHOULD);
                query3.Add(qSenderEmail, Occur.SHOULD);
                allQuery.Add(query3, Occur.MUST);
            }

            if (string.IsNullOrEmpty(allQuery.ToString()))
            {
                MessageBox.Show("Vui lòng chọn điều kiện", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information); return;
            }

            IndexSearcher searcher = MyLucene.GetSearcher();

            lst = MyLucene.Search(searcher, allQuery, iMaxResult);
            searcher.Dispose();
            analyzer.Dispose();

            dt.Clear();
            for (int i = 0; i < lst.Count; i++)
            {
                MailData mailData = lst[i];
                object[] row      = { i, mailData.DSentOn, mailData.SenderEmailAddress, mailData.Subject, mailData.DReceivedOn };
                dt.Rows.Add(row);
            }
            bindingSource1.DataSource = dt;
        }
Beispiel #9
0
        public WorkerThreadResult RunMT(List <FileMeta> LstMsgFile, bool IsReIndex)
        {
            WorkerThreadResult result = new WorkerThreadResult();

            string indexPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "LuceneData");

            Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(indexPath);
            Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);

            Worker2 workerObject = new Worker2();

            workerObject.Writer      = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
            workerObject.ConStackLog = ErrLog;
            workerObject.SetRAMBufferSizeMB();

            if (IsReIndex)
            {
                workerObject.DeleteAllDocuments();
            }
            else
            {
                //Xoa bo nhung item trung lap giua list moi tao va du lieu da index
                using (Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(directory))
                {
                    for (int idx2 = LstMsgFile.Count - 1; idx2 >= 0; idx2--)
                    {
                        if (MyLucene.CheckDocExist(searcher, LstMsgFile[idx2]))
                        {
                            LstMsgFile.RemoveAt(idx2);
                            result.IncreaseNumDupFiles();
                        }
                    }
                }
            }
            //Chia list thanh cac list con
            int chunkSize = 200;
            int maxThread = 25;

            int totalItem = LstMsgFile.Count;
            int numThread = totalItem / chunkSize;

            if (numThread > maxThread)
            {
                numThread = maxThread;
                chunkSize = totalItem / maxThread;
            }
            if (totalItem % chunkSize != 0)
            {
                numThread++;
            }

            List <List <FileMeta> > lst1 = new List <List <FileMeta> >();

            for (int idx = 0; idx < totalItem; idx++)
            {
                int iMin = idx * chunkSize;
                int iMax = iMin + chunkSize;
                if (iMax > totalItem)
                {
                    iMax = totalItem;
                }
                List <FileMeta> lst2 = new List <FileMeta>();
                for (int j = iMin; j < iMax; j++)
                {
                    lst2.Add(LstMsgFile[j]);
                }
                if (lst2.Count != 0)
                {
                    lst1.Add(lst2);
                }
            }

            //Chay song song
            List <Thread> lstTask = new List <Thread>();

            foreach (var item in lst1)
            {
                Thread thread = new Thread(workerObject.DoWork);
                thread.Start(item);
                lstTask.Add(thread);
            }

            //Cho cho den khi cac thread chay xong moi chay tiep
            foreach (var th in lstTask)
            {
                th.Join();
            }

            //Luu du lieu
            workerObject.DisposeWriter();
            analyzer.Dispose();
            directory.Dispose();

            result.FileDone = LstMsgFile;

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < ErrLog.Count; i++)
            {
                string item = ErrLog.Pop();
                sb.Append((i + 1).ToString());
                sb.Append(". ");
                sb.Append(item);
                sb.Append(Environment.NewLine);
            }
            result.ErrLog = sb.ToString();
            return(result);
        }