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