factored out the "unindex some piece of content code" so it can be used
[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 import freemarker.template.*;
43
44
45 import mir.util.*;
46 import mir.log.*;
47 import mir.producer.*;
48 //import mir.generator.*;
49 import mircoders.global.*;
50 import mircoders.localizer.*;
51 import mir.entity.*;
52 import mir.entity.adapter.*;
53 import mircoders.entity.*;
54 import mircoders.storage.*;
55 import mircoders.search.*;
56
57
58 public class IndexingProducerNode implements ProducerNode {
59   private String contentKey;
60   private String indexPath;
61
62
63   public IndexingProducerNode(String aContentKey, String pathToIndex) {
64     contentKey = aContentKey;
65     indexPath=pathToIndex;
66   }
67
68   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
69     IndexWriter indexWriter = null;
70     Object data;
71     Entity entity;
72     String index = null;
73     long startTime;
74     long endTime;
75
76     startTime = System.currentTimeMillis();
77     
78     
79     
80     try {
81       index = ParameterExpander.expandExpression(aValueMap, indexPath);
82       data =  ParameterExpander.findValueForKey( aValueMap, contentKey );
83       if (! (data instanceof EntityAdapter)) {
84         throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
85       }
86
87       entity = ((EntityAdapter) data).getEntity();
88       if (! (entity instanceof EntityContent)) {
89         throw new ProducerFailure("IndexingProducerNode: value of '"+contentKey+"' is not a content EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
90       }
91       aLogger.info("Indexing " + (String) entity.getValue("id") + " into " + index);
92
93       // create an index here if one did not already exist
94       if (! (IndexReader.indexExists(index))){
95         aLogger.error("Didn't find existing index, so I'm making one in "+index);
96         IndexWriter indexCreator = new IndexWriter(index,new StandardAnalyzer(),true);
97         indexCreator.close();
98       }
99
100       IndexUtil.unindexEntity((EntityContent) entity,index);
101
102       indexWriter = new IndexWriter(index, new StandardAnalyzer(), false);
103       Document theDoc =  new Document();
104
105       // Keyword is stored and indexed, but not tokenized
106       // Text is tokenized,stored, indexed
107       // Unindexed is not tokenized or indexed, only stored
108       // Unstored is tokenized and indexed, but not stored
109       
110       //this initialization should go somewhere global like an xml file....
111
112       (new KeywordSearchTerm("id","","id","","id")).index(theDoc,entity);
113       
114       (new KeywordSearchTerm("webdb_create_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted")).index(theDoc,entity);
115       
116       (new UnIndexedSearchTerm("","","","where","where")).indexValue(theDoc,entity.getValue("publish_path")+entity.getValue("id")+".shtml");
117
118       (new TextSearchTerm("creator","search_creator","creator","creator","creator")).index(theDoc,entity);
119       (new TextSearchTerm("title","search_title","title","title","title")).index(theDoc,entity);
120       (new UnIndexedSearchTerm("description","search_content","description","description","description")).index(theDoc,entity);
121       (new UnIndexedSearchTerm("webdb_create","search_irrelevant","creationDate","creationDate","creationDate")).index(theDoc,entity);
122
123       (new ContentSearchTerm("content_data","search_content","content","","")).indexValue(theDoc,
124                                                                                      entity.getValue("content_data")+ " "
125                                                                                      + entity.getValue("description")+ " "
126                                                                                      + entity.getValue("title")
127                                                                                      );
128
129       (new TopicSearchTerm()).index(theDoc,entity);
130
131       (new ImagesSearchTerm()).index(theDoc,entity);
132       
133       (new AudioSearchTerm()).index(theDoc,entity);
134       
135       (new VideoSearchTerm()).index(theDoc,entity);
136
137
138       //comments-just aggregate all relevant fields
139       //removed until i get a chance to do this right
140
141       //String commentsAggregate = "";
142       //TemplateModel comments=entity.get("to_comments");
143       //if (comments != null){
144       // while (((TemplateListModel)comments).hasNext()){
145       //    TemplateModel aComment = ((TemplateListModel)comments).next();
146       //    commentsAggregate = commentsAggregate + " " + ((TemplateHashModel)aComment).get("title").toString()
147       //     + " " + ((TemplateHashModel)aComment).get("creator").toString()
148       //      + " " + ((TemplateHashModel)aComment).get("text").toString();
149       //  }
150       //}
151       //theDoc.add(Field.UnStored("comments",commentsAggregate));
152
153       indexWriter.addDocument(theDoc);
154
155
156     }
157     catch (Throwable t) {
158       aLogger.error("Error while indexing content: " + t.getMessage());
159       t.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(aLogger, LoggerWrapper.DEBUG_MESSAGE)));
160     }
161     finally {
162       if (indexWriter != null){
163         try{
164           indexWriter.close();
165         }
166         catch (Throwable t) {
167           aLogger.warn("Error while closing indexWriter: " + t.getMessage());
168         }
169
170       }
171       try{
172         FSDirectory theIndexDir=FSDirectory.getDirectory(index,false);
173         if (IndexReader.isLocked(theIndexDir)){
174           IndexReader.unlock(theIndexDir);
175         }
176       }
177       catch (Throwable t) {
178         aLogger.warn("Error while unlocking index: " + t.getMessage());
179       }
180     }
181
182
183
184
185     endTime = System.currentTimeMillis();
186
187     aLogger.info("  IndexTime: " + (endTime-startTime) + " ms<br>");
188   }
189 }
190
191
192
193
194
195
196
197
198
199