private bool RenameDocumentInIndex(RenamedEventArgs renArgs, SearchContext context)
        {
            using (var indexDirectory = FSDirectory.Open(context.IndexPath))
            {
                using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                {
                    using (var indexWriter = new IndexWriter(indexDirectory, analyser, false, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        Document doc       = new Document();
                        Field    nameField = new Field("name", renArgs.Name, Field.Store.YES, Field.Index.ANALYZED);
                        Field    extField  = new Field("ext", Path.GetExtension(renArgs.FullPath), Field.Store.YES, Field.Index.NOT_ANALYZED);
                        Field    pathField = new Field("path", renArgs.FullPath, Field.Store.YES, Field.Index.NOT_ANALYZED);
                        doc.Add(nameField);
                        doc.Add(extField);
                        doc.Add(pathField);

                        //indexWriter?.DeleteDocuments(new Term("name", renArgs.OldName.ToLower()));
                        //indexWriter?.AddDocument(doc);
                        indexWriter?.UpdateDocument(new Term("path", renArgs.OldFullPath), doc);
                        indexWriter?.Optimize();
                        DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                            Data = string.Format("File Renamed: {0}", renArgs.FullPath)
                        });
                    }
                    analyser.Close();
                }
            }
            return(true);
        }
 private bool DeleteDocumentInIndex(FileSystemEventArgs fseArgs, SearchContext context)
 {
     using (var indexDirectory = FSDirectory.Open(context.IndexPath))
     {
         using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
         {
             using (var indexWriter = new IndexWriter(indexDirectory, analyser, false, IndexWriter.MaxFieldLength.UNLIMITED))
             {
                 //try query overload of DeleteDocuments
                 indexWriter?.DeleteDocuments(new Term("name", fseArgs.Name));
                 indexWriter?.Optimize();
                 DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                     Data = string.Format("File Deleted: {0}", fseArgs.FullPath)
                 });
             }
             analyser.Close();
         }
     }
     return(true);
 }
        public bool AddDocumentToIndex(string fullFilePath, SearchContext context)
        {
            using (var indexDirectory = FSDirectory.Open(context.IndexPath))
            {
                using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                {
                    using (var indexWriter = new IndexWriter(indexDirectory, analyser, false, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        Document document = new Document {
                        };
                        DocumentData doc  = new DocumentData
                        {
                            FileName  = Path.GetFileNameWithoutExtension(fullFilePath),
                            Extention = Path.GetExtension(fullFilePath),
                            FilePath  = fullFilePath
                        };

                        Field nameField = new Field("name", doc.FileName, Field.Store.YES, Field.Index.ANALYZED);
                        Field extField  = new Field("ext", doc.Extention, Field.Store.YES, Field.Index.NOT_ANALYZED);
                        Field pathField = new Field("path", doc.FilePath, Field.Store.YES, Field.Index.NOT_ANALYZED);
                        document.Add(nameField);
                        document.Add(extField);
                        document.Add(pathField);

                        indexWriter?.AddDocument(document);
                        indexWriter?.Optimize();
                        DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                            Data = string.Format("File Created: {0}", doc.FilePath)
                        });
                    }
                    analyser.Close();
                }
            }

            return(true);
        }
        public bool BuildIndex(SearchContext context)
        {
            _searchContext = context;

            try
            {
                if (System.IO.Directory.Exists(context.IndexPath))
                {
                    System.IO.Directory.Delete(context.IndexPath, true);
                }

                using (var indexDirectory = FSDirectory.Open(context.IndexPath))
                {
                    using (var analyser = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                    {
                        using (var indexWriter = new IndexWriter(indexDirectory, analyser, IndexWriter.MaxFieldLength.UNLIMITED))
                        {
                            //yield return call
                            foreach (var doc in _fileScanner.GetFileListWithFullPath(context.ScanPath))
                            {
                                if (doc == null)
                                {
                                    continue;
                                }

                                Document document  = new Document();
                                Field    nameField = new Field("name", doc.FileName, Field.Store.YES, Field.Index.ANALYZED);
                                Field    extField  = new Field("ext", doc.Extention, Field.Store.YES, Field.Index.NOT_ANALYZED);
                                Field    pathField = new Field("path", doc.FilePath, Field.Store.YES, Field.Index.NOT_ANALYZED);

                                document.Add(nameField);
                                document.Add(pathField);
                                document.Add(extField);
                                indexWriter?.AddDocument(document);
                                //Trace.WriteLine(string.Format("Added {0} to Index", doc.FileName));
                                //currStatus = string.Format("Added {0} to Index", doc.FilePath);

                                DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                                    Data = string.Format("Indexing: {0}", doc.FilePath)
                                });
                            }
                            indexWriter.Optimize();
                        }
                        analyser.Close();
                    }
                }

                DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                    Data = "Indexing Complete !!"
                });
            }
            catch (Exception ex)
            {
                DocumentAddedEvent?.Invoke(this, new EventDataArgs {
                    Data = string.Format("Error: {0}", ex.Message)
                });

                return(false);
            }
            return(true);
        }