KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > util > LuceneUtils


1 /*
2  * $Id: LuceneUtils.java,v 1.5 2005/01/14 22:40:17 michelson Exp $
3  *
4  * Copyright (c) 2005 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.util;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.lucene.analysis.Analyzer;
34 import org.apache.lucene.analysis.standard.StandardAnalyzer;
35 import org.apache.lucene.document.Document;
36 import org.apache.lucene.document.Field;
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.TermDocs;
41
42 import com.j2biz.blogunity.pojo.Entry;
43
44 public class LuceneUtils {
45     /**
46      * Logger for this class
47      */

48     private static final Log log = LogFactory.getLog(LuceneUtils.class);
49
50     public static void createNewIndex(File JavaDoc indexesDir, Analyzer analyzer) {
51         try {
52             IndexWriter writer = new IndexWriter(indexesDir, analyzer, true);
53             writer.close();
54         } catch (IOException JavaDoc e) {
55             log.error("Error creating index-directory " + indexesDir.getAbsolutePath(), e);
56         }
57     }
58
59     public static void index(Entry entry, File JavaDoc indexDir) {
60
61         try {
62             if (log.isDebugEnabled()) {
63                 log.debug("Indexing entry with ID=" + entry.getId());
64             }
65
66             boolean create = false;
67             if (indexDir.listFiles().length == 0) create = true;
68
69             IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), create);
70
71             Document doc = new Document();
72
73             doc.add(Field.Text("title", (entry.getRawTitle() != null) ? entry.getRawTitle() : ""));
74
75             doc.add(Field.Text("body", (entry.getRawBody() != null) ? entry.getRawBody() : ""));
76
77             doc.add(Field.Text("excerpt", (entry.getRawExcerpt() != null) ? entry.getRawExcerpt()
78                     : ""));
79
80             doc.add(Field.Keyword("id", "" + entry.getId()));
81
82             doc.add(Field.Keyword("permalink", (entry.getPermalink() != null) ? entry
83                     .getPermalink() : "" + entry.getId()));
84
85             doc.add(Field.Keyword("aliasname", (entry.getAliasname() != null) ? entry
86                     .getAliasname() : "" + entry.getId()));
87
88             doc.add(Field.Keyword("author", entry.getAuthor().getNickname()));
89
90             doc.add(Field.Keyword("blog", entry.getBlog().getUrlName()));
91
92             doc.add(Field.Keyword("createTime", BlogUtils.getInstance().formatDateTime(
93                     entry.getCreateTime())));
94
95             writer.addDocument(doc);
96
97             writer.close();
98
99         } catch (IOException JavaDoc e) {
100             log.error("Error indexing entry with ID=" + entry.getId(), e);
101         }
102     }
103
104     public static void remove(Entry entry, File JavaDoc indexDir) {
105         IndexReader reader = null;
106         try {
107
108             if (log.isDebugEnabled()) {
109                 log.debug("Removing from index entry with ID=" + entry.getId());
110             }
111
112             reader = IndexReader.open(indexDir);
113             Term t = new Term("id", "" + entry.getId());
114
115             TermDocs docs = reader.termDocs(t);
116
117             while (docs.next()) {
118                 int i = docs.doc();
119                 reader.delete(i);
120             }
121
122         } catch (IOException JavaDoc e) {
123             log.error("Error removing entry with ID=" + entry.getId() + " from index!", e);
124         }
125
126         finally {
127             try {
128                 if (reader != null) reader.close();
129             } catch (IOException JavaDoc e) { /* suck it up */
130             }
131         }
132
133     }
134
135 }
Popular Tags