View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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  		//Term deleteTerm = new Term(DOC_ID, docId);
73  		//long docNum2 = reader.docFreq(deleteTerm);
74  		//reader.delete(deleteTerm);
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  		// reader = new FileReader (docId);
94  
95  		Document doc = new Document();
96  		doc.add(Field.Text("contents", reader));
97  		//Field field = new Field(DOC_ID, docId, true, true, true);
98  		//doc.add(Field.Text(DOC_ID, docId));
99  		doc.add(Field.UnIndexed(DOC_ID, docId));
100 		// doc.add(Field.Text("charset", "Windows-31J"));
101 		writer.addDocument(doc);
102 		writer.optimize();
103 		writer.close();
104 	}
105 }