KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > index > LuceneIndexer


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/LuceneIndexer.java,v 1.2 2004/07/11 10:12:50 unico Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/11 10:12:50 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24
25 package org.apache.slide.index;
26
27 import java.io.FileReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
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 /**
50  * Not intended for production.
51  * Bug: running in test mode everything works fine,
52  * in slide context no index is deleted when deleting resources.
53  *
54  */

55 public class LuceneIndexer
56 {
57     private static final String JavaDoc DOC_ID = "documentId";
58     
59     
60     private String JavaDoc indexDb;
61     
62     
63     public LuceneIndexer (String JavaDoc indexDb)
64     {
65         this.indexDb = indexDb;
66     }
67     
68     
69     /**
70      * removes an index for a docId
71      * TODO: works in testmode (running Main), deletes nothing in slide context
72      *
73      * @param docId a String
74      *
75      * @throws IOException
76      *
77      */

78     public void removeIndex (String JavaDoc docId) throws IOException JavaDoc
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     /**
90      * Method createIndex
91      *
92      * @param docId a String
93      *
94      * @throws IOException
95      * @throws Exception
96      *
97      */

98     public void index (String JavaDoc docId, Reader JavaDoc reader)
99         throws Exception JavaDoc
100     {
101         IndexWriter writer =
102             new IndexWriter(indexDb, new StandardAnalyzer(), false);
103         
104 // reader = new FileReader (docId);
105

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 JavaDoc getReader (String JavaDoc file) throws IOException JavaDoc
116     {
117         FileReader JavaDoc reader = new FileReader JavaDoc (file);
118         return reader;
119     }
120     
121     private Set JavaDoc contains(String JavaDoc stringToFind) throws Exception JavaDoc
122     {
123         Set JavaDoc resultSet = new HashSet JavaDoc();
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 JavaDoc docId = doc.get(DOC_ID);
136             resultSet.add(docId);
137         }
138         searcher.close();
139         return resultSet;
140     }
141     
142     
143     /**
144      * Test. To run, adopt INDEX_DB and put two text files in current directory,
145      * otto.txt and fritz.txt containing the strings
146      * "hallo otto" and "hallo fritz"
147      *
148      * @param args a String[]
149      *
150      * @throws Exception
151      *
152      */

153     public static void main(String JavaDoc[] args) throws Exception JavaDoc
154     {
155         String JavaDoc 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 JavaDoc 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 JavaDoc result)
192     {
193         for (Iterator JavaDoc iter = result.iterator(); iter.hasNext();)
194         {
195             String JavaDoc element = (String JavaDoc) iter.next();
196             System.out.println(element);
197         }
198         
199     }
200 }
201
Popular Tags