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.barchart.feed.ddf.resolver.provider.ResolverDDF.java

License:BSD License

private void delete() throws Exception {

    final IndexWriterConfig config = new IndexWriterConfig(ConstResolver.VERSION, analyzer);

    final IndexWriter writer = new IndexWriter(getDirectory(), config);

    writer.deleteAll();/*from w w  w  .  j a  v  a2 s.  c  o m*/

    writer.close();

}

From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java

License:BSD License

void setStatus(final Status status) throws Exception {

    final Document doc = Status.encode(status);

    final IndexWriterConfig config = new IndexWriterConfig(ConstResolver.VERSION, analyzer);

    final IndexWriter writer = new IndexWriter(getDirectory(), config);

    writer.updateDocument(Status.TERM, doc);

    writer.close();

}

From source file:com.basistech.lucene.tools.LuceneQueryToolTest.java

License:Apache License

@BeforeClass
public static void oneTimeSetup() throws IOException, ParseException {
    LuceneQueryToolTest.showOutput = false; // for debugging tests
    Directory dir = new RAMDirectory();
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    IndexWriter writer = new IndexWriter(dir, config);
    Document doc = new Document();
    doc.add(new Field("longest-mention", "Bill Clinton", StringField.TYPE_STORED));
    doc.add(new Field("context", "Hillary Clinton Arkansas", TextField.TYPE_NOT_STORED));
    writer.addDocument(doc);/*from  ww w.  j  ava 2s. c o  m*/
    doc = new Document();
    doc.add(new Field("longest-mention", "George W. Bush", StringField.TYPE_STORED));
    doc.add(new Field("context", "Texas Laura Bush", TextField.TYPE_NOT_STORED));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new Field("longest-mention", "George H. W. Bush", StringField.TYPE_STORED));
    doc.add(new Field("context", "Barbara Bush Texas", TextField.TYPE_NOT_STORED));
    writer.addDocument(doc);
    doc = new Document();
    doc.add(new Field("bbb", "foo", StringField.TYPE_STORED));
    doc.add(new Field("bbb", "bar", StringField.TYPE_STORED));
    doc.add(new Field("aaa", "foo", StringField.TYPE_STORED));
    FieldType typeUnindexed = new FieldType(StringField.TYPE_STORED);
    typeUnindexed.setIndexOptions(IndexOptions.NONE);
    doc.add(new Field("zzz", "foo", typeUnindexed));
    writer.addDocument(doc);
    writer.close();
    reader = DirectoryReader.open(dir);
}

From source file:com.basistech.lucene.tools.LuceneQueryToolTest.java

License:Apache License

@Test
public void testBinaryField() throws IOException, ParseException {
    Directory dir = new RAMDirectory();
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    IndexWriter writer = new IndexWriter(dir, config);
    Document doc = new Document();
    doc.add(new Field("id", "1", StringField.TYPE_STORED));
    doc.add(new Field("binary-field", "ABC".getBytes(Charsets.UTF_8), StoredField.TYPE));
    writer.addDocument(doc);/*from  ww  w.  ja va  2s.  c om*/
    writer.close();
    reader = DirectoryReader.open(dir);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(bytes);
    LuceneQueryTool lqt = new LuceneQueryTool(reader, out);
    lqt.run(new String[] { "id:1" });
    String result = Joiner.on('\n').join(getOutput(bytes));
    assertTrue(result.contains("0x414243")); // binary rep of "ABC"
}

From source file:com.bdaum.zoom.lal.internal.LireActivator.java

License:Open Source License

public void releaseIndexWriter(File indexPath, boolean force) throws CorruptIndexException, IOException {
    synchronized (indexWriterMap) {
        IndexWriterEntry entry = indexWriterMap.get(indexPath);
        if (entry != null) {
            if (--entry.count <= 0 || force) {
                try {
                    IndexWriter writer = entry.writer;
                    @SuppressWarnings("resource")
                    Directory directory = writer.getDirectory();
                    writer.close();
                    directory.deleteFile("write.lock"); //$NON-NLS-1$
                    directory.close();//from  w  w w  .  ja  va2s .  c  o  m
                } finally {
                    indexWriterMap.remove(indexPath);
                }
            }
        }
    }
}

From source file:com.berico.clavin.index.IndexDirectoryBuilder.java

