1 36 37 package com.bluecubs.xinco.index; 38 39 import java.util.Vector ; 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 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 XincoIndexer.removeXincoCoreData(d, dbm); 62 63 try { 65 writer = new IndexWriter(dbm.config.FileIndexPath, new StandardAnalyzer(), false); 66 } catch (Exception ie) { 67 writer = new IndexWriter(dbm.config.FileIndexPath, new StandardAnalyzer(), true); 68 } 69 writer.addDocument(XincoDocument.getXincoDocument(d, index_content, dbm)); 70 writer.close(); 72 73 } catch (Exception e) { 74 if (writer != null) { 75 try { 76 writer.close(); 77 } catch (Exception 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 try { 91 reader = IndexReader.open(dbm.config.FileIndexPath); 92 reader.delete(new Term("id", "" + d.getId())); 93 reader.close(); 94 } catch (Exception re) { 95 if (reader != null) { 96 try { 97 reader.close(); 98 } catch (Exception 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 writer = new IndexWriter(dbm.config.FileIndexPath, new StandardAnalyzer(), false); 114 writer.optimize(); 115 writer.close(); 116 117 } catch (Exception e) { 118 if (writer != null) { 119 try { 120 writer.close(); 121 } catch (Exception we) {} 122 } 123 return false; 124 } 125 126 return true; 127 } 128 129 public static synchronized Vector findXincoCoreData(String s, int l, XincoDBManager dbm) { 130 131 int i = 0; 132 Vector v = new Vector (); 133 Searcher searcher = null; 134 135 try { 136 137 searcher = new IndexSearcher(dbm.config.FileIndexPath); 138 Analyzer analyzer = new StandardAnalyzer(); 139 140 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 xcde) { 152 } 154 if (i >= dbm.config.MaxSearchResult) { 155 break; 156 } 157 } 158 159 searcher.close(); 160 161 } catch (Exception e) { 162 if (searcher != null) { 163 try { 164 searcher.close(); 165 } catch (Exception se) {} 166 } 167 return null; 168 } 169 170 return v; 171 } 172 173 private XincoIndexer() { 175 } 176 177 } 178 | Popular Tags |