1 | /** @file GS2IndexModifier.java
|
---|
2 | *
|
---|
3 | * Extends the standard IndexModifier so that a use can retrieve documents and
|
---|
4 | * the underlying IndexReader and IndexWriter.
|
---|
5 | *
|
---|
6 | * Addresses the problem that the Lucene standard IndexModifier can't actually
|
---|
7 | * return a specified Document object, nor does it expose the IndexReader and
|
---|
8 | * IndexWriter so the user can access their methods instead.
|
---|
9 | *
|
---|
10 | * A component of the Greenstone digital library software from the New Zealand
|
---|
11 | * Digital Library Project at the University of Waikato, New Zealand.
|
---|
12 | *
|
---|
13 | * This program is free software; you can redistribute it and/or modify it
|
---|
14 | * under the terms of the GNU General Public License as published by the Free
|
---|
15 | * Software Foundation; either version 2 of the License, or (at your option)
|
---|
16 | * any later version.
|
---|
17 | *
|
---|
18 | * This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
21 | * more details.
|
---|
22 | *
|
---|
23 | * You should have received a copy of the GNU General Public License along
|
---|
24 | * with this program; if not, write to the Free Software Foundation, Inc., 675
|
---|
25 | * Mass Ave, Cambridge, MA 02139, USA.
|
---|
26 | *
|
---|
27 | * Copyright (c) 2006 DL Consulting Ltd., New Zealand
|
---|
28 | */
|
---|
29 |
|
---|
30 | package org.greenstone.LuceneWrapper;
|
---|
31 |
|
---|
32 | import java.io.IOException;
|
---|
33 |
|
---|
34 | import org.apache.lucene.analysis.Analyzer;
|
---|
35 | import org.apache.lucene.document.Document;
|
---|
36 | import org.apache.lucene.index.IndexReader;
|
---|
37 | import org.apache.lucene.index.IndexWriter;
|
---|
38 | import org.apache.lucene.index.Term;
|
---|
39 | import org.apache.lucene.index.TermPositions;
|
---|
40 | import org.apache.lucene.search.Searcher;
|
---|
41 | import org.apache.lucene.search.IndexSearcher;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | */
|
---|
45 | public class GS2IndexModifier
|
---|
46 | extends IndexModifier
|
---|
47 | {
|
---|
48 | /** **/
|
---|
49 | private boolean debug = true;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | */
|
---|
53 | public GS2IndexModifier (String dir_name, Analyzer analyzer)
|
---|
54 | throws IOException
|
---|
55 | {
|
---|
56 | super(dir_name, analyzer, false);
|
---|
57 | }
|
---|
58 | /** GS2IndexModifier() **/
|
---|
59 |
|
---|
60 | /**
|
---|
61 | */
|
---|
62 | protected void debug(String message)
|
---|
63 | {
|
---|
64 | if(debug)
|
---|
65 | {
|
---|
66 | System.err.println(message);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | /** debug() **/
|
---|
70 |
|
---|
71 | /**
|
---|
72 | */
|
---|
73 | public Document document(int n)
|
---|
74 | throws IOException
|
---|
75 | {
|
---|
76 | // Ensure the reader exists
|
---|
77 | createIndexReader();
|
---|
78 | return indexReader.document(n);
|
---|
79 | }
|
---|
80 | /** document() **/
|
---|
81 |
|
---|
82 | /**
|
---|
83 | */
|
---|
84 | public int getDocNumByNodeID(int node_id)
|
---|
85 | throws IOException
|
---|
86 | {
|
---|
87 | debug("GS2IndexModifier.getDocument(" + node_id + ")");
|
---|
88 | int doc_num = -1;
|
---|
89 | // Create a new term to encapsulate this node id
|
---|
90 | // was nodeID, now using docOID --kjdon
|
---|
91 | Term term = new Term("docOID", String.valueOf(node_id));
|
---|
92 | debug("Searching using term: " + term.toString());
|
---|
93 | // Ensure the indexReader exists
|
---|
94 | createIndexReader();
|
---|
95 | // We can return an enumeration of docNums which contains this term
|
---|
96 | TermPositions term_positions = indexReader.termPositions(term);
|
---|
97 | // If the size of term_positions is not 1 then somethings gone wrong in
|
---|
98 | // that the original terms did not uniquely identify a document.
|
---|
99 | if (term_positions.next())
|
---|
100 | {
|
---|
101 | doc_num = term_positions.doc();
|
---|
102 | }
|
---|
103 | else
|
---|
104 | {
|
---|
105 | debug("Term doesn't exists in index");
|
---|
106 | }
|
---|
107 | term_positions.close();
|
---|
108 | // To the garbage collection with thee
|
---|
109 | term_positions = null;
|
---|
110 | term = null;
|
---|
111 | // Done
|
---|
112 | return doc_num;
|
---|
113 | }
|
---|
114 | /** getDocumentByNodeID() **/
|
---|
115 |
|
---|
116 | /**
|
---|
117 | */
|
---|
118 | public IndexReader getIndexReader()
|
---|
119 | {
|
---|
120 | return indexReader;
|
---|
121 | }
|
---|
122 | /** getIndexReader() **/
|
---|
123 |
|
---|
124 | /**
|
---|
125 | */
|
---|
126 | public IndexWriter getIndexWriter()
|
---|
127 | {
|
---|
128 | return indexWriter;
|
---|
129 | }
|
---|
130 | /** getIndexWriter() **/
|
---|
131 | }
|
---|
132 | /** GS2IndexModifier **/
|
---|