1 /* Copyright (C) 2001, 2002 The Mir-coders group
3 * This file is part of Mir.
5 * Mir is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Mir is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mir; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * In addition, as a special exception, The Mir-coders gives permission to link
20 * the code of this program with the com.oreilly.servlet library, any library
21 * licensed under the Apache Software License, The Sun (tm) Java Advanced
22 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
23 * the above that use the same license as the above), and distribute linked
24 * combinations including the two. You must obey the GNU General Public
25 * License in all respects for all of the code used other than the above
26 * mentioned libraries. If you modify this file, you may extend this exception
27 * to your version of the file, but you are not obligated to do so. If you do
28 * not wish to do so, delete this exception statement from your version.
30 package mircoders.producer;
34 import mir.entity.Entity;
35 import mir.entity.adapter.EntityAdapter;
36 import mir.log.LoggerWrapper;
37 import mir.misc.StringUtil;
38 import mir.producer.ProducerFailure;
39 import mir.producer.ProducerNode;
40 import mir.util.ParameterExpander;
41 import mircoders.entity.EntityContent;
42 import mircoders.search.AudioSearchTerm;
43 import mircoders.search.ContentSearchTerm;
44 import mircoders.search.ImagesSearchTerm;
45 import mircoders.search.IndexUtil;
46 import mircoders.search.KeywordSearchTerm;
47 import mircoders.search.TextSearchTerm;
48 import mircoders.search.TopicSearchTerm;
49 import mircoders.search.UnIndexedSearchTerm;
50 import mircoders.search.VideoSearchTerm;
52 import org.apache.lucene.analysis.standard.StandardAnalyzer;
53 import org.apache.lucene.document.Document;
54 import org.apache.lucene.index.IndexReader;
55 import org.apache.lucene.index.IndexWriter;
56 import org.apache.lucene.store.FSDirectory;
59 public class IndexingProducerNode implements ProducerNode {
60 private String contentKey;
61 private String indexPath;
63 public IndexingProducerNode(String aContentKey, String pathToIndex) {
64 contentKey = aContentKey;
65 indexPath = pathToIndex;
68 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger)
69 throws ProducerFailure {
70 IndexWriter indexWriter = null;
77 startTime = System.currentTimeMillis();
80 index = ParameterExpander.expandExpression(aValueMap, indexPath);
81 data = ParameterExpander.findValueForKey(aValueMap, contentKey);
83 if (!(data instanceof EntityAdapter)) {
84 throw new ProducerFailure("IndexingProducerNode: value of '" +
85 contentKey + "' is not an EntityAdapter, but an " +
86 data.getClass().getName(), null);
89 entity = ((EntityAdapter) data).getEntity();
91 if (!(entity instanceof EntityContent)) {
92 throw new ProducerFailure("IndexingProducerNode: value of '" +
93 contentKey + "' is not a content EntityAdapter, but a " +
94 entity.getClass().getName() + " adapter", null);
97 aLogger.info("Indexing " + (String) entity.getValue("id") + " into " +
100 // create an index here if one did not already exist
101 if (!(IndexReader.indexExists(index))) {
102 aLogger.error("Didn't find existing index, so I'm making one in " +
105 IndexWriter indexCreator =
106 new IndexWriter(index, new StandardAnalyzer(), true);
107 indexCreator.close();
110 IndexUtil.unindexEntity((EntityContent) entity, index);
112 indexWriter = new IndexWriter(index, new StandardAnalyzer(), false);
114 Document theDoc = new Document();
116 // Keyword is stored and indexed, but not tokenized
117 // Text is tokenized,stored, indexed
118 // Unindexed is not tokenized or indexed, only stored
119 // Unstored is tokenized and indexed, but not stored
120 //this initialization should go somewhere global like an xml file....
121 (new KeywordSearchTerm("id", "", "id", "", "id")).index(theDoc, entity);
123 (new KeywordSearchTerm("webdb_create_formatted", "search_date",
124 "webdb_create_formatted", "webdb_create_formatted",
125 "webdb_create_formatted")).index(theDoc, entity);
127 (new UnIndexedSearchTerm("", "", "", "where", "where")).indexValue(theDoc,
128 StringUtil.webdbDate2path(entity.getValue("date")) +
129 entity.getValue("id") + ".shtml");
131 (new TextSearchTerm("creator", "search_creator", "creator", "creator",
132 "creator")).index(theDoc, entity);
133 (new TextSearchTerm("title", "search_title", "title", "title", "title")).index(theDoc,
135 (new UnIndexedSearchTerm("description", "search_content", "description",
136 "description", "description")).index(theDoc, entity);
137 (new UnIndexedSearchTerm("webdb_create", "search_irrelevant",
138 "creationDate", "creationDate", "creationDate")).index(theDoc, entity);
140 (new ContentSearchTerm("content_data", "search_content", "content", "", "")).indexValue(theDoc,
141 entity.getValue("content_data") + " " + entity.getValue("description") +
142 " " + entity.getValue("title"));
144 (new TopicSearchTerm()).index(theDoc, entity);
146 (new ImagesSearchTerm()).index(theDoc, entity);
148 (new AudioSearchTerm()).index(theDoc, entity);
150 (new VideoSearchTerm()).index(theDoc, entity);
152 //comments-just aggregate all relevant fields
153 //removed until i get a chance to do this right
154 //String commentsAggregate = "";
155 //TemplateModel comments=entity.get("to_comments");
156 //if (comments != null){
157 // while (((TemplateListModel)comments).hasNext()){
158 // TemplateModel aComment = ((TemplateListModel)comments).next();
159 // commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
160 // + " " + ((TemplateHashModel)aComment).get("creator").toString()
161 // + " " + ((TemplateHashModel)aComment).get("text").toString();
164 //theDoc.add(Field.UnStored("comments",commentsAggregate));
165 indexWriter.addDocument(theDoc);
167 catch (Throwable t) {
168 aLogger.error("Error while indexing content: " + t.getMessage());
169 t.printStackTrace(aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
172 if (indexWriter != null) {
175 } catch (Throwable t) {
176 aLogger.warn("Error while closing indexWriter: " + t.getMessage());
181 FSDirectory theIndexDir = FSDirectory.getDirectory(index, false);
183 if (IndexReader.isLocked(theIndexDir)) {
184 IndexReader.unlock(theIndexDir);
186 } catch (Throwable t) {
187 aLogger.warn("Error while unlocking index: " + t.getMessage());
191 endTime = System.currentTimeMillis();
193 aLogger.info(" IndexTime: " + (endTime - startTime) + " ms<br>");