Пример #1
0
        } // doIndex

        private static void removeAllDuplicateAndDeletedFiles(IndexableFileInfo[] fileInfos, string LuceneIndexDir, IndexCreationMode indexCreationMode)
        {
            if (indexCreationMode != IndexCreationMode.AppendToExistingIndex)
            {
                return;
            }

            IndexReader reader = IndexReader.Open(LuceneIndexDir);

            try
            {
                int numDocs = reader.NumDocs();
                for (int i = 0; i < numDocs; i++)
                {
                    Document docToCheck         = reader.Document(i);
                    bool     removeDocFromIndex = true;
                    string   filenameField      = docToCheck.GetField("filename").StringValue();
                    string   lastModified       = (docToCheck.GetField("LastModified").StringValue());

                    foreach (IndexableFileInfo fi in fileInfos)
                    {
                        if (String.Compare(fi.Filename, filenameField, true) == 0 && DateTools.DateToString(fi.LastModified, DateTools.Resolution.SECOND) == lastModified)
                        {
                            removeDocFromIndex = false;
                            break;
                        }
                    } // foreach

                    if (removeDocFromIndex)
                    {
                        reader.DeleteDocument(i);
                        if (!reader.HasDeletions())
                        {
                            throw new Exception("error: deletion failed!!");
                        }
                    }
                } // for each lucene doc
            }
            finally
            {
                reader.Close();
            }
            LuceneIndexer indexer = new LuceneIndexer(LuceneIndexDir, indexCreationMode); // open up the index again

            indexer.CloseIndexWriter(OptimizeMode.DoOptimization);                        // just to optimize the index (which removes deleted items).
        }
Пример #2
0
        public static void doIndex(string LuceneIndexDir, string SpellingIndexDir, IndexCreationMode indexCreationMode, IndexableFileInfo[] fileInfos, object ThreadState, onAddFileToIndex AddFileToIndex)
        {
            if (indexing)
            {
                return;
            }
            lock (padlock)
            {
                indexing = true;


                LuceneIndexer indexer = new LuceneIndexer(LuceneIndexDir, indexCreationMode); // create new index
                try
                {
                    for (int i = 0; i < fileInfos.Length; i++)
                    {
                        IndexableFileInfo fi = fileInfos[i];
                        if (AddFileToIndex != null)
                        {
                            AddFileToIndex(fi, (double)i / (double)fileInfos.Length);
                        }

                        indexer.addFileInfoToIndex(fi);
                    } // foreach
                }
                finally
                {
                    indexer.CloseIndexWriter(OptimizeMode.DoNotOptimize);
                }
                if (indexCreationMode == IndexCreationMode.AppendToExistingIndex)
                {
                    removeAllDuplicateAndDeletedFiles(fileInfos, LuceneIndexDir, indexCreationMode);
                }
                try
                {
                    doSpellCheckerIndexing(LuceneIndexDir, SpellingIndexDir);
                }
                catch
                { }



                indexing = false;
            }
        } // doIndex