KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > index > XincoIndexer


1 /**
2  *Copyright 2005 blueCubs.com
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, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoIndexer
23  *
24  * Description: handle document indexing
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004/10/31
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.index;
38
39 import java.util.Vector JavaDoc;
40 import org.apache.lucene.analysis.standard.StandardAnalyzer;
41 import org.apache.lucene.index.*;
42 import org.apache.lucene.search.*;
43 import org.apache.lucene.queryParser.QueryParser;
44 import org.apache.lucene.analysis.*;
45 import com.bluecubs.xinco.core.*;
46 import com.bluecubs.xinco.core.server.*;
47
48 /**
49  * This class handles document indexing for xinco.
50  * Edit configuration values in context.xml
51  */

52 public class XincoIndexer {
53
54     public static synchronized boolean indexXincoCoreData(XincoCoreData d, boolean index_content, XincoDBManager dbm) {
55         
56         IndexWriter writer = null;
57         
58         try {
59             
60             //check if document exists in index and delete
61
XincoIndexer.removeXincoCoreData(d, dbm);
62             
63             //add document to index
64
try {
65                 writer = new IndexWriter(dbm.config.FileIndexPath, new StandardAnalyzer(), false);
66             } catch (Exception JavaDoc ie) {
67                 writer = new IndexWriter(dbm.config.FileIndexPath, new StandardAnalyzer(), true);
68             }
69             writer.addDocument(XincoDocument.getXincoDocument(d, index_content, dbm));
70             //writer.optimize();
71
writer.close();
72             
73         } catch (Exception JavaDoc e) {
74             if (writer != null) {
75                 try {
76                     writer.close();
77                 } catch (Exception JavaDoc we) {}
78             }
79             return false;
80         }
81         
82         return true;
83     }
84     
85     public static synchronized boolean removeXincoCoreData(XincoCoreData d, XincoDBManager dbm) {
86         
87         IndexReader reader = null;
88         
89         //check if document exists in index and delete
90
try {
91             reader = IndexReader.open(dbm.config.FileIndexPath);
92             reader.delete(new Term("id", "" + d.getId()));
93             reader.close();
94         } catch (Exception JavaDoc re) {
95             if (reader != null) {
96                 try {
97                     reader.close();
98                 } catch (Exception JavaDoc re2) {}
99             }
100             return false;
101         }
102         
103         return true;
104     }
105     
106     public static synchronized boolean optimizeIndex(XincoDBManager dbm) {
107         
108         IndexWriter writer = null;
109         
110         try {
111             
112             //optimize index
113
writer = new IndexWriter(dbm.config.FileIndexPath, new StandardAnalyzer(), false);
114             writer.optimize();
115             writer.close();
116             
117         } catch (Exception JavaDoc e) {
118             if (writer != null) {
119                 try {
120                     writer.close();
121                 } catch (Exception JavaDoc we) {}
122             }
123             return false;
124         }
125         
126         return true;
127     }
128     
129     public static synchronized Vector JavaDoc findXincoCoreData(String JavaDoc s, int l, XincoDBManager dbm) {
130         
131         int i = 0;
132         Vector JavaDoc v = new Vector JavaDoc();
133         Searcher searcher = null;
134         
135         try {
136             
137             searcher = new IndexSearcher(dbm.config.FileIndexPath);
138             Analyzer analyzer = new StandardAnalyzer();
139             
140             //add language to query
141
if (l != 0) {
142                 s = s + " AND language:" + l;
143             }
144             Query query = QueryParser.parse(s, "designation", analyzer);
145             
146             Hits hits = searcher.search(query);
147
148             for (i=0;i<hits.length();i++) {
149                 try {
150                     v.addElement(new XincoCoreDataServer(Integer.parseInt(hits.doc(i).get("id")), dbm));
151                 } catch (Exception JavaDoc xcde) {
152                     // don't add non-existing data
153
}
154                 if (i >= dbm.config.MaxSearchResult) {
155                     break;
156                 }
157             }
158             
159             searcher.close();
160             
161         } catch (Exception JavaDoc e) {
162             if (searcher != null) {
163                 try {
164                     searcher.close();
165                 } catch (Exception JavaDoc se) {}
166             }
167             return null;
168         }
169         
170         return v;
171     }
172     
173     //private constructor to avoid instance generation with new-operator!
174
private XincoIndexer() {
175     }
176     
177 }
178
Popular Tags