1 | /** @file GS2LuceneDelete.java
|
---|
2 | *
|
---|
3 | * Provides a wrapper to the document deleting features of Lucene.
|
---|
4 | *
|
---|
5 | * This java application no longer makes use of the long-deprecated
|
---|
6 | * existing Lucene class IndexModifier
|
---|
7 | * to access and make changes to the information stored about documents in a
|
---|
8 | * Lucene database. This is an essential component of the IncrementalBuilder
|
---|
9 | * PERL module, and endevours to make editing the text and metadata of
|
---|
10 | * documents without having to rebuild the entire collection a reality (in
|
---|
11 | * other words, true incremental/dynamic building).
|
---|
12 | *
|
---|
13 | * A component of the Greenstone digital library software from the New Zealand
|
---|
14 | * Digital Library Project at the University of Waikato, New Zealand.
|
---|
15 | *
|
---|
16 | * This program is free software; you can redistribute it and/or modify it
|
---|
17 | * under the terms of the GNU General Public License as published by the Free
|
---|
18 | * Software Foundation; either version 2 of the License, or (at your option)
|
---|
19 | * any later version.
|
---|
20 | *
|
---|
21 | * This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
22 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
23 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
24 | * more details.
|
---|
25 | *
|
---|
26 | * You should have received a copy of the GNU General Public License along
|
---|
27 | * with this program; if not, write to the Free Software Foundation, Inc., 675
|
---|
28 | * Mass Ave, Cambridge, MA 02139, USA.
|
---|
29 | *
|
---|
30 | * Copyright (c) 2006 DL Consulting Ltd., New Zealand
|
---|
31 | */
|
---|
32 |
|
---|
33 | package org.greenstone.LuceneWrapper4;
|
---|
34 |
|
---|
35 | import java.io.IOException;
|
---|
36 | //import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
---|
37 | import org.apache.lucene.analysis.Analyzer;
|
---|
38 | import org.apache.lucene.index.IndexWriter;
|
---|
39 | import org.apache.lucene.index.Term;
|
---|
40 | import org.apache.lucene.util.Version;
|
---|
41 |
|
---|
42 | /** Contains methods for deleting a document that has previously been indexed
|
---|
43 | * into a Lucene database.
|
---|
44 | * @author John Thompson, DL Consulting Ltd. (unless stated otherwise)
|
---|
45 | */
|
---|
46 | public class GS2LuceneDelete
|
---|
47 | {
|
---|
48 | /** This is the main entry point to the deletor and is responsible for
|
---|
49 | * parsing the arguments and creating an instance of the deletor class.
|
---|
50 | *
|
---|
51 | * @param args The arguments passed into the application as a string
|
---|
52 | * array
|
---|
53 | * @return An integer describing the exit state of the application
|
---|
54 | * @throws Exception on any fatal error state
|
---|
55 | */
|
---|
56 | static public void main (String args[])
|
---|
57 | throws Exception
|
---|
58 | {
|
---|
59 | // Parse arguments
|
---|
60 | String index_path = "";
|
---|
61 | int node_id = -1;
|
---|
62 |
|
---|
63 | for (int i = 0; i < args.length; i += 2)
|
---|
64 | {
|
---|
65 | if (args[i].equals("--index"))
|
---|
66 | {
|
---|
67 | index_path = args[i + 1];
|
---|
68 | }
|
---|
69 | else if (args[i].equals("--nodeid"))
|
---|
70 | {
|
---|
71 | node_id = Integer.parseInt(args[i + 1]);
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | System.out.println("Error! Unknown argument: " + args[i]);
|
---|
76 | GS2LuceneDelete.printUsage();
|
---|
77 | System.exit(0);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | // Check arguments
|
---|
82 | if (index_path.equals(""))
|
---|
83 | {
|
---|
84 | System.out.println("Error! Missing index path");
|
---|
85 | GS2LuceneDelete.printUsage();
|
---|
86 | System.exit(0);
|
---|
87 | }
|
---|
88 | if (node_id == -1)
|
---|
89 | {
|
---|
90 | System.out.println("Error! Missing or invalid Node ID");
|
---|
91 | GS2LuceneDelete.printUsage();
|
---|
92 | System.exit(0);
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Instantiate deletor, and perform the delete
|
---|
96 | GS2LuceneDelete deletor = new GS2LuceneDelete(index_path);
|
---|
97 | deletor.deleteDocument(node_id);
|
---|
98 | deletor.destroy();
|
---|
99 | deletor = null;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /** Display program usage message.
|
---|
104 | */
|
---|
105 | static public void printUsage()
|
---|
106 | {
|
---|
107 | System.out.println("usage: GS2LuceneDelete --index <path> --nodeid <int>");
|
---|
108 | System.out.println("");
|
---|
109 | System.out.println("where:");
|
---|
110 | System.out.println(" index - is the full path to the directory containing the directory");
|
---|
111 | System.out.println(" to edit, including the level (ie didx, sidx)");
|
---|
112 | System.out.println(" nodeid - the unique identifier of the document to delete. This is the");
|
---|
113 | System.out.println(" same as the docnum in the GDBM");
|
---|
114 | System.out.println("");
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /** **/
|
---|
119 | private boolean debug = true;
|
---|
120 |
|
---|
121 | /** **/
|
---|
122 | private IndexWriter index_writer = null;
|
---|
123 |
|
---|
124 |
|
---|
125 | /** Constructor which takes the path to the Lucene index to be edited.
|
---|
126 | *
|
---|
127 | * @param index_path The full path to the index directory as a String
|
---|
128 | */
|
---|
129 | public GS2LuceneDelete(String index_path)
|
---|
130 | throws IOException
|
---|
131 | {
|
---|
132 | index_writer = GSLuceneUtil.getIndexWriter(index_path);
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /** When called prints a debug message but only if debugging is enabled.
|
---|
137 | */
|
---|
138 | public void debug(String message)
|
---|
139 | {
|
---|
140 | if (debug)
|
---|
141 | {
|
---|
142 | System.err.println(message);
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /** Destructor which unallocates connection to Lucene.
|
---|
148 | */
|
---|
149 | public void destroy()
|
---|
150 | throws IOException
|
---|
151 | {
|
---|
152 | index_writer.close();
|
---|
153 | index_writer = null;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /** Delete the indicated document from the Lucene index. This process is
|
---|
158 | * very similar to the initial step of index editing.
|
---|
159 | *
|
---|
160 | * @param node_id The unique identifier of a Lucene document as an
|
---|
161 | * integer
|
---|
162 | */
|
---|
163 | public void deleteDocument(int node_id)
|
---|
164 | throws IOException
|
---|
165 | {
|
---|
166 | debug("GS2LuceneDelete.deleteDocument(" + node_id + ")");
|
---|
167 | debug("- Initial number of documents in index: " + index_writer.numDocs());
|
---|
168 | index_writer.deleteDocuments(new Term("nodeid", "" + node_id));
|
---|
169 | debug("- Final number of documents in index: " + index_writer.numDocs());
|
---|
170 | }
|
---|
171 | }
|
---|