KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > search > ReplySearchProxy


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.search;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
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 /**
40  * 评论信息搜索代理
41  * @author Liudong
42  */

43 public class ReplySearchProxy extends SearchProxy {
44     
45     public ReplySearchProxy() {
46         
47     }
48     /* (non-Javadoc)
49      * @see jdlog.search.SearchProxy#searchFor(java.lang.String, int, int)
50      */

51     public List JavaDoc searchFor(int site,int catid,String JavaDoc word, int from, int count) throws IOException JavaDoc, ParseException{
52         List JavaDoc replies = new ArrayList JavaDoc();
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         // Don't return more search results than the maximum number allowed.
67
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 JavaDoc(((Document) hits.doc(i)).get("replyId")));
72         }
73         return replies;
74     }
75     /* (non-Javadoc)
76      * @see jdlog.search.SearchProxy#deleteIndex(int[])
77      */

78     public int deleteIndex(int[] id) throws IOException JavaDoc {
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 JavaDoc e) {}
90             }
91         } finally {
92             try {
93                 reader.close();
94             } catch (Exception JavaDoc e) {}
95         }
96         return dc;
97     }
98     /* (non-Javadoc)
99      * @see jdlog.search.SearchProxy#updateIndex(java.lang.Object)
100      */

101     public int updateIndex(Object JavaDoc obj) throws IOException JavaDoc {
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     /* (non-Javadoc)
110      * @see jdlog.search.SearchProxy#addIndex(java.lang.Object)
111      */

112     public int addIndex(Object JavaDoc obj) throws IOException JavaDoc {
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     /* (non-Javadoc)
134      * @see jdlog.search.SearchProxy#getWriter()
135      */

136     public IndexWriter getWriter() throws IOException JavaDoc {
137         String JavaDoc replyPath = getReplyIndexPath();
138         File JavaDoc rp = new File JavaDoc(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 JavaDoc e) {
146                 return null;
147             }
148             wc++;
149         }
150         File JavaDoc segments = new File JavaDoc(replyPath + File.separator + SEGMENTS);
151         boolean bCreate = !segments.exists();
152         return new IndexWriter(replyPath,getAnalyzer(),bCreate);
153     }
154 }
155
Popular Tags