[12264] | 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 |
|
---|
[13686] | 30 | package org.greenstone.LuceneWrapper;
|
---|
[12264] | 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.IndexModifier;
|
---|
| 37 | import org.apache.lucene.index.IndexReader;
|
---|
| 38 | import org.apache.lucene.index.IndexWriter;
|
---|
| 39 | import org.apache.lucene.index.Term;
|
---|
| 40 | import org.apache.lucene.index.TermPositions;
|
---|
| 41 | import org.apache.lucene.search.Searcher;
|
---|
| 42 | import org.apache.lucene.search.IndexSearcher;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | */
|
---|
| 46 | public class GS2IndexModifier
|
---|
| 47 | extends IndexModifier
|
---|
| 48 | {
|
---|
| 49 | /** **/
|
---|
| 50 | private boolean debug = true;
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | */
|
---|
| 54 | public GS2IndexModifier (String dir_name, Analyzer analyzer)
|
---|
| 55 | throws IOException
|
---|
| 56 | {
|
---|
| 57 | super(dir_name, analyzer, false);
|
---|
| 58 | }
|
---|
| 59 | /** GS2IndexModifier() **/
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | */
|
---|
| 63 | protected void debug(String message)
|
---|
| 64 | {
|
---|
| 65 | if(debug)
|
---|
| 66 | {
|
---|
| 67 | System.err.println(message);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | /** debug() **/
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | */
|
---|
| 74 | public Document document(int n)
|
---|
| 75 | throws IOException
|
---|
| 76 | {
|
---|
| 77 | // Ensure the reader exists
|
---|
| 78 | createIndexReader();
|
---|
| 79 | return indexReader.document(n);
|
---|
| 80 | }
|
---|
| 81 | /** document() **/
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | */
|
---|
| 85 | public int getDocNumByNodeID(int node_id)
|
---|
| 86 | throws IOException
|
---|
| 87 | {
|
---|
| 88 | debug("GS2IndexModifier.getDocument(" + node_id + ")");
|
---|
| 89 | int doc_num = -1;
|
---|
| 90 | // Create a new term to encapsulate this node id
|
---|
| 91 | Term term = new Term("nodeID", 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 **/
|
---|