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.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void saveBookmark(String id, String indexName) {
    try {/* w w w. j  a  v  a 2s.com*/
        IndexWriter mainIndexWriter = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));

        Document doc = new Document();
        doc.add(new Field("qcId", id, Store.YES, Index.NOT_ANALYZED));
        doc.add(new Field("qcIndexName", indexName, Store.YES, Index.NOT_ANALYZED));

        mainIndexWriter.updateDocument(new Term("id", id), doc);
        mainIndexWriter.prepareCommit();
        mainIndexWriter.commit();
        mainIndexWriter.close();
        mainIndexWriter = null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void deleteBookmark(String id, String indexName) {
    try {/*from w  ww  . jav  a 2  s. com*/
        IndexWriter mainIndexWriter = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        mainIndexWriter.deleteDocuments(new Term("qcId", id));
        mainIndexWriter.prepareCommit();
        mainIndexWriter.commit();
        mainIndexWriter.close();
        mainIndexWriter = null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void updateCard(CardSnapshot cardSnap, String title, String tags, String text) {
    try {/* w  w  w .  ja v  a 2s.c o  m*/
        IndexWriter writer = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + cardSnap.getIndexName())),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));

        Document doc = new Document();
        doc.add(new Field("title", title, Store.YES, Index.ANALYZED));
        doc.add(new Field("tags", tags, Store.YES, Index.ANALYZED));
        doc.add(new Field("text", text, Store.YES, Index.ANALYZED));
        doc.add(new Field("analyzedText", text, Store.YES, Index.ANALYZED));
        doc.add(new Field("indexName", cardSnap.getIndexName(), Store.YES, Index.ANALYZED));
        doc.add(new Field("id", cardSnap.getId(), Store.YES, Index.NOT_ANALYZED));

        writer.updateDocument(new Term("id", cardSnap.getId()), doc, new StandardAnalyzer(Version.LUCENE_35));
        writer.prepareCommit();
        writer.commit();
        writer.close();
        writer = null;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public static void main(String[] args) {
    try {/*from ww  w.ja  v  a 2 s . com*/
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + "null")),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        writer.deleteDocuments(new Term("indexName", "null"));
        writer.prepareCommit();
        writer.commit();
        writer.close();

    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Failed to Initialize", ex);
    }
}

From source file:com.gentics.cr.lucene.indexaccessor.IndexAccessorFactory.java

License:Apache License

private void createAccessor(final Directory dir, final Analyzer analyzer, final Query query,
        final Set<Sort> sortFields) throws IOException {
    IndexAccessor accessor = null;/*from  ww  w  .  j av a2 s  .  c om*/
    if (query != null) {
        accessor = new WarmingIndexAccessor(dir, analyzer, query);
    } else {
        accessor = new DefaultIndexAccessor(dir, analyzer);
    }
    accessor.open();

    if (dir.listAll().length == 0) {
        IndexWriter indexWriter = new IndexWriter(dir, null, true, IndexWriter.MaxFieldLength.UNLIMITED);
        indexWriter.close();
    }

    IndexAccessor existingAccessor = indexAccessors.putIfAbsent(dir, accessor);
    if (existingAccessor != null) {
        accessor.close();
        throw new IllegalStateException("IndexAccessor already exists: " + dir);
    }

}

From source file:com.gitblit.LuceneExecutor.java

License:Apache License

/**
 * Close the writer/searcher objects for a repository.
 * /*  w ww.  j a v  a 2  s. com*/
 * @param repositoryName
 */
public synchronized void close(String repositoryName) {
    try {
        IndexSearcher searcher = searchers.remove(repositoryName);
        if (searcher != null) {
            searcher.getIndexReader().close();
        }
    } catch (Exception e) {
        logger.error("Failed to close index searcher for " + repositoryName, e);
    }

    try {
        IndexWriter writer = writers.remove(repositoryName);
        if (writer != null) {
            writer.close();
        }
    } catch (Exception e) {
        logger.error("Failed to close index writer for " + repositoryName, e);
    }
}

From source file:com.github.alvanson.xltsearch.IndexTask.java

License:Apache License

