1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package com.isenshi.util.extlib;
17  
18  import java.io.IOException;
19  import java.io.Reader;
20  
21  import org.apache.lucene.analysis.cjk.CJKAnalyzer;
22  import org.apache.lucene.document.Document;
23  import org.apache.lucene.document.Field;
24  import org.apache.lucene.index.IndexReader;
25  import org.apache.lucene.index.IndexWriter;
26  import org.apache.lucene.store.Directory;
27  import org.apache.lucene.store.FSDirectory;
28  
29  public class CJKLuceneIndexer {
30  
31  	private static final String DOC_ID = "documentId";
32  
33  	private String indexDb;
34  
35  	public CJKLuceneIndexer(String indexDb) {
36  		this.indexDb = indexDb;
37  	}
38  
39  	/***
40  	 * removes an index for a docId TODO: works in testmode (running Main),
41  	 * deletes nothing in slide context
42  	 * 
43  	 * @param docId
44  	 *            a String
45  	 * 
46  	 * @throws IOException
47  	 * 
48  	 */
49  	public void removeIndex(String docId) throws IOException {
50  		Directory directory = FSDirectory.getDirectory(indexDb, false);
51  		IndexReader reader = IndexReader.open(directory);
52  
53  		
54  		int docNum = reader.numDocs();
55  						
56  		try {
57  			for (int i = 0; i < docNum; i++) {
58  				if(reader.isDeleted(i)){
59  					continue;
60  				}
61  				Document doc = reader.document(i);
62  				String curDocId = doc.get(DOC_ID);
63  				if (doc != null && docId.equals(curDocId)) {
64  					reader.delete(i);
65  					break;
66  				}
67  			}
68  		} catch (Exception e) {
69  			e.printStackTrace();
70  		}
71  		
72  		
73  		
74  		
75  
76  		reader.close();
77  		directory.close();
78  	}
79  
80  	/***
81  	 * Method createIndex
82  	 * 
83  	 * @param docId
84  	 *            a String
85  	 * 
86  	 * @throws IOException
87  	 * @throws Exception
88  	 * 
89  	 */
90  	public void index(String docId, Reader reader) throws Exception {
91  		IndexWriter writer = new IndexWriter(indexDb, new CJKAnalyzer(), false);
92  
93  		
94  
95  		Document doc = new Document();
96  		doc.add(Field.Text("contents", reader));
97  		
98  		
99  		doc.add(Field.UnIndexed(DOC_ID, docId));
100 		
101 		writer.addDocument(doc);
102 		writer.optimize();
103 		writer.close();
104 	}
105 }