KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > SnipIndexer


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip;
27
28 import org.apache.lucene.index.IndexReader;
29 import org.apache.lucene.index.IndexWriter;
30 import org.apache.lucene.index.Term;
31 import org.apache.lucene.queryParser.MultiFieldQueryParser;
32 import org.apache.lucene.search.Hits;
33 import org.apache.lucene.search.IndexSearcher;
34 import org.apache.lucene.search.Query;
35 import org.apache.lucene.search.Searcher;
36 import org.apache.lucene.analysis.standard.StandardAnalyzer;
37 import org.radeox.util.logging.Logger;
38 import org.snipsnap.app.Application;
39 import org.snipsnap.container.Components;
40
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.Map JavaDoc;
46
47
48 /**
49  * Indexes snips for fulltext searches
50  *
51  * @author stephan
52  * @version $Id: SnipIndexer.java 1663 2004-06-21 07:22:55Z leo $
53  */

54
55 public class SnipIndexer {
56   private static final String JavaDoc[] searchFields = new String JavaDoc[]{"content", "title"};
57   private Map JavaDoc indexMap = new HashMap JavaDoc();
58
59   private File JavaDoc indexFile() {
60     Application app = Application.get();
61     String JavaDoc appOid = (String JavaDoc)app.getObject(Application.OID);
62     File JavaDoc indexFile = (File JavaDoc) indexMap.get(appOid);
63     if (indexFile == null) {
64       indexFile = Application.get().getConfiguration().getIndexPath();
65       indexMap.put(appOid, indexFile);
66     }
67     return indexFile;
68   }
69
70   public void deleteIndex() throws IOException JavaDoc {
71     File JavaDoc indexFile = indexFile();
72     if(indexFile.exists() && indexFile.isDirectory()) {
73       File JavaDoc oldIndexFile = new File JavaDoc(indexFile.getParentFile(), indexFile.getName()+".old");
74       boolean done = indexFile.renameTo(oldIndexFile);
75       if(done) {
76         removeDirectory(oldIndexFile);
77       } else {
78         throw new IOException JavaDoc("unable to move old index directory to "+oldIndexFile);
79       }
80     }
81   }
82
83   private void removeDirectory(File JavaDoc root) {
84     File JavaDoc[] files = root.listFiles();
85     for(int f = 0; f < files.length; f++) {
86       if(files[f].isDirectory()) {
87         removeDirectory(files[f]);
88       }
89       boolean done = files[f].delete();
90       if(!done) {
91         Logger.fatal("Unable to delete old index directory: "+files[f]);
92       }
93     }
94
95     if (!root.delete()) {
96       Logger.fatal("Unable to delete old index directory: " + root);
97     }
98   }
99
100   public void removeIndex(Snip snip) {
101     IndexReader reader = null;
102     try {
103       reader = IndexReader.open(indexFile());
104       int count = reader.delete(new Term("id", Integer.toHexString(snip.getName().hashCode())));
105       // Logger.debug("Deleted: "+ count );
106
} catch (IOException JavaDoc e) {
107       Logger.warn("Unable to delete snip " + snip.getName() + " from index", e);
108     } finally {
109       close(reader);
110     }
111     return;
112   }
113
114   public void reIndex(Snip snip) {
115     index(snip, true);
116     return;
117   }
118
119   public void index(Snip snip) {
120     index(snip, false);
121     return;
122   }
123
124   private synchronized void index(Snip snip, boolean exists) {
125     IndexWriter writer = null;
126
127     try {
128       File JavaDoc f;
129       boolean create = true;
130       // create index if the directory does not exist
131
if ((f = indexFile()).exists() && f.isDirectory()) {
132         create = false;
133       } else {
134         create = true;
135       }
136
137       if (exists && f.exists()) {
138         removeIndex(snip);
139       }
140
141       writer = new IndexWriter(f, new StandardAnalyzer(), create);
142       writer.mergeFactor = 20;
143       writer.addDocument(SnipDocument.Document(snip));
144       writer.optimize();
145     } catch (IOException JavaDoc e) {
146       Logger.warn("Unable to index snip", e);
147     } finally {
148       close(writer);
149     }
150   }
151
152   public Hits search(String JavaDoc queryString) {
153     Searcher searcher = null;
154     try {
155       searcher = new IndexSearcher(indexFile().getAbsolutePath());
156     } catch (IOException JavaDoc e) {
157       Logger.warn("Unable to open index file: " + indexFile(), e);
158     }
159
160     // When there is only one term, then add '*' to
161
// end of the query to get more matches
162
//if (-1 == queryString.indexOf(' ')) {
163
// queryString = queryString + "*";
164
//}
165

166     // parse the query String.
167
Query query = null;
168     try {
169       query = MultiFieldQueryParser.parse(queryString, searchFields, new StandardAnalyzer());
170     } catch (org.apache.lucene.queryParser.ParseException e1) {
171       close(searcher);
172       Logger.warn("Unable to parse: '" + queryString + "'");
173     }
174
175     // get the hits from the searcher for
176
// the given query
177
Hits hits = null;
178     try {
179       hits = searcher.search(query);
180     } catch (IOException JavaDoc e) {
181       close(searcher);
182       Logger.warn("SnipIndexer: unable to get hits", e);
183     }
184     return hits;
185   }
186
187   private static void close(IndexWriter writer) {
188     if (null != writer) {
189       try {
190         writer.close();
191       } catch (Exception JavaDoc e) {
192       }
193     }
194   }
195
196   private static void close(Searcher searcher) {
197     if (null != searcher) {
198       try {
199         searcher.close();
200       } catch (Exception JavaDoc e) {
201       }
202     }
203   }
204
205   private static void close(IndexReader reader) {
206     if (null != reader) {
207       try {
208         reader.close();
209       } catch (Exception JavaDoc e) {
210       }
211     }
212   }
213
214 }
215
216
217
Popular Tags