using Lucene.Net.Index; using Lucene.Net.Documents; string indexLocation = "C:\\Index\\"; Analyzer analyzer = new StandardAnalyzer(); IndexWriter writer = new IndexWriter(indexLocation, analyzer, true); Document doc = new Document(); doc.Add(new Field("title", "Lucene.Net demo", Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("content", "This is a demonstration of Lucene.Net.", Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Commit(); writer.Dispose();In this example, we create a new index in the "C:\Index\" location, using the StandardAnalyzer to tokenize and index the document. We add a new document to the index containing two fields: "title" and "content". The AddDocument() method creates an index entry for the document, which can later be retrieved using a search query. Overall, IndexWriter is a powerful tool that allows developers to easily create and update search indexes within their applications.
If an index will not have more documents added for a while and optimal search performance is desired, then the optimize method should be called before the index is closed.
Opening an IndexWriter creates a lock file for the directory in use. Trying to open another IndexWriter on the same directory will lead to an IOException. The IOException is also thrown if an IndexReader on the same directory is used to delete documents from the index.