@Override
protected Boolean call() {
    IndexWriter iwriter = null;
    boolean result = false;

    updateMessage("started");
    try {//w  ww.  j a v  a  2 s.c om
        int count = 0;
        Docket docket;

        IndexWriterConfig iwconfig = new IndexWriterConfig(config.getVersion(), config.getAnalyzer());
        iwconfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
        iwconfig.setSimilarity(config.getSimilarity());
        iwriter = new IndexWriter(config.getDirectory(), iwconfig);

        while ((docket = inQueue.take()) != Docket.DONE) {
            count++;
            updateMessage(docket.relPath);
            switch (docket.status) {
            case PARSED:
                // index parsed file
                Document doc = new Document();
                // store relative path  ** must be indexed for updateDocument
                doc.add(new StringField(config.pathField, docket.relPath, Field.Store.YES));
                // index content
                doc.add(new TextField(config.contentField, docket.content.toString(), Field.Store.NO));
                // index standard metadata
                for (Map.Entry<String, Property> e : config.metadataFields.entrySet()) {
                    for (String value : docket.metadata.getValues(e.getValue())) {
                        doc.add(new TextField(e.getKey(), value, Field.Store.YES));
                    }
                }
                // store hashsum
                doc.add(new StringField(config.hashSumField, docket.hashSum, Field.Store.YES));
                // add/update document
                iwriter.updateDocument(new Term(config.pathField, docket.relPath), doc);
                // fall through
            case PASS:
                break;
            case DELETE:
                iwriter.deleteDocuments(new Term(config.pathField, docket.relPath));
                break;
            default:
                logger.error("Unexpected docket state while processing {}: {}", docket.relPath,
                        docket.status.toString());
                cancel(true); // cancel task
            }
            updateProgress(count, count + docket.workLeft);
        }
        // end of queue
        updateMessage("complete");
        updateProgress(count, count + docket.workLeft);
        result = true;
    } catch (IOException ex) {
        updateMessage("I/O exception");
        logger.error("I/O exception while writing to index", ex);
    } catch (InterruptedException ex) {
        if (isCancelled()) {
            updateMessage("cancelled");
        } else {
            updateMessage("interrupted");
            logger.error("Interrupted", ex);
        }
    }
    // close iwriter
    if (iwriter != null) {
        try {
            iwriter.close();
        } catch (IOException ex) {
            logger.warn("I/O exception while closing index writer", ex);
        }
    }
    return result;
}

From source file:com.github.buzztaiki.lucene.lastuni.CJKSingleCharQueryTest.java

License:Apache License

private void addDoc(Directory dir, Analyzer analyzer, String content) throws IOException {
    IndexWriter writer = newWriter(dir, analyzer);
    try {/*from w ww.  j a va  2 s  .c  o  m*/
        addDoc(writer, content);
        writer.commit();
    } finally {
        writer.close();
    }
}

From source file:com.github.lucene.store.CreateJavaTestIndex.java

License:Apache License

public static void populate(final Directory directory, final Analyzer analyzer)
        throws IOException, ParseException {
    final String dataDir = new File("src").getAbsolutePath();
    final List<File> results = new ArrayList<File>();
    findFiles(results, new File(dataDir));

    final IndexWriterConfig config = TestUtils.getIndexWriterConfig(analyzer, openMode, useCompoundFile);
    final IndexWriter writer = new IndexWriter(directory, config);
    for (final File file : results) {
        final Document doc = getDocument(dataDir, file);
        writer.addDocument(doc);/*from w  w w .j  av  a 2  s .c  om*/
    }
    writer.close();
}

From source file:com.github.lucene.store.CreateTestIndex.java

License:Apache License

public static void populate(final Directory directory, final Analyzer analyzer)
        throws IOException, ParseException {
    final String dataDir = new File("target/test-classes/data").getAbsolutePath();
    final List<File> results = new ArrayList<File>();
    findFiles(results, new File(dataDir));

    final IndexWriterConfig config = TestUtils.getIndexWriterConfig(analyzer, openMode, useCompoundFile);
    final IndexWriter writer = new IndexWriter(directory, config);
    for (final File file : results) {
        final Document doc = getDocument(dataDir, file);
        writer.addDocument(doc);//  ww  w.ja v  a2 s. co  m
    }
    writer.close();
}