KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > search > LogSearchProxy


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 import java.util.StringTokenizer JavaDoc;
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 /**
40  * 日记信息搜索
41  *
42  * @author Liudong
43  */

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

53     public List JavaDoc searchFor(int site, int catid, String JavaDoc word, int from, int count)
54             throws IOException JavaDoc, ParseException {
55         List JavaDoc logs = new ArrayList JavaDoc();
56         Searcher searcher = getSearcher(getLogIndexPath());
57         if (searcher == null) return logs;
58
59         BooleanQuery comboQuery = new BooleanQuery();
60         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(word);
61         while(st.hasMoreElements()){
62             String JavaDoc 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         // Don't return more search results than the maximum number allowed.
76
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 JavaDoc(((Document) hits.doc(i)).get("logId")));
81         }
82         return logs;
83     }
84     /**
85      * 增加索引
86      */

87     public int addIndex(Object JavaDoc obj) throws IOException JavaDoc {
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     /*
109      * (non-Javadoc)
110      * @see jdlog.search.SearchProxy#deleteIndex(int[])
111      */

112     public int deleteIndex(int[] id) throws IOException JavaDoc {
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 JavaDoc e) {}
124             }
125         } finally {
126             try {
127                 reader.close();
128             } catch (Exception JavaDoc e) {}
129         }
130         return dc;
131     }
132
133     /*
134      * (non-Javadoc)
135      * @see jdlog.search.SearchProxy#updateIndex(java.lang.Object)
136      */

137     public int updateIndex(Object JavaDoc obj) throws IOException JavaDoc {
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     /* (non-Javadoc)
146      * @see jdlog.search.SearchProxy#getWriter()
147      */

148     public IndexWriter getWriter() throws IOException JavaDoc{
149         String JavaDoc logPath = getLogIndexPath();
150         File JavaDoc rp = new File JavaDoc(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 JavaDoc e) {
158                 return null;
159             }
160             wc++;
161         }
162         File JavaDoc segments = new File JavaDoc(logPath + File.separator + SEGMENTS);
163         boolean bCreate = !segments.exists();
164         return new IndexWriter(logPath,getAnalyzer(),bCreate);
165     }
166 }
167
Popular Tags