📄 indexbuilder.java
字号:
/* * XP Forum * * Copyright (c) 2002-2003 RedSoft Group. All rights reserved. * */package org.redsoft.forum.search;import java.util.Collection;import java.util.Iterator;import java.sql.SQLException;import java.io.IOException;import org.apache.lucene.document.Document;import org.apache.lucene.analysis.cn.ChineseAnalyzer;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.document.Field;import org.redsoft.forum.ForumConstants;import org.redsoft.forum.exception.DAOException;import org.redsoft.forum.dao.Thread;import org.redsoft.forum.dao.DAOFactory;import org.redsoft.forum.dao.ThreadDAO;import org.redsoft.forum.fixture.MysqlFixture;/** * Build index for all threads in db * If index is not built before, get all threads in db and build the index * If index has been built, append newly posted threads to index * * @author cinc * @version $Id: IndexBuilder.java,v 1.2 2004/02/26 03:05:37 mustang Exp $ */public class IndexBuilder{ /** * Index directory */ private String indexDir; /** * Constructor * * @param indexDir the index directory */ public IndexBuilder( String indexDir ){ System.out.println ("IndexBuilder Initialzed ok."); this.indexDir = indexDir; } /** * Check if index is built before */ private boolean isIndexExist(){ return IndexReader.indexExists( indexDir ); } /** * Adding Thread to index * @throws IOException */ public synchronized void buildIndex() throws IOException, SQLException, DAOException { ThreadDAO dao = DAOFactory.getInstance().getThreadDAO(); IndexWriter writer; Collection threads; if ( isIndexExist() ) { //System.out.println( "Index already existed." ); writer = new IndexWriter( indexDir, new ChineseAnalyzer(), false ); threads = dao.findThreadsPostedLaterThan( lastedModified() ); } else { System.out.println( "Building index for threads from scratch..." ); writer = new IndexWriter( indexDir, new ChineseAnalyzer(), true ); threads = dao.findThreadsPostedLaterThan( 0 ); } appendThreadsToIndex(threads, writer); writer.optimize(); writer.close(); } private void appendThreadsToIndex(Collection threads, IndexWriter writer) throws IOException { if (threads.size() == 0){ System.out.println ("No newly posted threads, doing nothing."); return; } Iterator iterator = threads.iterator(); while( iterator.hasNext() ){ Thread thread = (Thread)iterator.next(); System.out.println ( "Adding article "+ thread.getTitle() + " to index" ); writer.addDocument( createDocument(thread) ); } } /** * Get the time index is last modified */ public long lastedModified() throws IOException{ return IndexReader.lastModified( indexDir ); } /** * create a document for a thread */ private Document createDocument(Thread thread){ Document doc = new Document(); doc.add(Field.Text("title", thread.getTitle())); doc.add(Field.Text("content", thread.getContent())); doc.add(Field.Text("author", thread.getAuthor())); doc.add(Field.Text("timestamp", thread.getTimeStamp() + "") ); String category = DAOFactory.getInstance().getForumDAO().getForum(thread.getCategory()).getName(); doc.add(Field.Text("category", category)); doc.add(Field.Text("categoryID", "" + thread.getCategory())); long parentID = thread.getId(); if (thread.getParent_id() != -1){ parentID = thread.getParent_id(); } doc.add(Field.Text("parentID", "" + parentID)); return doc; } /** * Program entry point * * @param args [0] the directory index is to be built */ public static void main( String args[] ){ String indexDir = args[0] + ForumConstants.INDEX_DIR; initDAOFactory(); try{ IndexBuilder builder = new IndexBuilder( indexDir ); builder.buildIndex(); }catch (SQLException sqle){ sqle.printStackTrace(); }catch (IOException ioe){ ioe.printStackTrace(); } catch (DAOException daoe) { daoe.printStackTrace(); } } private static void initDAOFactory() { try { new MysqlFixture().setUp(); DAOFactory.getInstance().buildForumDAO("web-inf/forums.xml"); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use Options | File Templates. } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -