1 package org.apache.lucene.demo; 2 3 /** 4 * Licensed to the Apache Software Foundation (ASF) under one or more 5 * contributor license agreements. See the NOTICE file distributed with 6 * this work for additional information regarding copyright ownership. 7 * The ASF licenses this file to You under the Apache License, Version 2.0 8 * (the "License"); you may not use this file except in compliance with 9 * the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 import java.io.File; 21 22 import org.apache.lucene.store.Directory; 23 import org.apache.lucene.store.FSDirectory; 24 import org.apache.lucene.index.IndexReader; 25 import org.apache.lucene.index.Term; 26 //import org.apache.lucene.index.Term; 27 28 29 /** Deletes documents from an index that do not contain a term. */ 30 public class DeleteFiles { 31 32 private DeleteFiles() {} // singleton 33 34 /** Deletes documents from an index that do not contain a term. */ 35 public static void main(String[] args) { 36 String usage = "java org.apache.lucene.demo.DeleteFiles <unique_term>"; 37 if (args.length == 0) { 38 System.err.println("Usage: " + usage); 39 System.exit(1); 40 } 41 try { 42 Directory directory = FSDirectory.open(new File("index")); 43 IndexReader reader = IndexReader.open(directory, false); // we don't want read-only because we are about to delete 44 45 Term term = new Term("path", args[0]); 46 int deleted = reader.deleteDocuments(term); 47 48 System.out.println("deleted " + deleted + 49 " documents containing " + term); 50 51 // one can also delete documents by their internal id: 52 /* 53 for (int i = 0; i < reader.maxDoc(); i++) { 54 System.out.println("Deleting document with id " + i); 55 reader.delete(i); 56 }*/ 57 58 reader.close(); 59 directory.close(); 60 61 } catch (Exception e) { 62 System.out.println(" caught a " + e.getClass() + 63 "\n with message: " + e.getMessage()); 64 } 65 } 66 }