lucene實現搜索引擎的建立索引數據庫,處理搜索結果.
一,創建索引
?public boolean createIndex() throws IOException {
? //.......
??Directory fsDirectory = FSDirectory.getDirectory(Constant.INDEX_DIR, true);
??Analyzer analyzer =? new IK_CAnalyzer();//分析器
??IndexWriter indexWriter = new IndexWriter(fsDirectory, analyzer, true);
??for (int i = 0; i < htmls.length; i++) {
???String htmlPath = htmls[i].getAbsolutePath();
???if (htmlPath.endsWith(".html") || htmlPath.endsWith(".htm")) {
???//添加?Document
???addDocument(htmlPath, indexWriter);
???}
??}
??indexWriter.optimize();
??indexWriter.close();
?}
二 ,添加?Document
?public void addDocument(String htmlPath, IndexWriter indexWriter) {
??HTMLDocParser htmlParser = new HTMLDocParser(htmlPath);
??String path = htmlParser.getPath();
??String title = htmlParser.getTitle();
??Reader content = htmlParser.getContent();
??LineNumberReader reader = new LineNumberReader(content);
??String snap = "";
??try {
???snap = reader.readLine();
???String tp = reader.readLine();
???if (snap.length() < 30 & tp != null) {
????snap = snap + tp;
???}
??} catch (IOException e1) {
???e1.printStackTrace();
??}
??Document document = new Document();
??document.add(new Field("path", path, Field.Store.YES, Field.Index.NO));//文件路徑
??document.add(new Field("title", title, Field.Store.YES,Field.Index.TOKENIZED));//文件標題
??document.add(new Field("snap", snap, Field.Store.YES, Field.Index.NO));//快照
??document.add(new Field("content", content));//內容
??try {
???indexWriter.addDocument(document);
??} catch (IOException e) {
???e.printStackTrace();
??}
?}
三 ,顯示結果
public List search() {
??List searchResult = new ArrayList();
??if (false == indexManager.ifIndexExist()) {
???try {
????if (false == indexManager.createIndex()) {
?????return searchResult;
????}
???} catch (IOException e) {
????e.printStackTrace();
????return searchResult;
???}
??}
??IndexSearcher indexSearcher = null;
??try {
???indexSearcher = new IndexSearcher(indexManager.getIndexDir());
??} catch (IOException ioe) {
???ioe.printStackTrace();
??}
??QueryParser queryParser = new QueryParser("content", analyzer);
??Query query = null;
??try {
???query = queryParser.parse(searchWord);
??} catch (ParseException e) {
???e.printStackTrace();
??}
??if (null != query && null != indexSearcher) {
???try {
????Hits hits = indexSearcher.search(query);
????for (int i = 0; i < hits.length(); i++) {
?????SearchResultBean resultBean = new SearchResultBean();
?????resultBean.setHtmlPath(hits.doc(i).get("path"));
?????resultBean.setHtmlTitle(hits.doc(i).get("title"));
?????resultBean.setHtmlSnap(hits.doc(i).get("snap"));
?????searchResult.add(resultBean);
????}
???} catch (IOException e) {
????e.printStackTrace();
???}
??}
??return searchResult;
?}
?
myeclipse4.2 weblogic 9? 驗證通過
部署方法
下載 lucene-test.rar
修改web.xml啟動參數dataDir,indexDir
?
jwebee
我的個人網站
posted on 2007-05-28 15:32
周行 閱讀(2300)
評論(0) 編輯 收藏 所屬分類:
IT技術