Example usage for org.apache.lucene.index IndexWriter close

List of usage examples for org.apache.lucene.index IndexWriter close

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes all open resources and releases the write lock.

Usage

From source file:com.work.IndexFiles.java

License:Apache License

/** Index all text files under a directory. */
public static void main(String[] args) {

    // English//  w w  w.  jav a  2  s  .c  o m
    //  String indexPath = "C:/Users/Harish/Desktop/IR/Data/Data English/masc_500k_texts/written/letters/index/one/";
    //  String docsPath = "C:/Users/Harish/Desktop/IR/Data/Data English/masc_500k_texts/written/letters/files/";
    //  Analyzer analyzer = new StandardAnalyzer();

    //Hindi
    String indexPath = "C:/Users/Harish/Desktop/IR/Data/Hindi Data/hin_corp_unicode/index/one/";
    String docsPath = "C:/Users/Harish/Desktop/IR/Data/Hindi Data/hin_corp_unicode/sample/";
    Analyzer analyzer = new HindiAnalyzer();

    //Chinese
    //  Analyzer analyzer = new CJKAnalyzer();

    boolean create = true;

    final Path docDir = Paths.get(docsPath);
    if (!Files.isReadable(docDir)) {
        System.out.println("Document directory '" + docDir.toAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(Paths.get(indexPath));

        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        if (create) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer.  But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here.  This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.wrmsr.search.dsl.SearchServiceImpl.java

License:Apache License

@Override
public synchronized void commit() throws IOException {
    checkState(this.indexWriter.isPresent());

    Lock lock = indexSearcherLock.writeLock();
    try {//from   w w w.  j  a  va 2  s.  c  o  m
        lock.lock();

        if (this.indexSearcher.isPresent()) {
            IndexSearcher indexSearcher = this.indexSearcher.get();
            indexSearcher.getIndexReader().close();
            this.indexSearcher = Optional.empty();
        }

        IndexWriter indexWriter = this.indexWriter.get();
        indexWriter.commit();
        indexWriter.close();

        IndexReader indexReader = IndexReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        this.indexSearcher = Optional.of(indexSearcher);
    } finally {
        lock.unlock();
    }
}

From source file:com.xpn.xwiki.plugin.lucene.IndexUpdaterTest.java

License:Open Source License

public void testLock() throws IOException {
    Directory directory;//w ww  .  ja v a  2 s .co m
    File f = new File(INDEXDIR);
    int indexingInterval;
    indexingInterval = 100;
    int maxQueueSize;
    maxQueueSize = 1000;

    if (!f.exists()) {
        f.mkdirs();
    }
    directory = FSDirectory.open(f);

    LucenePlugin plugin = new LucenePlugin("Monkey", "Monkey", getContext());

    final IndexUpdater indexUpdater = new TestIndexUpdater(directory, indexingInterval, maxQueueSize, plugin,
            getContext());

    plugin.init(indexUpdater, getContext());

    Thread permanentBlocker = new Thread(indexUpdater, "permanentBlocker");
    permanentBlocker.start();
    this.writeBlockerAcquiresLock.acquireUninterruptibly();

    assertTrue(IndexWriter.isLocked(indexUpdater.getDirectory()));

    final boolean[] doneCleaningIndex = { false };

    Thread indexCleaner = new Thread(new Runnable() {
        public void run() {
            indexUpdater.cleanIndex();

            doneCleaningIndex[0] = true;
        }
    }, "indexCleaner");

    indexCleaner.start();

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
    }

    assertFalse(doneCleaningIndex[0]);

    boolean wasActuallyLocked = false;

    try {
        if (!IndexWriter.isLocked(indexUpdater.getDirectory())) {
            new IndexWriter(indexUpdater.getDirectory(), new StandardAnalyzer(Version.LUCENE_29),
                    MaxFieldLength.LIMITED);
        } else {
            wasActuallyLocked = true;
        }
        // assert(IndexWriter.isLocked(indexUpdater.getDirectory()));
    } catch (LockObtainFailedException e) {
        /*
         * Strange, the isLocked method appears to be unreliable.
         */
        wasActuallyLocked = true;
    }

    assertTrue(wasActuallyLocked);

    this.writeBlockerWait.release();

    while (true) {
        try {
            indexCleaner.join();
            break;
        } catch (InterruptedException e) {
        }
    }

    assertTrue(doneCleaningIndex[0]);

    assertFalse(IndexWriter.isLocked(indexUpdater.getDirectory()));

    IndexWriter w = new IndexWriter(indexUpdater.getDirectory(), new StandardAnalyzer(Version.LUCENE_29),
            MaxFieldLength.LIMITED);
    w.close();
}

From source file:com.xpn.xwiki.plugin.lucene.internal.IndexRebuilder.java

License:Open Source License

public synchronized int startIndex(Collection<String> wikis, String hqlFilter, boolean clearIndex,
        boolean onlyNew, XWikiContext context) {
    if (this.rebuildInProgress) {
        LOGGER.warn("Cannot launch rebuild because another rebuild is in progress");

        return LucenePluginApi.REBUILD_IN_PROGRESS;
    } else {/*from w  w  w  .j a va  2s. co  m*/
        if (clearIndex) {
            if (wikis == null) {
                this.indexUpdater.cleanIndex();
            } else {
                try {
                    IndexWriter writer = this.indexUpdater.openWriter(false);
                    try {
                        for (String wiki : wikis) {
                            writer.deleteDocuments(new Term(IndexFields.DOCUMENT_WIKI, wiki));
                        }
                    } finally {
                        writer.close();
                    }
                } catch (IOException ex) {
                    LOGGER.warn("Failed to clean wiki index: {}", ex.getMessage());
                }
            }
        }

        this.wikis = wikis != null ? new ArrayList<String>(wikis) : null;
        this.hqlFilter = hqlFilter;
        this.onlyNew = onlyNew;
        this.rebuildInProgress = true;

        Thread indexRebuilderThread = new Thread(this, "Lucene Index Rebuilder");
        // The JVM should be allowed to shutdown while this thread is running
        indexRebuilderThread.setDaemon(true);
        // Client requests are more important than indexing
        indexRebuilderThread.setPriority(3);
        // Finally, start the rebuild in the background
        indexRebuilderThread.start();

        // Too bad that now we can't tell how many items are there to be indexed...
        return 0;
    }
}

From source file:com.xpn.xwiki.plugin.lucene.internal.IndexUpdater.java

License:Open Source License

/**
 * Polls the queue for documents to be indexed.
 *///  w  w  w  .j  a va2 s. co  m
private void updateIndex() {
    if (this.queue.isEmpty()) {
        LOGGER.debug("IndexUpdater: queue empty, nothing to do");
    } else {
        LOGGER.debug("IndexUpdater: documents in queue, start indexing");

        XWikiContext context = getContext();
        context.getWiki().getStore().cleanUp(context);

        IndexWriter writer;
        RETRY: while (true) {
            // We will retry after repairing if the index was
            // corrupt
            try {
                try {
                    writer = openWriter(false);
                    break RETRY;
                } catch (CorruptIndexException e) {
                    this.plugin.handleCorruptIndex(context);
                }
            } catch (IOException e) {
                LOGGER.error("Failed to open index", e);

                throw new RuntimeException(e);
            }
        }

        try {
            int nb = 0;
            while (!this.queue.isEmpty()) {
                AbstractIndexData data = this.queue.remove();

                try {
                    if (data.isDeleted()) {
                        removeFromIndex(writer, data, context);
                    } else {
                        addToIndex(writer, data, context);
                    }

                    ++nb;
                } catch (Throwable e) {
                    LOGGER.error("error indexing document [{}]", data, e);
                }
            }

            LOGGER.info("indexed [{}] docs to lucene index", nb);
        } catch (Exception e) {
            LOGGER.error("error indexing documents", e);
        } finally {
            try {
                context.getWiki().getStore().cleanUp(context);
            } catch (Exception e) {
                LOGGER.error("Failed to cleanup hibernate session in lucene index updater.", e);
            }

            try {
                writer.close();
            } catch (IOException e) {
                LOGGER.error("Failed to close writer.", e);
            }
        }

        this.plugin.openIndexReaders(context);
    }
}

From source file:com.xpn.xwiki.plugin.lucene.internal.IndexUpdater.java

License:Open Source License

/**
 * @return the number of documents in Lucene index writer.
 *//*from  w  ww  .  ja  v a  2 s .  com*/
public long getLuceneDocCount() {
    int n = -1;

    try {
        IndexWriter w = openWriter(false);
        try {
            n = w.numDocs();
        } finally {
            w.close();
        }
    } catch (IOException e) {
        LOGGER.error("Failed to get the number of documents in Lucene index writer", e);
    }

    return n;
}

From source file:com.xx.platform.web.listener.WebApplicationContextListener.java

/**
 * /*from w ww  . j  av  a 2s . co  m*/
 * @throws Exception
 */
public static void optimize() throws Exception {
    IndexWriter iw = null;
    File file = new File(SearchContext.search_dir + File.separator + "index");
    Directory directory = FSDirectory.getDirectory(file, false);
    IndexDeletionPolicy policy = new KeepLastIndexDeletionPolicy();
    iw = new IndexWriter(directory, new NutchDocumentAnalyzer(), false, policy, MaxFieldLength.UNLIMITED);
    iw.setUseCompoundFile(true);
    iw.setTermIndexInterval(128);
    iw.optimize(false);
    iw.close();
}

From source file:com.yangxu.searchengine.index.IndexFiles.java

License:Apache License

/**
 * //from   w  ww  .ja v  a  2 s.c o m
 * @param createOrUpdate
 *             create update
 */
public void createIndex(boolean createOrUpdate) {
    if (docsPath == null) {
        System.err.println("docsPath not exists!");
        System.exit(1);
    }

    final File docDir = new File(docsPath);
    if (!docDir.exists() || !docDir.canRead()) {
        System.out.println("Document directory '" + docDir.getAbsolutePath()
                + "' does not exist or is not readable, please check the path");
        System.exit(1);
    }

    Date start = new Date();
    try {
        System.out.println("Indexing to directory '" + indexPath + "'...");

        Directory dir = FSDirectory.open(new File(indexPath));
        Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_31);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);

        if (createOrUpdate) {
            // Create a new index in the directory, removing any
            // previously indexed documents:
            iwc.setOpenMode(OpenMode.CREATE);
        } else {
            // Add new documents to an existing index:
            iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        }

        // Optional: for better indexing performance, if you
        // are indexing many documents, increase the RAM
        // buffer. But if you do this, increase the max heap
        // size to the JVM (eg add -Xmx512m or -Xmx1g):
        //
        // iwc.setRAMBufferSizeMB(256.0);

        IndexWriter writer = new IndexWriter(dir, iwc);
        indexDocs(writer, docDir);

        // NOTE: if you want to maximize search performance,
        // you can optionally call forceMerge here. This can be
        // a terribly costly operation, so generally it's only
        // worth it when your index is relatively static (ie
        // you're done adding documents to it):
        //
        // writer.forceMerge(1);

        writer.close();

        Date end = new Date();
        System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
        System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
    }
}

