⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbsearchindexer.java

📁 Jive 是一个系统工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    protected final void addMessageToIndex(ForumMessage message, IndexWriter writer)            throws IOException    {        if (writer == null) {            return;        }        String subject = message.getUnfilteredSubject();        if (subject == null) {            subject = "";        }        String body = message.getUnfilteredBody();        if (body == null) {            body = "";        }        String messageID = Integer.toString(message.getID());        String userID = Integer.toString(message.getUser().getID());        String threadID = null;        String forumID = null;        //It's possible that this message that we're trying to index        //hasn't been assigned a thread or forum yet. In that case, we        //want to skip indexing it.        try {            threadID = Integer.toString(                message.getForumThread().getID()            );            forumID = Integer.toString(                message.getForumThread().getForum().getID()            );        }        catch (Exception e) {            return;        }        String creationDate = DateField.dateToString(message.getCreationDate());        Document doc = new Document();        doc.add(Field.Keyword("messageID",messageID));        doc.add(new Field("userID", userID, false, true, false));        doc.add(new Field("threadID", threadID, false, true, false));        doc.add(new Field("forumID", forumID, false, true, false));        doc.add(Field.UnStored("subject", subject));        doc.add(Field.UnStored("body", body));        doc.add(new Field("creationDate", creationDate, false, true, false));        writer.addDocument(doc);    }    /**     * Deletes a message from the index.     */    protected final void deleteMessagesFromIndex(int [] messages) throws IOException {        if (messages == null) {            return;        }        IndexReader reader = getReader();        if (reader == null) {            //Reader will be null if the search index doesn't exist.            return;        }        Term messageIDTerm;        for (int i=0; i<messages.length; i++) {            messageIDTerm = new Term("messageID", Integer.toString(messages[i]));            try {                reader.delete(messageIDTerm);            }            catch (Exception e) { }        }        try {            reader.close();        }        catch (Exception e) { }    }    /**     * Rebuilds the search index from scratch. It deletes the entire index     * and word tables and then indexes every message up to the end time.     */    protected final void rebuildIndex(long end) {        System.err.println("Rebuilding index...");        int [] messages = null;        Connection con = null;        PreparedStatement pstmt = null;        try {            con = DbConnectionManager.getConnection();            //Now, find all messages that need to inserted            pstmt = con.prepareStatement(MESSAGES_BEFORE_DATE_COUNT);            pstmt.setString(1, Long.toString(end));            ResultSet rs = pstmt.executeQuery();            rs.next();            int messageCount = rs.getInt(1);            pstmt.close();            messages = new int[messageCount];            pstmt = con.prepareStatement(MESSAGES_BEFORE_DATE);            pstmt.setString(1, Long.toString(end));            rs = pstmt.executeQuery();            for (int i=0; i<messages.length; i++) {                rs.next();                messages[i] = rs.getInt("messageID");            }            pstmt.close();        }        catch( SQLException sqle ) {            System.err.println("Error in DbSearchIndexer:updateIndex()-" + sqle);            sqle.printStackTrace();        }        finally {            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        IndexWriter writer = null;        try {            writer = getWriter(true);            //Finally, index all messages;            int i=0;            try {                for ( ; i<messages.length; i++) {                    ForumMessage message = factory.getMessage(messages[i]);                    addMessageToIndex(message, writer);                }            }            catch( ForumMessageNotFoundException fmnfe) {                System.err.println("Index rebuilding failed: message " +                    messages[i] + " could not be loaded.");                fmnfe.printStackTrace();            }            //A rebuild of the index warrants calling optimize.            writer.optimize();        }        catch(Exception e) {            e.printStackTrace();        }        finally{            try { writer.close(); }            catch (Exception e) {                e.printStackTrace();            }        }        System.err.println("Done rebuilding index.");    }    /**     * Updates the index. It first deletes any messages in the index between     * the start and end times, and then adds all messages to the index that     * are between the start and end times.     */    protected final void updateIndex(long start, long end) {        Connection con = null;        PreparedStatement pstmt = null;        IndexWriter writer = null;        int [] messages = null;        try {            con = DbConnectionManager.getConnection();            //For a clean update, we need to make sure that we first delete            //any index entries that were made since we last updated. This            //might happen if a process was calling indexMessage() between runs            //of this method. For this reason, the two types of indexing (manual            //and automatic) should not be intermixed. However, we still perform            //this deletion to be safe.            pstmt = con.prepareStatement(MESSAGES_SINCE_DATE_COUNT);            pstmt.setString(1, Long.toString(start));            pstmt.setString(2, Long.toString(end));            ResultSet rs = pstmt.executeQuery();            rs.next();            int messageCount = rs.getInt(1);            messages = new int[messageCount];            pstmt.close();            pstmt = con.prepareStatement(MESSAGES_SINCE_DATE);            pstmt.setString(1, Long.toString(start));            pstmt.setString(2, Long.toString(end));            rs = pstmt.executeQuery();            for (int i=0; i<messages.length; i++) {                rs.next();                messages[i] = rs.getInt("messageID");            }        }        catch (Exception e) {            e.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        try {            deleteMessagesFromIndex(messages);            //Finally, index all new messages;            writer = getWriter(false);            for (int i=0; i<messages.length; i++) {                ForumMessage message = factory.getMessage(messages[i]);                addMessageToIndex(message, writer);            }        }        catch( Exception e ) {            e.printStackTrace();        }        finally {            try {  writer.close();  }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Returns a Lucene IndexWriter.     */    private static IndexWriter getWriter(boolean create) throws IOException {        if (indexPath == null) {            //Get path of where search index should be. It should be            //the search subdirectory of [jiveHome].            String jiveHome = PropertyManager.getProperty("jiveHome");            if (jiveHome == null) {                System.err.println("ERROR: the jiveHome property is not set.");                throw new IOException("Unable to open index for searching " +                        "because jiveHome was not set.");            }            indexPath =  jiveHome + File.separator + "search";        }        IndexWriter writer = null;        //If create is true, we always want to create a new index writer.        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();            }        }        //Otherwise, use an existing index if it exists.        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 index writer.");                    e.printStackTrace();                }            }        }        return writer;    }    /**     * Returns a Lucene IndexReader.     */    private static IndexReader getReader() throws IOException {        if (indexPath == null) {            //Get path of where search index should be. It should be            //the search subdirectory of [jiveHome].            String jiveHome = PropertyManager.getProperty("jiveHome");            if (jiveHome == null) {                System.err.println("ERROR: the jiveHome property is not set.");                throw new IOException("Unable to open index for searching " +                        "because jiveHome was not set.");            }            indexPath = jiveHome + File.separator + "search";        }        if (indexExists(indexPath)) {            IndexReader reader = IndexReader.open(indexPath);            return reader;        }        else {            return null;        }    }    /**     * Returns true if the search index exists at the specified path.     *     * @param indexPath the path to check for the search index at.     */    private static boolean indexExists(String indexPath) {        //Lucene always creates a file called "segments" -- if it exists, we        //assume that the search index exists.        File segments = new File(indexPath + File.separator + "segments");        return segments.exists();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -