Search in sources :

Example 1 with LuceneDAO

use of net.jforum.dao.LuceneDAO in project jforum2 by rafaelsteil.

the class LuceneReindexer method reindex.

private void reindex() {
    try {
        if (recreate) {
            this.settings.createIndexDirectory(SystemGlobals.getValue(ConfigKeys.LUCENE_INDEX_WRITE_PATH));
        }
    } catch (IOException e) {
        throw new ForumException(e);
    }
    LuceneDAO dao = DataAccessDriver.getInstance().newLuceneDAO();
    IndexSearcher searcher = null;
    LuceneSearch luceneSearch = ((LuceneManager) SearchFacade.manager()).luceneSearch();
    LuceneIndexer luceneIndexer = ((LuceneManager) SearchFacade.manager()).luceneIndexer();
    int fetchCount = SystemGlobals.getIntValue(ConfigKeys.LUCENE_INDEXER_DB_FETCH_COUNT);
    try {
        if (!recreate) {
            searcher = new IndexSearcher(this.settings.directory());
        }
        boolean hasMorePosts = true;
        long processStart = System.currentTimeMillis();
        int firstPostId = args.filterByMessage() ? args.getFirstPostId() : dao.firstPostIdByDate(args.getFromDate());
        if (args.filterByMessage()) {
            int dbFirstPostId = dao.firstPostId();
            if (firstPostId < dbFirstPostId) {
                firstPostId = dbFirstPostId;
            }
        }
        int lastPostId = args.filterByMessage() ? args.getLastPostId() : dao.lastPostIdByDate(args.getToDate());
        int counter = 1;
        int indexTotal = 0;
        long indexRangeStart = System.currentTimeMillis();
        while (hasMorePosts) {
            boolean contextFinished = false;
            int toPostId = firstPostId + fetchCount < lastPostId ? firstPostId + fetchCount : lastPostId;
            try {
                JForumExecutionContext ex = JForumExecutionContext.get();
                JForumExecutionContext.set(ex);
                List l = dao.getPostsToIndex(firstPostId, toPostId);
                if (counter >= 5000) {
                    long end = System.currentTimeMillis();
                    System.out.println("Indexed ~5000 documents in " + (end - indexRangeStart) + " ms (" + indexTotal + " so far)");
                    indexRangeStart = end;
                    counter = 0;
                }
                JForumExecutionContext.finish();
                contextFinished = true;
                for (Iterator iter = l.iterator(); iter.hasNext(); ) {
                    if ("0".equals(SystemGlobals.getValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING))) {
                        hasMorePosts = false;
                        break;
                    }
                    Post post = (Post) iter.next();
                    if (!recreate && args.avoidDuplicatedRecords()) {
                        if (luceneSearch.findDocumentByPostId(post.getId()) != null) {
                            continue;
                        }
                    }
                    luceneIndexer.batchCreate(post);
                    counter++;
                    indexTotal++;
                }
                firstPostId += fetchCount;
                hasMorePosts = hasMorePosts && l.size() > 0;
            } finally {
                if (!contextFinished) {
                    JForumExecutionContext.finish();
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("**** Total: " + (end - processStart) + " ms");
    } catch (IOException e) {
        throw new ForumException(e);
    } finally {
        SystemGlobals.setValue(ConfigKeys.LUCENE_CURRENTLY_INDEXING, "0");
        luceneIndexer.flushRAMDirectory();
        if (searcher != null) {
            try {
                searcher.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) JForumExecutionContext(net.jforum.JForumExecutionContext) Post(net.jforum.entities.Post) LuceneDAO(net.jforum.dao.LuceneDAO) IOException(java.io.IOException) IOException(java.io.IOException) ForumException(net.jforum.exceptions.ForumException) ForumException(net.jforum.exceptions.ForumException) Iterator(java.util.Iterator) List(java.util.List)

Aggregations

IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 JForumExecutionContext (net.jforum.JForumExecutionContext)1 LuceneDAO (net.jforum.dao.LuceneDAO)1 Post (net.jforum.entities.Post)1 ForumException (net.jforum.exceptions.ForumException)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1