View Javadoc

1   /*
2    * MMBase Lucene module
3    *
4    * The contents of this file are subject to the Mozilla Public License
5    * Version 1.0 (the "License"); you may not use this file except in
6    * compliance with the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS"
10   * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11   * License for the specific language governing rights and limitations
12   * under the License.
13   */
14  package net.sf.mmapps.modules.lucenesearch;
15  
16  import java.io.IOException;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.lucene.analysis.Analyzer;
21  import org.apache.lucene.analysis.standard.StandardAnalyzer;
22  import org.apache.lucene.document.Document;
23  import org.apache.lucene.index.IndexReader;
24  import org.apache.lucene.index.IndexWriter;
25  import org.apache.lucene.index.Term;
26  import org.apache.lucene.search.IndexSearcher;
27  
28  /***
29   * This object handles access to Lucene indexes
30   * 
31   * @author Wouter Heijke
32   * @version $Revision: 1.1 $
33   */
34  public class IndexHelper {
35      private static Log log = LogFactory.getLog(IndexHelper.class);
36  
37      private IndexWriter writer;
38  
39      private Analyzer analyzer = new StandardAnalyzer();
40  
41      private String indexName;
42  
43      /***
44       * Constructor for the IndexHelper object
45       */
46      public IndexHelper() {
47          log.debug("IndexHelper started");
48      }
49  
50      /***
51       * Sets the analyzer attribute of the IndexHelper object
52       * 
53       * @param a The new analyzer value
54       */
55      public void setAnalyzer(Analyzer a) {
56          this.analyzer = a;
57      }
58  
59      /***
60       * Sets the indexName attribute of the IndexHelper object
61       * 
62       * @param name The new indexName value
63       */
64      public void setIndexName(String name) {
65          this.indexName = name;
66      }
67  
68      /***
69       * Returns the name of the index
70       * 
71       * @return Returns the indexName.
72       */
73      public String getIndexName() {
74          return indexName;
75      }
76  
77      /***
78       * Create the index (writer)
79       * 
80       * @param name
81       * @param analyzer
82       */
83      public void create(String name, Analyzer analyzer) {
84          log.debug("creating Lucene storage with index name '" + name + "'");
85          setAnalyzer(analyzer);
86          setIndexName(name);
87          open(true);
88      }
89  
90      /***
91       * Open the writer
92       */
93      public void open() {
94          log.debug("opening Lucene storage with index name '" + indexName + "'");
95          open(false);
96      }
97  
98      /***
99       * Close the writer
100      */
101     public void close() {
102         log.debug("closing Lucene storage with index name '" + indexName + "'");
103         try {
104             writer.optimize();
105             writer.close();
106             writer = null;
107         } catch (IOException e) {
108             log.error("IOException occured when closing Lucene Index with index name '" + indexName + "'");
109             e.printStackTrace();
110         }
111     }
112 
113     /***
114      * Write a Lucene Document to the index
115      * 
116      * @param document Lucene Document
117      */
118     public void write(Document document) {
119         try {
120             writer.addDocument(document);
121         } catch (IOException e) {
122             log.error("IOException occured when writing document to Lucene Index with index name '" + indexName + "'");
123             e.printStackTrace();
124         }
125     }
126 
127     /***
128      * Return the amount of documents in the index when writing to it.
129      * 
130      * @return -1 if no size or the amount of documents in the index
131      */
132     public int size() {
133         int result = -1;
134         if (writer != null) {
135             result = writer.docCount();
136         }
137         return result;
138     }
139 
140     /***
141      * Returns the IndexSearcher for this index
142      * 
143      * @return IndexSearcher
144      */
145     public IndexSearcher getSearcher() {
146         IndexSearcher result = null;
147         try {
148             result = new IndexSearcher(indexName);
149         } catch (IOException e) {
150             log.error("IOException occured when opening Lucene Index with index name '" + indexName + "' for searching");
151             e.printStackTrace();
152         }
153         return result;
154     }
155 
156     /***
157      * Returns the IndexReader for this index
158      * 
159      * @return IndexReader
160      */
161     private IndexReader getReader() {
162         IndexReader result = null;
163         try {
164             result = IndexReader.open(indexName);
165         } catch (IOException e) {
166             log.error("IOException occured when opening Lucene Index with index name '" + indexName + "' for reading");
167             e.printStackTrace();
168         }
169         return result;
170     }
171 
172     /***
173      * Delete a document from the index
174      * 
175      * @param term A Lucene Term describing the document to delete
176      * @return Amount of deleted documents
177      */
178     public int delete(Term term) {
179         int result = -1;
180         try {
181             IndexReader reader = getReader();
182             result = reader.delete(term);
183             reader.close();
184         } catch (IOException e) {
185             log.error("IOException occured when deleting a document from Lucene Index with index name '" + indexName + "'");
186             e.printStackTrace();
187         }
188         return result;
189     }
190 
191     /***
192      * Open the index for writing
193      * 
194      * @param create Open for update (false) or a clean new index (true)
195      */
196     private void open(boolean create) {
197         try {
198             writer = new IndexWriter(indexName, analyzer, create);
199         } catch (IOException e) {
200             log.error("IOException occured when opening Lucene Index with index name '" + indexName + "' for writing");
201             e.printStackTrace();
202         }
203         if (writer != null) {
204             log.debug("lucene storage opened successfully");
205         }
206     }
207 
208     /*
209      * (non-Javadoc)
210      * 
211      * @see java.lang.Object#finalize()
212      */
213     protected void finalize() throws Throwable {
214         log.debug("finalize on index name '" + indexName + "'");
215         try {
216             writer.close();
217         } finally {
218             super.finalize();
219         }
220     }
221 }