1 26 27 29 package de.nava.informa.search; 30 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.ArrayList ; 34 35 import org.apache.lucene.analysis.Analyzer; 36 import org.apache.lucene.analysis.standard.StandardAnalyzer; 37 import org.apache.lucene.index.IndexWriter; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import de.nava.informa.core.ItemIF; 42 import de.nava.informa.core.ChannelIF; 43 44 50 public final class ChannelIndexer { 51 52 private static Log logger = LogFactory.getLog(ChannelIndexer.class); 53 54 private String indexDir; 55 private int nrOfIndexedItems; 56 private Analyzer analyzer; 57 58 66 public ChannelIndexer(String indexDir) { 67 this.indexDir = indexDir; 68 this.nrOfIndexedItems = 0; 69 this.analyzer = new StandardAnalyzer(); 70 } 71 72 79 public void indexChannels(boolean createNewIndex, Collection channels) 80 throws java.io.IOException { 81 82 Collection items = new ArrayList (); 83 Iterator itC = channels.iterator(); 84 while (itC.hasNext()) { 85 ChannelIF channel = (ChannelIF) itC.next(); 86 if (logger.isDebugEnabled()) { 87 logger.debug("Searching channel " + channel + " for items."); 88 } 89 items.addAll(channel.getItems()); 90 } 91 if (!items.isEmpty()) { 92 indexItems(createNewIndex, items); 93 } else { 94 logger.info("No items found for indexing."); 95 } 96 } 97 98 105 public void indexItems(boolean createNewIndex, Collection items) 106 throws java.io.IOException { 107 108 logger.info("Start writing index."); 109 IndexWriter writer = new IndexWriter(indexDir, analyzer, createNewIndex); 110 Iterator itI = items.iterator(); 111 while (itI.hasNext()) { 112 ItemIF item = (ItemIF) itI.next(); 113 if (logger.isDebugEnabled()) { 114 logger.debug("Add item " + item + " to index."); 115 } 116 writer.addDocument(ItemDocument.makeDocument(item)); 117 } 118 writer.optimize(); 119 nrOfIndexedItems = writer.docCount(); 120 writer.close(); 121 logger.info("Finished writing index."); 122 } 123 124 131 public int getNrOfIndexedItems() { 132 return nrOfIndexedItems; 133 } 134 135 public void setIndexDir(String indexDir) { 136 this.indexDir = indexDir; 137 } 138 139 public String getIndexDir() { 140 return indexDir; 141 } 142 143 } 144 | Popular Tags |