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 23 24 import org.apache.commons.lang.StringUtils; 25 import org.apache.lucene.document.DateField; 26 import org.apache.lucene.document.Document; 27 import org.apache.lucene.document.Field; 28 import org.apache.lucene.index.IndexReader; 29 import org.apache.lucene.index.IndexWriter; 30 import org.apache.lucene.index.Term; 31 import org.apache.lucene.queryParser.ParseException; 32 import org.apache.lucene.queryParser.QueryParser; 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.ReplyForm; 38 39 43 public class ReplySearchProxy extends SearchProxy { 44 45 public ReplySearchProxy() { 46 47 } 48 51 public List searchFor(int site,int catid,String word, int from, int count) throws IOException , ParseException{ 52 List replies = new ArrayList (); 53 Searcher searcher = getSearcher(getReplyIndexPath()); 54 if (searcher == null) return replies; 55 56 word = StringUtils.deleteWhitespace(word); 57 Query bodyQuery = QueryParser.parse(word, "content", getAnalyzer()); 58 59 MultiFilter multiFilter = new MultiFilter(1); 60 multiFilter.add(new FieldFilter("siteId",String.valueOf(site))); 61 if(catid>=0) 62 multiFilter.add(new FieldFilter("categoryId",String.valueOf(catid))); 63 64 Hits hits = searcher.search(bodyQuery, multiFilter); 65 66 int numResults = hits.length(); 68 for (int i = 0; i < numResults; i++) { 69 if (count > 0 && replies.size() >= count) break; 70 if (i < from) continue; 71 replies.add(new Integer (((Document) hits.doc(i)).get("replyId"))); 72 } 73 return replies; 74 } 75 78 public int deleteIndex(int[] id) throws IOException { 79 IndexReader reader = IndexReader.open(getReplyIndexPath()); 80 if (reader == null) 81 return 0; 82 int dc = 0; 83 try { 84 Term logIdTerm; 85 for (int i = 0; i < id.length; i++) { 86 logIdTerm = new Term("replyId", Integer.toString(id[i])); 87 try { 88 dc += reader.delete(logIdTerm); 89 }catch (Exception e) {} 90 } 91 } finally { 92 try { 93 reader.close(); 94 } catch (Exception e) {} 95 } 96 return dc; 97 } 98 101 public int updateIndex(Object obj) throws IOException { 102 if(obj==null) 103 return 0; 104 ReplyForm reply = (ReplyForm)obj; 105 int dc = deleteIndex(new int[] {reply.getId()}); 106 addIndex(obj); 107 return dc; 108 } 109 112 public int addIndex(Object obj) throws IOException { 113 if(obj==null) 114 return 0; 115 ReplyForm reply = (ReplyForm)obj; 116 Document doc = new Document(); 117 doc.add(Field.Keyword("replyId", Integer.toString(reply.getId()))); 118 doc.add(Field.Keyword("logId", Integer.toString(reply.getLogId()))); 119 doc.add(new Field("categoryId", Integer.toString(reply.getLog().getCategoryId()),false,true, false)); 120 doc.add(new Field("author", reply.getAuthorName(),false,true,false)); 121 doc.add(new Field("siteId", Integer.toString(reply.getSite().getId()),false,true,false)); 122 doc.add(Field.UnStored("content", reply.getContent())); 123 doc.add(new Field("replyDate", DateField.dateToString(reply.getWriteTime()),false,true,false)); 124 IndexWriter writer = getWriter(); 125 try { 126 writer.addDocument(doc); 127 writer.optimize(); 128 }finally { 129 writer.close(); 130 } 131 return 1; 132 } 133 136 public IndexWriter getWriter() throws IOException { 137 String replyPath = getReplyIndexPath(); 138 File rp = new File (replyPath); 139 if(!rp.exists()) 140 rp.mkdirs(); 141 int wc = 0; 142 while(wc<10 && IndexReader.isLocked(replyPath)){ 143 try { 144 Thread.sleep(100); 145 } catch (InterruptedException e) { 146 return null; 147 } 148 wc++; 149 } 150 File segments = new File (replyPath + File.separator + SEGMENTS); 151 boolean bCreate = !segments.exists(); 152 return new IndexWriter(replyPath,getAnalyzer(),bCreate); 153 } 154 } 155 | Popular Tags |