📄 dbsearchmanager.java
字号:
package net.ijsp.news.search.database;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company:ijsp.net </p> * @author ccjsmile * @version 1.0 */import org.apache.lucene.document.*;import org.apache.lucene.analysis.cn.*;import org.apache.lucene.analysis.*;import org.apache.lucene.analysis.standard.*;import org.apache.lucene.index.*;import org.apache.lucene.store.*;import java.io.*;import net.ijsp.news.search.SearchManager;import net.ijsp.news.news.News;import net.ijsp.news.database.LoadProp;public class DbSearchManager implements SearchManager { private boolean searchEnabled = true; private boolean busy = false; private boolean autoIndexEnabled = true; private static String indexPath = null; // 索引路径 protected static Analyzer analyzer = new ChineseAnalyzer(); // 中文检索 public boolean isSearchEnabled() { return searchEnabled; } public void setSearchEnabled(boolean searchEnabled) { this.searchEnabled = searchEnabled; } public boolean isBusy() { return busy; } public boolean isAutoIndexEnabled() { return autoIndexEnabled; } public synchronized void addToIndex(News news) { if (!searchEnabled) { return; } IndexWriter writer = null; try { writer = getWriter(false); addMessageToIndex(news.getNewsFile(),news.getTitle(),news.getContent(), Long.toString(news.getDate()),writer,news.getBoardID()); }catch (IOException ioe) { ioe.printStackTrace(); }finally{ try { writer.close(); }catch (Exception e) { } } } private static IndexWriter getWriter(boolean create) throws IOException { if (indexPath == null) { // 获取全文检索数据路径 LoadProp loadProp = new LoadProp(); loadProp = loadProp.prop(); indexPath = loadProp.getIndexPath(); } IndexWriter writer = null; if (create) { try { writer = new IndexWriter(indexPath, analyzer, true); }catch (Exception e) { System.err.println("ERROR: Failed to create a new index writer."); e.printStackTrace(); } } else { if (indexExists(indexPath)) { try { writer = new IndexWriter(indexPath, analyzer, false); }catch (Exception e) { System.err.println("ERROR: Failed to open an index writer."); e.printStackTrace(); } }else { try { writer = new IndexWriter(indexPath, analyzer, true); }catch (Exception e) { System.err.println("ERROR: Failed to create a new inex writer."); e.printStackTrace(); } } } return writer; } /** * 分析检索数据路径是否正确 */ private static boolean indexExists(String indexPath) { File segments = new File(indexPath + File.separator + "segments"); return segments.exists(); } /** * 将数据存入全文索引文件中 */ protected final void addMessageToIndex(String newsPath, String subject, String body, String creationDate, IndexWriter writer,int newsClass) throws IOException { if (writer == null) { return; } Document doc = new Document(); doc.add(Field.UnIndexed("newsPath",newsPath)); doc.add(Field.Text("subject", subject)); doc.add(Field.UnStored("body", body)); doc.add(new Field("creationDate",creationDate,false, true, false)); doc.add(new Field("newsClass", Integer.toString(newsClass), false, true, false)); writer.addDocument(doc); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -