fixed 2 problems with john's code: publish_path shouldn't be used, and the
[mir.git] / source / mircoders / producer / IndexingProducerNode.java
1 /* Copyright (C) 2001, 2002  The Mir-coders group
2  *
3  * This file is part of Mir.
4  *
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.
9  *
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.
14  *
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
18  *
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.
29  */
30
31 package mircoders.producer;
32
33 import java.util.*;
34 import java.io.*;
35
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;
41
42
43 import mir.util.*;
44 import mir.misc.*;
45 import mir.log.*;
46 import mir.entity.*;
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.*;
54
55
56 public class IndexingProducerNode implements ProducerNode {
57   private String contentKey;
58   private String indexPath;
59
60
61   public IndexingProducerNode(String aContentKey, String pathToIndex) {
62     contentKey = aContentKey;
63     indexPath=pathToIndex;
64   }
65
66   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
67     IndexWriter indexWriter = null;
68     Object data;
69     Entity entity;
70     String index = null;
71     long startTime;
72     long endTime;
73
74     startTime = System.currentTimeMillis();
75
76
77
78     try {
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);
83       }
84
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);
88       }
89       aLogger.info("Indexing " + (String) entity.getValue("id") + " into " + index);
90
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);
95         indexCreator.close();
96       }
97
98       IndexUtil.unindexEntity((EntityContent) entity,index);
99
100       indexWriter = new IndexWriter(index, new StandardAnalyzer(), false);
101       Document theDoc =  new Document();
102
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
107
108       //this initialization should go somewhere global like an xml file....
109
110       (new KeywordSearchTerm("id","","id","","id")).index(theDoc,entity);
111
112       (new KeywordSearchTerm("webdb_create_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted")).index(theDoc,entity);
113
114       (new UnIndexedSearchTerm("","","","where","where")).indexValue(theDoc, StringUtil.webdbDate2path(entity.getValue("date"))+entity.getValue("id")+".shtml");
115
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);
120
121       (new ContentSearchTerm("content_data","search_content","content","","")).indexValue(theDoc,
122                                                                                      entity.getValue("content_data")+ " "
123                                                                                      + entity.getValue("description")+ " "
124                                                                                      + entity.getValue("title")
125                                                                                      );
126
127       (new TopicSearchTerm()).index(theDoc,entity);
128
129       (new ImagesSearchTerm()).index(theDoc,entity);
130
131       (new AudioSearchTerm()).index(theDoc,entity);
132
133       (new VideoSearchTerm()).index(theDoc,entity);
134
135
136       //comments-just aggregate all relevant fields
137       //removed until i get a chance to do this right
138
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();
147       //  }
148       //}
149       //theDoc.add(Field.UnStored("comments",commentsAggregate));
150
151       indexWriter.addDocument(theDoc);
152
153
154     }
155     catch (Throwable t) {
156       aLogger.error("Error while indexing content: " + t.getMessage());
157       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
158     }
159     finally {
160       if (indexWriter != null){
161         try{
162           indexWriter.close();
163         }
164         catch (Throwable t) {
165           aLogger.warn("Error while closing indexWriter: " + t.getMessage());
166         }
167
168       }
169       try{
170         FSDirectory theIndexDir=FSDirectory.getDirectory(index,false);
171         if (IndexReader.isLocked(theIndexDir)){
172           IndexReader.unlock(theIndexDir);
173         }
174       }
175       catch (Throwable t) {
176         aLogger.warn("Error while unlocking index: " + t.getMessage());
177       }
178     }
179
180
181
182
183     endTime = System.currentTimeMillis();
184
185     aLogger.info("  IndexTime: " + (endTime-startTime) + " ms<br>");
186   }
187 }
188
189
190
191
192
193
194
195
196
197