예제 #1
0
        /// <summary>
        /// 查找指定字段Term
        /// </summary>
        /// <param name="field"></param>
        /// <param name="text"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public TermModel FindTerm(string field, string text, bool current)
        {
            TermEnum enum2 = open.Reader.Terms();

            if (enum2.SkipTo(new Term(field, text)))
            {
                TermModel modle2 = null;
                while ((!current && enum2.Next() && field.Equals(enum2.Term().Field())) ||
                       current)
                {
                    modle2 = new TermModel(enum2.Term(), enum2.DocFreq());
                    break;
                }
                enum2.Close();
                return(modle2);
            }
            return(null);
        }
예제 #2
0
        public void End(bool shouldClose)
        {
            if (!_is_started)
            {
                return;
            }
            if (!shouldClose)
            {
                return;
            }
            //build 2del file list
            if (!_job_status.Cancelled)
            {
                TermEnum term_enum = _index_reader.Terms();
                Term     path_term = new Term("path");
                int      nb_terms  = 0;
                while (term_enum.SkipTo(path_term))                 //skip to new term equal or *ABOVE* "path:" !!!
                {
                    Term term = term_enum.Term();
                    if (term.Field() != path_term.Field())
                    {
                        break;
                    }
                    if (!File.Exists(term.Text()))
                    {
                        _del_file_list.Add(term.Text());
                    }
                    if (_job_status.Cancelled)
                    {
                        break;
                    }
                    nb_terms++;
                }
                term_enum.Close();
                Logger.Log.Info("update: deletion: {0} analyzed terms, found {1} vanished files.", nb_terms, _del_file_list.Count);
            }
            _index_searcher.Close();
            _index_reader.Close();
            //--- deleting deprecated
            if ((_del_file_list.Count > 0) && (!_job_status.Cancelled))
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();

                int         num_file = 0;
                int         nb_files = _del_file_list.Count;
                IndexWriter writer   = new IndexWriter(_index_path, _default_analyzer, false);

                foreach (string path in _del_file_list)
                {
                    if (((num_file++) % 101) == 1)
                    {
                        int progress = ((((num_file++) + 1)) * 100) / nb_files;
                        _job_status.Progress    = progress;
                        _job_status.Description = String.Format("upd: removing (from index) file {0}/{1} - {2}", num_file, _del_file_list.Count,
                                                                StringFu.TimeSpanToString(new TimeSpan((long)(watch.ElapsedMilliseconds) * 10000)));
                    }
                    if (_job_status.Cancelled)
                    {
                        break;
                    }
                    writer.DeleteDocuments(new Term("path", path));
                }
                writer.Commit();
                writer.Close();
                watch.Stop();
            }
            //adding new files
            if ((_add_file_list.Count > 0) && (!_job_status.Cancelled))
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();

                IndexWriter writer = null;
                try
                {
                    writer = new IndexWriter(_index_path, _default_analyzer, false, new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH));
                    int num_file = 0;
                    int nb_files = _add_file_list.Count;
                    foreach (BasicFileInfo fi in _add_file_list)
                    {
                        if (((num_file++) % 101) == 1)
                        {
                            int progress = ((((num_file++) + 1)) * 100) / nb_files;
                            _job_status.Progress    = progress;
                            _job_status.Description = String.Format("upd: indexing new file {0}/{1} - {2}", num_file, _add_file_list.Count,
                                                                    StringFu.TimeSpanToString(new TimeSpan((long)(watch.ElapsedMilliseconds) * 10000)));
                        }
                        if (_job_status.Cancelled)
                        {
                            break;
                        }

                        writer.AddDocument(_doc_factory.CreateFromPath(fi.FilePath, fi.LastModification));
                        if (num_file % 20 == 0)
                        {
                            writer.Commit();
                        }
                    }
                    writer.Commit();
                }
                catch (System.Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
                watch.Stop();
            }
            //updating modified files
            if ((_upd_file_list.Count > 0) && (!_job_status.Cancelled))
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();

                int         num_file = 0;
                int         nb_files = _upd_file_list.Count;
                IndexWriter writer   = null;
                try
                {
                    writer = new IndexWriter(_index_path, _default_analyzer, false,
                                             new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH));

                    foreach (BasicFileInfo fi in _upd_file_list)
                    {
                        if (((num_file++) % 101) == 1)
                        {
                            int progress = ((((num_file++) + 1)) * 100) / nb_files;
                            _job_status.Progress    = progress;
                            _job_status.Description = String.Format("upd: modified file {0}/{1} - {2}", num_file, _upd_file_list.Count,
                                                                    StringFu.TimeSpanToString(new TimeSpan((long)(watch.ElapsedMilliseconds) * 10000)));
                        }
                        if (_job_status.Cancelled)
                        {
                            break;
                        }
                        writer.UpdateDocument(new Term("path", fi.FilePath),
                                              _doc_factory.CreateFromPath(fi.FilePath, fi.LastModification));
                    }
                    writer.Commit();
                    //LittleBeagle.Properties.Settings.Default.NbIndexedFiles = num_file;
                }
                catch (System.Exception ex)
                {
                    Log.Error(ex);
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
                watch.Stop();
            }
        }