1 16 package dlog4j.search; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.lucene.document.DateField; 25 import org.apache.lucene.document.Document; 26 import org.apache.lucene.document.Field; 27 import org.apache.lucene.index.IndexReader; 28 import org.apache.lucene.index.IndexWriter; 29 import org.apache.lucene.index.Term; 30 import org.apache.lucene.queryParser.ParseException; 31 import org.apache.lucene.queryParser.QueryParser; 32 import org.apache.lucene.search.BooleanQuery; 33 import org.apache.lucene.search.Hits; 34 import org.apache.lucene.search.Query; 35 import org.apache.lucene.search.Searcher; 36 37 import dlog4j.formbean.LogForm; 38 39 44 public class LogSearchProxy extends SearchProxy { 45 46 public LogSearchProxy() { 47 } 48 49 53 public List searchFor(int site, int catid, String word, int from, int count) 54 throws IOException , ParseException { 55 List logs = new ArrayList (); 56 Searcher searcher = getSearcher(getLogIndexPath()); 57 if (searcher == null) return logs; 58 59 BooleanQuery comboQuery = new BooleanQuery(); 60 StringTokenizer st = new StringTokenizer (word); 61 while(st.hasMoreElements()){ 62 String q = st.nextToken(); 63 Query subjectQuery = QueryParser.parse(q, "title", getAnalyzer()); 64 comboQuery.add(subjectQuery, false, false); 65 Query bodyQuery = QueryParser.parse(q, "content", getAnalyzer()); 66 comboQuery.add(bodyQuery, false, false); 67 } 68 MultiFilter multiFilter = new MultiFilter(1); 69 multiFilter.add(new FieldFilter("siteId",String.valueOf(site))); 70 if(catid>=0) 71 multiFilter.add(new FieldFilter("categoryId",String.valueOf(catid))); 72 73 Hits hits = searcher.search(comboQuery, multiFilter); 74 75 int numResults = hits.length(); 77 for (int i = 0; i < numResults; i++) { 78 if (count > 0 && logs.size() >= count) break; 79 if (i < from) continue; 80 logs.add(new Integer (((Document) hits.doc(i)).get("logId"))); 81 } 82 return logs; 83 } 84 87 public int addIndex(Object obj) throws IOException { 88 if(obj==null) 89 return 0; 90 LogForm log = (LogForm)obj; 91 Document doc = new Document(); 92 doc.add(Field.Keyword("logId", Integer.toString(log.getId()))); 93 doc.add(new Field("author", log.getOwnerName(),false,true,false)); 94 doc.add(new Field("siteId", Integer.toString(log.getSite().getId()),false,true,false)); 95 doc.add(new Field("categoryId", Integer.toString(log.getCategoryId()),false,true, false)); 96 doc.add(Field.UnStored("title", log.getTitle())); 97 doc.add(Field.UnStored("content", log.getContent())); 98 doc.add(new Field("logDate", DateField.dateToString(log.getLogTime()),false,true,false)); 99 IndexWriter writer = getWriter(); 100 try { 101 writer.addDocument(doc); 102 writer.optimize(); 103 }finally { 104 writer.close(); 105 } 106 return 1; 107 } 108 112 public int deleteIndex(int[] id) throws IOException { 113 IndexReader reader = IndexReader.open(getLogIndexPath()); 114 if (reader == null) 115 return 0; 116 int dc = 0; 117 try { 118 Term logIdTerm; 119 for (int i = 0; i < id.length; i++) { 120 logIdTerm = new Term("logId", Integer.toString(id[i])); 121 try { 122 dc += reader.delete(logIdTerm); 123 }catch (Exception e) {} 124 } 125 } finally { 126 try { 127 reader.close(); 128 } catch (Exception e) {} 129 } 130 return dc; 131 } 132 133 137 public int updateIndex(Object obj) throws IOException { 138 if(obj==null) 139 return 0; 140 LogForm log = (LogForm)obj; 141 int dc = deleteIndex(new int[] {log.getId()}); 142 addIndex(obj); 143 return dc; 144 } 145 148 public IndexWriter getWriter() throws IOException { 149 String logPath = getLogIndexPath(); 150 File rp = new File (logPath); 151 if(!rp.exists()) 152 rp.mkdirs(); 153 int wc = 0; 154 while(wc<10 && IndexReader.isLocked(logPath)){ 155 try { 156 Thread.sleep(100); 157 } catch (InterruptedException e) { 158 return null; 159 } 160 wc++; 161 } 162 File segments = new File (logPath + File.separator + SEGMENTS); 163 boolean bCreate = !segments.exists(); 164 return new IndexWriter(logPath,getAnalyzer(),bCreate); 165 } 166 } 167 | Popular Tags |