License:Apache License

/**
 * Turns a GeoNames gazetteer file into a Lucene index, and adds
 * some supplementary gazetteer records at the end.
 * /*ww  w .  j a  v a2  s. c  o m*/
 * @param args            not used
 * @throws IOException
 */
public static void main(String[] args) throws IOException {

    logger.info("Indexing... please wait.");

    // Create a new index file on disk, allowing Lucene to choose
    // the best FSDirectory implementation given the environment.
    // TODO: delete this directory first, if it exists
    FSDirectory index = FSDirectory.open(new File("./IndexDirectory"));

    // indexing by lower-casing & tokenizing on whitespace
    Analyzer indexAnalyzer = new WhitespaceLowerCaseAnalyzer();

    // create the object that will actually build the Lucene index
    IndexWriter indexWriter = new IndexWriter(index, new IndexWriterConfig(Version.LUCENE_40, indexAnalyzer));

    // open the gazetteer files to be loaded
    BufferedReader r = new BufferedReader(
            new InputStreamReader(new FileInputStream(new File(pathToGazetteer)), "UTF-8"));
    BufferedReader r2 = new BufferedReader(new InputStreamReader(
            new FileInputStream(new File("./src/main/resources/SupplementaryGazetteer.txt")), "UTF-8"));

    String line;

    // let's see how long this takes...
    Date start = new Date();

    // load GeoNames gazetteer into Lucene index
    while ((line = r.readLine()) != null)
        addToIndex(indexWriter, line);

    // add supplementary gazetteer records to index
    while ((line = r2.readLine()) != null)
        addToIndex(indexWriter, line);

    // that wasn't so long, was it?
    Date stop = new Date();

    logger.info("[DONE]");
    logger.info(indexWriter.maxDoc() + " geonames added to index.");
    logger.info("Merging indices... please wait.");

    indexWriter.close();
    index.close();
    r.close();
    r2.close();

    logger.info("[DONE]");

    DateFormat df = new SimpleDateFormat("HH:mm:ss");
    long elapsed_MILLIS = stop.getTime() - start.getTime();
    logger.info("Process started: " + df.format(start) + ", ended: " + df.format(stop) + "; elapsed time: "
            + MILLISECONDS.toSeconds(elapsed_MILLIS) + " seconds.");
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void createEntity(SEntity src) {
    if (src.getId().length() == 0)
        return;//from   w w w . j  ava  2s . com
    if (src.getKind().length() == 0)
        return;

    try {
        if (!src.getKind().equals(KIND_QUOTA)) {
            if (!quotaCreate(src))
                return;
        }
        backup(src);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        Document doc = new Document();
        write(src, doc);
        writer.addDocument(doc);
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void updateEntity(SEntity src) {
    if (src.getId().length() == 0)
        return;//ww w  .j  a  va  2 s  .co m
    if (src.getKind().length() == 0)
        return;

    try {
        if (!src.getKind().equals(KIND_QUOTA)) {
            if (!quotaUpdate(src))
                return;
        }
        backup(src);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        Document doc = new Document();
        write(src, doc);
        writer.updateDocument(new Term(SEntity.ID, src.getId()), doc);
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

protected void deleteEntity(String id) {
    if (id.length() == 0)
        return;//from  w  w  w .j  a  v  a 2 s  . c  om
    String kind = "";

    try {
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs td = searcher.search(new TermQuery(new Term(SEntity.ID, id)), 1);
        if (td.totalHits > 0) {
            Document doc = searcher.doc(td.scoreDocs[0].doc);
            kind = doc.get(SEntity.KIND);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    if (kind.length() == 0)
        return;
    if (!allowDelete(id, kind))
        return;

    try {
        if (!kind.equals(KIND_QUOTA)) {
            if (!quotaDelete(id, kind))
                return;
        }
        removeBackup(id, kind);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(dirIndex)), iwc);
        writer.deleteDocuments(new Term(SEntity.ID, id));
        writer.close();
    } catch (Exception e) {
    }
}

From source file:com.bitplan.pdfindex.Pdfindexer.java

License:Apache License

/**
 * close// w  w w  .  j a  va2s  . com
 * @throws Exception
 */
private void close() throws Exception {
    IndexWriter indexWriter = getIndexWriter();
    indexWriter.optimize();
    indexWriter.close();
}