Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
DeleteFiles.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Index;
20 using FSDirectory = Lucene.Net.Store.FSDirectory;
21 
22 namespace Lucene.Net.Demo
23 {
25  public static class DeleteFiles
26  {
27 
29  [STAThread]
30  public static void Main(System.String[] args)
31  {
32  var usage = typeof(DeleteFiles) + " <unique_term>";
33  if (args.Length == 0)
34  {
35  Console.Error.WriteLine("Usage: " + usage);
36  Environment.Exit(1);
37  }
38 
39  try
40  {
41  // We don't want a read-only reader because we are about to delete.
42  using (var directory = FSDirectory.Open("index"))
43  using (var reader = IndexReader.Open(directory, false))
44  {
45  var term = new Term("path", args[0]);
46  var deleted = reader.DeleteDocuments(term);
47 
48  Console.Out.WriteLine("deleted " + deleted + " documents containing " + term);
49 
50  // one can also delete documents by their internal id:
51  /*
52  for (int i = 0; i < reader.MaxDoc; i++) {
53  Console.Out.WriteLine("Deleting document with id " + i);
54  reader.DeleteDocument(i);
55  }
56  */
57 
58  reader.Commit();
59  }
60  }
61  catch (Exception e)
62  {
63  Console.Out.WriteLine(" caught a " + e.GetType() + "\n with message: " + e.Message);
64  }
65  }
66  }
67 }