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

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

Introduction

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

Prototype

public void forceMergeDeletes() throws IOException 

Source Link

Document

Forces merging of all segments that have deleted documents.

Usage

From source file:org.getopt.luke.Luke.java

License:Apache License

/**
 * Optimize the index./*from  www .  j a  v a  2  s . c  om*/
 */
public void optimize(final Object dialog) {
    Thread t = new Thread() {
        public void run() {
            IndexWriter iw = null;
            Object optimizeButton = find(dialog, "optimizeButton");
            setBoolean(optimizeButton, "enabled", false);
            Object closeButton = find(dialog, "closeButton");
            setBoolean(closeButton, "enabled", false);
            Object msg = find(dialog, "msg");
            Object stat = find(dialog, "stat");
            setString(stat, "text", "Running ...");
            PanelPrintWriter ppw = new PanelPrintWriter(Luke.this, msg);
            boolean useCompound = getBoolean(find(dialog, "optCompound"), "selected");
            boolean expunge = getBoolean(find(dialog, "optExpunge"), "selected");
            boolean keep = getBoolean(find(dialog, "optKeepAll"), "selected");
            boolean useLast = getBoolean(find(dialog, "optLastCommit"), "selected");
            Object tiiSpin = find(dialog, "tii");
            Object segnumSpin = find(dialog, "segnum");
            int tii = Integer.parseInt(getString(tiiSpin, "text"));
            int segnum = Integer.parseInt(getString(segnumSpin, "text"));
            try {
                if (is != null)
                    is = null;
                if (ir != null)
                    ir.close();
                if (ar != null)
                    ar.close();
                IndexDeletionPolicy policy;
                if (keep) {
                    policy = new KeepAllIndexDeletionPolicy();
                } else {
                    policy = new KeepLastIndexDeletionPolicy();
                }
                IndexWriterConfig cfg = new IndexWriterConfig(LV, new WhitespaceAnalyzer(LV));
                if (!useLast) {
                    IndexCommit ic = ((DirectoryReader) ir).getIndexCommit();
                    if (ic != null) {
                        cfg.setIndexCommit(ic);
                    }
                }
                cfg.setIndexDeletionPolicy(policy);
                cfg.setTermIndexInterval(tii);
                cfg.setUseCompoundFile(useCompound);
                cfg.setInfoStream(ppw);
                iw = new IndexWriter(dir, cfg);
                long startSize = Util.calcTotalFileSize(pName, dir);
                long startTime = System.currentTimeMillis();
                if (expunge) {
                    iw.forceMergeDeletes();
                } else {
                    if (segnum > 1) {
                        iw.forceMerge(segnum, true);
                    } else {
                        iw.forceMerge(1, true);
                    }
                }
                iw.commit();
                long endTime = System.currentTimeMillis();
                long endSize = Util.calcTotalFileSize(pName, dir);
                long deltaSize = startSize - endSize;
                String sign = deltaSize < 0 ? " Increased " : " Reduced ";
                String sizeMsg = sign + Util.normalizeSize(Math.abs(deltaSize))
                        + Util.normalizeUnit(Math.abs(deltaSize));
                String timeMsg = String.valueOf(endTime - startTime) + " ms";
                showStatus(sizeMsg + " in " + timeMsg);
                iw.close();
                setString(stat, "text", "Finished OK.");
            } catch (Exception e) {
                e.printStackTrace(ppw);
                setString(stat, "text", "ERROR - aborted.");
                errorMsg("ERROR optimizing: " + e.toString());
                if (iw != null)
                    try {
                        iw.close();
                    } catch (Exception e1) {
                    }
            } finally {
                setBoolean(closeButton, "enabled", true);
            }
            try {
                actionReopen();
                is = new IndexSearcher(ir);
                // add dialog again
                add(dialog);
            } catch (Exception e) {
                e.printStackTrace(ppw);
                errorMsg("ERROR reopening after optimize:\n" + e.getMessage());
            }
        }
    };
    t.start();
}