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.
31 package mircoders.producer;
36 import org.apache.lucene.analysis.standard.StandardAnalyzer;
37 import org.apache.lucene.index.*;
38 import org.apache.lucene.document.Document;
39 import org.apache.lucene.document.Field;
40 import org.apache.lucene.store.FSDirectory;
47 import mir.entity.adapter.*;
48 import mir.producer.*;
49 import mircoders.global.*;
50 import mircoders.localizer.*;
51 import mircoders.entity.*;
52 import mircoders.storage.*;
53 import mircoders.search.*;
56 public class IndexingProducerNode implements ProducerNode {
57 private String contentKey;
58 private String indexPath;
61 public IndexingProducerNode(String aContentKey, String pathToIndex) {
62 contentKey = aContentKey;
63 indexPath=pathToIndex;
66 public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
67 IndexWriter indexWriter = null;
74 startTime = System.currentTimeMillis();
79 index = ParameterExpander.expandExpression(aValueMap, indexPath);
80 data = ParameterExpander.findValueForKey( aValueMap, contentKey );
81 if (! (data instanceof EntityAdapter)) {
82 throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
85 entity = ((EntityAdapter) data).getEntity();
86 if (! (entity instanceof EntityContent)) {
87 throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
89 aLogger.info("Indexing " + (String) entity.getValue("id") + " into " + index);
91 // create an index here if one did not already exist
92 if (! (IndexReader.indexExists(index))){
93 aLogger.error("Didn't find existing index, so I'm making one in "+index);
94 IndexWriter indexCreator = new IndexWriter(index,new StandardAnalyzer(),true);
98 IndexUtil.unindexEntity((EntityContent) entity,index);
100 indexWriter = new IndexWriter(index, new StandardAnalyzer(), false);
101 Document theDoc = new Document();
103 // Keyword is stored and indexed, but not tokenized
104 // Text is tokenized,stored, indexed
105 // Unindexed is not tokenized or indexed, only stored
106 // Unstored is tokenized and indexed, but not stored
108 //this initialization should go somewhere global like an xml file....
110 (new KeywordSearchTerm("id","","id","","id")).index(theDoc,entity);
112 (new KeywordSearchTerm("webdb_create_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted")).index(theDoc,entity);
114 (new UnIndexedSearchTerm("","","","where","where")).indexValue(theDoc, StringUtil.webdbDate2path(entity.getValue("date"))+entity.getValue("id")+".shtml");
116 (new TextSearchTerm("creator","search_creator","creator","creator","creator")).index(theDoc,entity);
117 (new TextSearchTerm("title","search_title","title","title","title")).index(theDoc,entity);
118 (new UnIndexedSearchTerm("description","search_content","description","description","description")).index(theDoc,entity);
119 (new UnIndexedSearchTerm("webdb_create","search_irrelevant","creationDate","creationDate","creationDate")).index(theDoc,entity);
121 (new ContentSearchTerm("content_data","search_content","content","","")).indexValue(theDoc,
122 entity.getValue("content_data")+ " "
123 + entity.getValue("description")+ " "
124 + entity.getValue("title")
127 (new TopicSearchTerm()).index(theDoc,entity);
129 (new ImagesSearchTerm()).index(theDoc,entity);
131 (new AudioSearchTerm()).index(theDoc,entity);
133 (new VideoSearchTerm()).index(theDoc,entity);
136 //comments-just aggregate all relevant fields
137 //removed until i get a chance to do this right
139 //String commentsAggregate = "";
140 //TemplateModel comments=entity.get("to_comments");
141 //if (comments != null){
142 // while (((TemplateListModel)comments).hasNext()){
143 // TemplateModel aComment = ((TemplateListModel)comments).next();
144 // commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
145 // + " " + ((TemplateHashModel)aComment).get("creator").toString()
146 // + " " + ((TemplateHashModel)aComment).get("text").toString();
149 //theDoc.add(Field.UnStored("comments",commentsAggregate));
151 indexWriter.addDocument(theDoc);
155 catch (Throwable t) {
156 aLogger.error("Error while indexing content: " + t.getMessage());
157 t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
160 if (indexWriter != null){
164 catch (Throwable t) {
165 aLogger.warn("Error while closing indexWriter: " + t.getMessage());
170 FSDirectory theIndexDir=FSDirectory.getDirectory(index,false);
171 if (IndexReader.isLocked(theIndexDir)){
172 IndexReader.unlock(theIndexDir);
175 catch (Throwable t) {
176 aLogger.warn("Error while unlocking index: " + t.getMessage());
183 endTime = System.currentTimeMillis();
185 aLogger.info(" IndexTime: " + (endTime-startTime) + " ms<br>");