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.IOException;
019: import java.util.List;
020:
021: import org.apache.lucene.analysis.Analyzer;
022: import org.apache.lucene.index.IndexReader;
023: import org.apache.lucene.index.IndexWriter;
024: import org.apache.lucene.queryParser.ParseException;
025: import org.apache.lucene.search.IndexSearcher;
026: import org.apache.lucene.search.Searcher;
027: import org.apache.lucene.store.Directory;
028: import org.apache.lucene.store.FSDirectory;
029:
030: /**
031: * 信息搜索的代理类,通过该类可以获取对日记以及评论信息的搜索,索引的删除增加等功能
032: * 两个子类:LogSearchProxy;ReplySearchProxy
033: *
034: * @author Liudong
035: */
036: public abstract class SearchProxy {
037:
038: public final static String SEGMENTS = "segments";
039:
040: /**
041: * 获取日记搜索代理
042: * @return
043: */
044: public static SearchProxy getLogQuery() {
045: return new LogSearchProxy();
046: }
047:
048: /**
049: * 获取评论搜索代理
050: * @return
051: */
052: public static SearchProxy getReplyQuery() {
053: return new ReplySearchProxy();
054: }
055:
056: /**
057: * 搜索信息
058: * @param word 要搜索的关键字
059: * @param from 结果集合的起点
060: * @param count 取结果数(当该值小于零时取全部信息)
061: * @return
062: */
063: public abstract List searchFor(int site, int catid, String word,
064: int from, int count) throws IOException, ParseException;
065:
066: /**
067: * 增加某个对象的索引
068: * @param obj
069: * @return
070: * @throws IOException
071: */
072: public abstract int addIndex(Object obj) throws IOException;
073:
074: /**
075: * 删除指定编号的信息的索引
076: * @param id
077: * @return
078: */
079: public abstract int deleteIndex(int[] id) throws IOException;
080:
081: /**
082: * 更新索引信息
083: * @param obj
084: * @return
085: */
086: public abstract int updateIndex(Object obj) throws IOException;
087:
088: /**
089: * 获取一个索引的Writer
090: * @return
091: */
092: public abstract IndexWriter getWriter() throws IOException;
093:
094: /**
095: * 获取一个搜索的助理
096: * @param idxPath
097: * @return
098: * @throws IOException
099: */
100: protected Searcher getSearcher(String idxPath) throws IOException {
101: Searcher searcher = null;
102: //Acquire a lock -- analyzer is a convenient object to do this on.
103: synchronized (getAnalyzer()) {
104: Directory searchDirectory = FSDirectory.getDirectory(
105: idxPath, false);
106: IndexReader reader = IndexReader.open(searchDirectory);
107: searcher = new IndexSearcher(reader);
108: }
109: return searcher;
110: }
111:
112: /**
113: * 获取日记索引所在的目录
114: * @return
115: */
116: protected String getLogIndexPath() {
117: return SearchEnginePlugIn.getLogIndexPath();
118: }
119:
120: /**
121: * 获取评论索引所在的目录
122: * @return
123: */
124: protected String getReplyIndexPath() {
125: return SearchEnginePlugIn.getReplyIndexPath();
126: }
127:
128: /**
129: * 得到查询分析器
130: * @return
131: */
132: protected Analyzer getAnalyzer() {
133: return SearchEnginePlugIn.getAnalyzer();
134: }
135:
136: }
|