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: import java.util.StringTokenizer;
023:
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.BooleanQuery;
033: import org.apache.lucene.search.Hits;
034: import org.apache.lucene.search.Query;
035: import org.apache.lucene.search.Searcher;
036:
037: import dlog4j.formbean.LogForm;
038:
039: /**
040: * 日记信息搜索
041: *
042: * @author Liudong
043: */
044: public class LogSearchProxy extends SearchProxy {
045:
046: public LogSearchProxy() {
047: }
048:
049: /*
050: * (non-Javadoc)
051: * @see jdlog.search.SearchProxy#searchFor(java.lang.String, int, int)
052: */
053: public List searchFor(int site, int catid, String word, int from,
054: int count) throws IOException, ParseException {
055: List logs = new ArrayList();
056: Searcher searcher = getSearcher(getLogIndexPath());
057: if (searcher == null)
058: return logs;
059:
060: BooleanQuery comboQuery = new BooleanQuery();
061: StringTokenizer st = new StringTokenizer(word);
062: while (st.hasMoreElements()) {
063: String q = st.nextToken();
064: Query subjectQuery = QueryParser.parse(q, "title",
065: getAnalyzer());
066: comboQuery.add(subjectQuery, false, false);
067: Query bodyQuery = QueryParser.parse(q, "content",
068: getAnalyzer());
069: comboQuery.add(bodyQuery, false, false);
070: }
071: MultiFilter multiFilter = new MultiFilter(1);
072: multiFilter
073: .add(new FieldFilter("siteId", String.valueOf(site)));
074: if (catid >= 0)
075: multiFilter.add(new FieldFilter("categoryId", String
076: .valueOf(catid)));
077:
078: Hits hits = searcher.search(comboQuery, multiFilter);
079:
080: // Don't return more search results than the maximum number allowed.
081: int numResults = hits.length();
082: for (int i = 0; i < numResults; i++) {
083: if (count > 0 && logs.size() >= count)
084: break;
085: if (i < from)
086: continue;
087: logs
088: .add(new Integer(((Document) hits.doc(i))
089: .get("logId")));
090: }
091: return logs;
092: }
093:
094: /**
095: * 增加索引
096: */
097: public int addIndex(Object obj) throws IOException {
098: if (obj == null)
099: return 0;
100: LogForm log = (LogForm) obj;
101: Document doc = new Document();
102: doc.add(Field.Keyword("logId", Integer.toString(log.getId())));
103: doc.add(new Field("author", log.getOwnerName(), false, true,
104: false));
105: doc.add(new Field("siteId", Integer.toString(log.getSite()
106: .getId()), false, true, false));
107: doc.add(new Field("categoryId", Integer.toString(log
108: .getCategoryId()), false, true, false));
109: doc.add(Field.UnStored("title", log.getTitle()));
110: doc.add(Field.UnStored("content", log.getContent()));
111: doc.add(new Field("logDate", DateField.dateToString(log
112: .getLogTime()), false, true, false));
113: IndexWriter writer = getWriter();
114: try {
115: writer.addDocument(doc);
116: writer.optimize();
117: } finally {
118: writer.close();
119: }
120: return 1;
121: }
122:
123: /*
124: * (non-Javadoc)
125: * @see jdlog.search.SearchProxy#deleteIndex(int[])
126: */
127: public int deleteIndex(int[] id) throws IOException {
128: IndexReader reader = IndexReader.open(getLogIndexPath());
129: if (reader == null)
130: return 0;
131: int dc = 0;
132: try {
133: Term logIdTerm;
134: for (int i = 0; i < id.length; i++) {
135: logIdTerm = new Term("logId", Integer.toString(id[i]));
136: try {
137: dc += reader.delete(logIdTerm);
138: } catch (Exception e) {
139: }
140: }
141: } finally {
142: try {
143: reader.close();
144: } catch (Exception e) {
145: }
146: }
147: return dc;
148: }
149:
150: /*
151: * (non-Javadoc)
152: * @see jdlog.search.SearchProxy#updateIndex(java.lang.Object)
153: */
154: public int updateIndex(Object obj) throws IOException {
155: if (obj == null)
156: return 0;
157: LogForm log = (LogForm) obj;
158: int dc = deleteIndex(new int[] { log.getId() });
159: addIndex(obj);
160: return dc;
161: }
162:
163: /* (non-Javadoc)
164: * @see jdlog.search.SearchProxy#getWriter()
165: */
166: public IndexWriter getWriter() throws IOException {
167: String logPath = getLogIndexPath();
168: File rp = new File(logPath);
169: if (!rp.exists())
170: rp.mkdirs();
171: int wc = 0;
172: while (wc < 10 && IndexReader.isLocked(logPath)) {
173: try {
174: Thread.sleep(100);
175: } catch (InterruptedException e) {
176: return null;
177: }
178: wc++;
179: }
180: File segments = new File(logPath + File.separator + SEGMENTS);
181: boolean bCreate = !segments.exists();
182: return new IndexWriter(logPath, getAnalyzer(), bCreate);
183: }
184: }
|