KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > search > SearchProxy


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.IOException JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.lucene.analysis.Analyzer;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.IndexWriter;
24 import org.apache.lucene.queryParser.ParseException;
25 import org.apache.lucene.search.IndexSearcher;
26 import org.apache.lucene.search.Searcher;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.store.FSDirectory;
29
30 /**
31  * 信息搜索的代理类,通过该类可以获取对日记以及评论信息的搜索,索引的删除增加等功能
32  * 两个子类:LogSearchProxy;ReplySearchProxy
33  *
34  * @author Liudong
35  */

36 public abstract class SearchProxy {
37
38     public final static String JavaDoc SEGMENTS = "segments";
39     /**
40      * 获取日记搜索代理
41      * @return
42      */

43     public static SearchProxy getLogQuery() {
44         return new LogSearchProxy();
45     }
46
47     /**
48      * 获取评论搜索代理
49      * @return
50      */

51     public static SearchProxy getReplyQuery() {
52         return new ReplySearchProxy();
53     }
54
55     /**
56      * 搜索信息
57      * @param word 要搜索的关键字
58      * @param from 结果集合的起点
59      * @param count 取结果数(当该值小于零时取全部信息)
60      * @return
61      */

62     public abstract List JavaDoc searchFor(int site,int catid,String JavaDoc word, int from, int count) throws IOException JavaDoc, ParseException;
63
64     /**
65      * 增加某个对象的索引
66      * @param obj
67      * @return
68      * @throws IOException
69      */

70     public abstract int addIndex(Object JavaDoc obj) throws IOException JavaDoc;
71     /**
72      * 删除指定编号的信息的索引
73      * @param id
74      * @return
75      */

76     public abstract int deleteIndex(int[] id) throws IOException JavaDoc ;
77
78     /**
79      * 更新索引信息
80      * @param obj
81      * @return
82      */

83     public abstract int updateIndex(Object JavaDoc obj) throws IOException JavaDoc;
84     /**
85      * 获取一个索引的Writer
86      * @return
87      */

88     public abstract IndexWriter getWriter() throws IOException JavaDoc;
89     /**
90      * 获取一个搜索的助理
91      * @param idxPath
92      * @return
93      * @throws IOException
94      */

95     protected Searcher getSearcher(String JavaDoc idxPath) throws IOException JavaDoc {
96         Searcher searcher = null;
97         //Acquire a lock -- analyzer is a convenient object to do this on.
98
synchronized (getAnalyzer()) {
99             Directory searchDirectory = FSDirectory.getDirectory(idxPath, false);
100             IndexReader reader = IndexReader.open(searchDirectory);
101             searcher = new IndexSearcher(reader);
102         }
103         return searcher;
104     }
105
106     /**
107      * 获取日记索引所在的目录
108      * @return
109      */

110     protected String JavaDoc getLogIndexPath() {
111         return SearchEnginePlugIn.getLogIndexPath();
112     }
113
114     /**
115      * 获取评论索引所在的目录
116      * @return
117      */

118     protected String JavaDoc getReplyIndexPath() {
119         return SearchEnginePlugIn.getReplyIndexPath();
120     }
121
122     /**
123      * 得到查询分析器
124      * @return
125      */

126     protected Analyzer getAnalyzer() {
127         return SearchEnginePlugIn.getAnalyzer();
128     }
129
130 }
131
Popular Tags