001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.search;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.lucene.document.DateField;
025: import org.apache.lucene.document.Document;
026: import org.apache.lucene.document.Field;
027: import org.apache.lucene.index.IndexReader;
028: import org.apache.lucene.index.IndexWriter;
029: import org.apache.lucene.index.Term;
030: import org.apache.lucene.queryParser.ParseException;
031: import org.apache.lucene.queryParser.QueryParser;
032: import org.apache.lucene.search.Hits;
033: import org.apache.lucene.search.Query;
034: import org.apache.lucene.search.Searcher;
035:
036: import dlog4j.formbean.ReplyForm;
037:
038: /**
039: * 评论信息搜索代理
040: * @author Liudong
041: */
042: public class ReplySearchProxy extends SearchProxy {
043:
044: public ReplySearchProxy() {
045:
046: }
047:
048: /* (non-Javadoc)
049: * @see jdlog.search.SearchProxy#searchFor(java.lang.String, int, int)
050: */
051: public List searchFor(int site, int catid, String word, int from,
052: int count) throws IOException, ParseException {
053: List replies = new ArrayList();
054: Searcher searcher = getSearcher(getReplyIndexPath());
055: if (searcher == null)
056: return replies;
057:
058: word = StringUtils.deleteWhitespace(word);
059: Query bodyQuery = QueryParser.parse(word, "content",
060: getAnalyzer());
061:
062: MultiFilter multiFilter = new MultiFilter(1);
063: multiFilter
064: .add(new FieldFilter("siteId", String.valueOf(site)));
065: if (catid >= 0)
066: multiFilter.add(new FieldFilter("categoryId", String
067: .valueOf(catid)));
068:
069: Hits hits = searcher.search(bodyQuery, multiFilter);
070:
071: // Don't return more search results than the maximum number allowed.
072: int numResults = hits.length();
073: for (int i = 0; i < numResults; i++) {
074: if (count > 0 && replies.size() >= count)
075: break;
076: if (i < from)
077: continue;
078: replies.add(new Integer(((Document) hits.doc(i))
079: .get("replyId")));
080: }
081: return replies;
082: }
083:
084: /* (non-Javadoc)
085: * @see jdlog.search.SearchProxy#deleteIndex(int[])
086: */
087: public int deleteIndex(int[] id) throws IOException {
088: IndexReader reader = IndexReader.open(getReplyIndexPath());
089: if (reader == null)
090: return 0;
091: int dc = 0;
092: try {
093: Term logIdTerm;
094: for (int i = 0; i < id.length; i++) {
095: logIdTerm = new Term("replyId", Integer.toString(id[i]));
096: try {
097: dc += reader.delete(logIdTerm);
098: } catch (Exception e) {
099: }
100: }
101: } finally {
102: try {
103: reader.close();
104: } catch (Exception e) {
105: }
106: }
107: return dc;
108: }
109:
110: /* (non-Javadoc)
111: * @see jdlog.search.SearchProxy#updateIndex(java.lang.Object)
112: */
113: public int updateIndex(Object obj) throws IOException {
114: if (obj == null)
115: return 0;
116: ReplyForm reply = (ReplyForm) obj;
117: int dc = deleteIndex(new int[] { reply.getId() });
118: addIndex(obj);
119: return dc;
120: }
121:
122: /* (non-Javadoc)
123: * @see jdlog.search.SearchProxy#addIndex(java.lang.Object)
124: */
125: public int addIndex(Object obj) throws IOException {
126: if (obj == null)
127: return 0;
128: ReplyForm reply = (ReplyForm) obj;
129: Document doc = new Document();
130: doc.add(Field.Keyword("replyId", Integer
131: .toString(reply.getId())));
132: doc.add(Field.Keyword("logId", Integer.toString(reply
133: .getLogId())));
134: doc.add(new Field("categoryId", Integer.toString(reply.getLog()
135: .getCategoryId()), false, true, false));
136: doc.add(new Field("author", reply.getAuthorName(), false, true,
137: false));
138: doc.add(new Field("siteId", Integer.toString(reply.getSite()
139: .getId()), false, true, false));
140: doc.add(Field.UnStored("content", reply.getContent()));
141: doc.add(new Field("replyDate", DateField.dateToString(reply
142: .getWriteTime()), false, true, false));
143: IndexWriter writer = getWriter();
144: try {
145: writer.addDocument(doc);
146: writer.optimize();
147: } finally {
148: writer.close();
149: }
150: return 1;
151: }
152:
153: /* (non-Javadoc)
154: * @see jdlog.search.SearchProxy#getWriter()
155: */
156: public IndexWriter getWriter() throws IOException {
157: String replyPath = getReplyIndexPath();
158: File rp = new File(replyPath);
159: if (!rp.exists())
160: rp.mkdirs();
161: int wc = 0;
162: while (wc < 10 && IndexReader.isLocked(replyPath)) {
163: try {
164: Thread.sleep(100);
165: } catch (InterruptedException e) {
166: return null;
167: }
168: wc++;
169: }
170: File segments = new File(replyPath + File.separator + SEGMENTS);
171: boolean bCreate = !segments.exists();
172: return new IndexWriter(replyPath, getAnalyzer(), bCreate);
173: }
174: }
|