1 23 24 25 package org.apache.slide.index; 26 27 import java.io.FileReader ; 28 import java.io.IOException ; 29 import java.io.Reader ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.Set ; 33 34 import org.apache.lucene.analysis.Analyzer; 35 import org.apache.lucene.analysis.standard.StandardAnalyzer; 36 import org.apache.lucene.document.Document; 37 import org.apache.lucene.document.Field; 38 import org.apache.lucene.index.IndexReader; 39 import org.apache.lucene.index.IndexWriter; 40 import org.apache.lucene.index.Term; 41 import org.apache.lucene.queryParser.QueryParser; 42 import org.apache.lucene.search.Hits; 43 import org.apache.lucene.search.IndexSearcher; 44 import org.apache.lucene.search.Query; 45 import org.apache.lucene.search.Searcher; 46 import org.apache.lucene.store.Directory; 47 import org.apache.lucene.store.FSDirectory; 48 49 55 public class LuceneIndexer 56 { 57 private static final String DOC_ID = "documentId"; 58 59 60 private String indexDb; 61 62 63 public LuceneIndexer (String indexDb) 64 { 65 this.indexDb = indexDb; 66 } 67 68 69 78 public void removeIndex (String docId) throws IOException 79 { 80 Directory directory = FSDirectory.getDirectory (indexDb, false); 81 IndexReader reader = IndexReader.open(directory); 82 83 Term deleteTerm = new Term(DOC_ID, docId); 84 reader.delete(deleteTerm); 85 reader.close(); 86 directory.close(); 87 } 88 89 98 public void index (String docId, Reader reader) 99 throws Exception 100 { 101 IndexWriter writer = 102 new IndexWriter(indexDb, new StandardAnalyzer(), false); 103 104 106 Document doc = new Document(); 107 doc.add (Field.Text ("contents", reader)); 108 Field field = new Field(DOC_ID, docId, true, true, true); 109 doc.add(field); 110 writer.addDocument(doc); 111 writer.optimize(); 112 writer.close(); 113 } 114 115 private static Reader getReader (String file) throws IOException 116 { 117 FileReader reader = new FileReader (file); 118 return reader; 119 } 120 121 private Set contains(String stringToFind) throws Exception 122 { 123 Set resultSet = new HashSet (); 124 Searcher searcher = new IndexSearcher (indexDb); 125 Analyzer analyzer = new StandardAnalyzer(); 126 127 Query query = QueryParser.parse(stringToFind, "contents", analyzer); 128 129 Hits hits = searcher.search(query); 130 131 int noOfHits = hits.length(); 132 for (int i = 0; i < noOfHits; i++) 133 { 134 Document doc = hits.doc(i); 135 String docId = doc.get(DOC_ID); 136 resultSet.add(docId); 137 } 138 searcher.close(); 139 return resultSet; 140 } 141 142 143 153 public static void main(String [] args) throws Exception 154 { 155 String INDEX_DB ="D:\\projects\\tmp\\index"; 156 157 LuceneIndexer indexer = new LuceneIndexer (INDEX_DB); 158 159 IndexWriter writer = 160 new IndexWriter(INDEX_DB, new StandardAnalyzer(), true); 161 162 writer.close(); 163 164 String ottoFile = "otto.txt"; 165 new LuceneIndexer (INDEX_DB).index (ottoFile, getReader(ottoFile)); 166 167 System.out.println("expect one element otto.txt"); 168 displayResult(indexer.contains("otto")); 169 170 new LuceneIndexer (INDEX_DB).index("fritz.txt", getReader("fritz.txt")); 171 System.out.println("expect fritz.txt"); 172 displayResult(indexer.contains("fritz")); 173 174 System.out.println("expect fritz.txt and otto.txt"); 175 displayResult(indexer.contains("Hallo")); 176 177 new LuceneIndexer (INDEX_DB).removeIndex ("otto.txt"); 178 179 System.out.println("expect fritz.txt"); 180 displayResult (indexer.contains("Hallo")); 181 182 System.out.println("expect null"); 183 displayResult (indexer.contains("otto")); 184 185 new LuceneIndexer (INDEX_DB).removeIndex ("fritz.txt"); 186 187 System.out.println("expect null"); 188 displayResult(indexer.contains("fritz")); 189 } 190 191 private static void displayResult(Set result) 192 { 193 for (Iterator iter = result.iterator(); iter.hasNext();) 194 { 195 String element = (String ) iter.next(); 196 System.out.println(element); 197 } 198 199 } 200 } 201 | Popular Tags |