From source file:com.Yasna.forum.database.DbSearchIndexer.java

License:Open Source License

public void addToIndex(ForumMessage message) {
    //acquire the index lock so that no other indexing operations
    //are performed.
    synchronized (indexLock) {
        IndexWriter writer = null;
        try {/*  w  w w  . j ava 2s  .c  o m*/
            writer = getWriter(false);
            addMessageToIndex(writer, message.getID(), message.getUnfilteredSubject(),
                    message.getUnfilteredBody(), message.getUser().getID(), message.getForumThread().getID(),
                    message.getForumThread().getForum().getID(), message.getCreationDate());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.Yasna.forum.database.DbSearchIndexer.java

License:Open Source License

/**
 * Rebuilds the search index from scratch. It deletes the entire index
 * and word tables and then indexes every message up to the end time.
 *//*from  w  w w. j a va  2  s. c o m*/
protected final void rebuildIndex(long end) {
    System.err.println("Rebuilding index...");

    IndexWriter writer = null;
    Connection con = null;
    try {
        writer = getWriter(true);
        con = DbConnectionManager.getConnection();
        PreparedStatement pstmt = con.prepareStatement(MESSAGES_BEFORE_DATE);
        pstmt.setString(1, Long.toString(end));
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            int messageID = rs.getInt(1);
            int userID = rs.getInt(2);
            int threadID = rs.getInt(3);
            int forumID = rs.getInt(4);
            String subject = rs.getString(5);
            String body = rs.getString(6);
            java.util.Date creationDate = new java.util.Date(Long.parseLong(rs.getString(7).trim()));
            //ForumMessage message = new DbForumMessage(messageID, factory);// factory.getMessage(messageID);
            addMessageToIndex(writer, messageID, subject, body, userID, threadID, forumID, creationDate);
        }
        pstmt.close();
    } catch (Exception sqle) {
        sqle.printStackTrace();
    } finally {
        try {
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            //A rebuild of the index warrants calling optimize.
            writer.optimize();
        } catch (Exception e) {
        }
        try {
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    System.err.println("Done rebuilding index.");
}