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.knowgate.lucene.NewsMessageIndexer.java

License:Open Source License

public static void addOrReplaceNewsMessage(Properties oProps, String sGuid, String sThread, String sWorkArea,
        String sContainer, String sTitle, String sAuthor, Date dtCreated, String sText)
        throws ClassNotFoundException, IOException, IllegalArgumentException, NoSuchFieldException,
        IllegalAccessException, InstantiationException, NullPointerException {

    String sDirectory = oProps.getProperty("luceneindex");

    if (null == sDirectory) {
        if (DebugFile.trace)
            DebugFile.decIdent();//from ww w .j  av  a 2 s . c  o  m
        throw new NoSuchFieldException("Cannot find luceneindex property");
    }

    sDirectory = Gadgets.chomp(sDirectory, File.separator) + "k_newsmsgs" + File.separator + sWorkArea;

    if (DebugFile.trace)
        DebugFile.writeln("index directory is " + sDirectory);

    File oDir = new File(sDirectory);
    boolean bNewIndex = !oDir.exists();
    if (bNewIndex) {
        FileSystem oFs = new FileSystem();
        try {
            oFs.mkdirs("file://" + sDirectory);
        } catch (Exception xcpt) {
            if (DebugFile.trace)
                DebugFile.writeln(xcpt.getClass() + " " + xcpt.getMessage());
            throw new FileNotFoundException(
                    "Could not create directory " + sDirectory + " " + xcpt.getMessage());
        }
    } else {
        File[] aFiles = oDir.listFiles();
        if (aFiles == null) {
            bNewIndex = true;
        } else if (aFiles.length == 0) {
            bNewIndex = true;
        }
    }

    Directory oFsDir = Indexer.openDirectory(sDirectory);

    if (!bNewIndex) {
        if (DebugFile.trace)
            DebugFile.writeln("new IndexReader(...)");
        IndexReader oIRdr = IndexReader.open(oFsDir, false);
        if (DebugFile.trace)
            DebugFile.writeln("IndexReader.deleteDocuments(" + sGuid + ")");
        oIRdr.deleteDocuments(new Term("guid", sGuid));
        oIRdr.close();
    }

    if (DebugFile.trace)
        DebugFile.writeln("new IndexWriter(...)");

    IndexWriter oIWrt = new IndexWriter(oFsDir, Indexer.instantiateAnalyzer(oProps),
            IndexWriter.MaxFieldLength.LIMITED);

    addNewsMessage(oIWrt, sGuid, sThread, sWorkArea, sContainer, sTitle, sAuthor, dtCreated, sText);

    oIWrt.close();
    oFsDir.close();

}

From source file:com.knowledgetree.indexer.IndexerManager.java

public void create() throws Exception {
    IndexWriter writer = new IndexWriter(this.indexDirectory, this.analyzer, true);
    writer.close();
}

From source file:com.knowledgetree.indexer.IndexerManager.java

/**
 * Optimise the lucene database.//  w w w.ja v a2 s . c om
 * @throws Exception
 */
public void optimise() throws Exception {
    synchronized (this) {
        this.optimiseCount++;
    }

    this.logger.debug("Optimise index");
    WriteLock lock = this.locker.writeLock();
    lock.lock();
    try {
        if (null != this.queryReader) {
            this.querySearcher.close();
            this.queryReader.close();
        }

        IndexWriter writer = new IndexWriter(this.indexDirectory, this.analyzer, false);
        writer.optimize();
        writer.close();

        this.queryReader = IndexReader.open(this.indexDirectory);
        this.querySearcher = new IndexSearcher(this.queryReader);
    } finally {
        lock.unlock();
    }
}

From source file:com.knowledgetree.indexer.IndexerManager.java

/**
 * This adds a lucene document//w  w  w .j  a  v  a 2  s  . c o  m
 * 
 * @param documentId
 * @param content
 * @param discussion
 * @param title
 * @param version
 * @throws Exception 
 */
private void addLuceneDocument(int documentId, String content, String discussion, String title, String version)
        throws Exception {
    // create the lucene document

    Document document = new Document();
    document.add(new Field("DocumentID", IndexerManager.longToString(documentId), Field.Store.YES,
            Field.Index.TOKENIZED));
    document.add(new Field("Content", content, Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("Discussion", discussion, Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("Title", title, Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("Version", version, Field.Store.YES, Field.Index.UN_TOKENIZED));

    // add the document to lucene index
    try {
        this.logger.debug("Opening index writer: documentid=" + documentId);
        this.logger.debug("DocumentID: " + IndexerManager.longToString(documentId));
        this.logger.debug("Content: " + content);
        this.logger.debug("Discussion: " + discussion);
        IndexWriter writer = new IndexWriter(this.indexDirectory, this.analyzer, false);
        writer.addDocument(document);
        writer.close();
        this.logger.debug("Closing index writer: documentid=" + documentId);
    } catch (IOException ex) {
        logger.error(
                "Problem indexing document: documentid=" + documentId + " with exception: " + ex.getMessage());
    }

    this.reopenIndex();
}

From source file:com.krawler.esp.indexer.KrawlerIndexCreator.java

License:Open Source License

public int CreateIndex(ArrayList<DocumentFields> DocFields) {
    Document doc = new Document();
    Iterator<DocumentFields> itr = DocFields.iterator();

    while (itr.hasNext()) {
        DocumentFields tempfield = itr.next();
        Field docfield = new Field(tempfield.GetFieldName(), tempfield.GetFieldValue(), Field.Store.YES,
                Field.Index.TOKENIZED);
        doc.add(docfield);/*from w w w .  j  a v  a  2  s  . c  o  m*/
    }

    try {
        boolean CreateIndex = true;
        File f = new File(this.indexPath + "/segments");
        if (f.exists()) {
            CreateIndex = false;
        }

        IndexWriter indWriter = new IndexWriter(this.indexPath, this.KWLAnalyzer, CreateIndex);
        indWriter.addDocument(doc);
        indWriter.close();
    } catch (Exception ex) {
        return 0;
    }

    return 1;
}

From source file:com.krawler.esp.indexer.KrawlerIndexCreator.java

License:Open Source License

public void DeleteIndex(String documentId) {
    Term t = new Term("DocumentId", documentId);
    try {/*from w  w w.j ava  2 s  . co m*/
        IndexReader docInRead = IndexReader.open(this.indexPath);
        docInRead.deleteDocuments(t);
        docInRead.close();
        IndexWriter inw = new IndexWriter(this.indexPath, KWLAnalyzer, false);
        inw.optimize();
        inw.close();
    } catch (Exception ex) {
        System.out.print(ex.toString());
    }

}

From source file:com.krawler.esp.indexer.KrawlerIndexCreator.java

License:Open Source License

public void deleteAlertIndex(String alertId) {
    Term t = new Term("alertId", alertId);
    try {//from ww w  .j av a2 s.c  o m
        IndexReader docInRead = IndexReader.open(this.indexPath);
        docInRead.deleteDocuments(t);
        docInRead.close();
        IndexWriter inw = new IndexWriter(this.indexPath, KWLAnalyzer, false);
        inw.optimize();
        inw.close();
    } catch (Exception ex) {
        System.out.print(ex.toString());
    }

}

From source file:com.krawler.luceneSearchService.LuceneSearchImpl.java

License:Open Source License

@Override
public int writeIndex(List<Document> lucendDocs, String indexPath) {
    try {/*from   w  w  w .j  a v a2  s  . co m*/
        boolean CreateIndex = true;
        File f = new File(indexPath);
        if (f.exists()) {
            CreateIndex = false;
        }
        IndexWriter indWriter = new IndexWriter(indexPath, this.KWLAnalyzer, CreateIndex);

        for (Document luceneDoc : lucendDocs) {
            indWriter.addDocument(luceneDoc);
        }

        indWriter.optimize();
        indWriter.close();
    } catch (Exception ex) {
        Logger.getLogger(LuceneSearchImpl.class.getName()).log(Level.SEVERE, null, ex);
        return 0;
    }
    return 1;
}

From source file:com.krawler.luceneSearchService.LuceneSearchImpl.java

License:Open Source License

@Override
public void deleteIndex(String indexName, String searchValue, String indexPath) throws KWLuceneException {
    Term t = new Term(indexName, searchValue);
    try {/*from  w w  w  . j a  v a2s.c o m*/
        IndexReader docInRead = IndexReader.open(indexPath);
        docInRead.deleteDocuments(t);
        docInRead.close();
        IndexWriter inw = new IndexWriter(indexPath, this.KWLAnalyzer, false);
        inw.optimize();
        inw.close();
    } catch (Exception ex) {
        throw new KWLuceneException("DeleteIndex: " + ex.toString(), ex);
    }
}

From source file:com.leavesfly.lia.advsearching.MultiPhraseQueryTest.java

License:Apache License

protected void setUp() throws Exception {
    Directory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
            IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc1 = new Document();
    doc1.add(new Field("field", "the quick brown fox jumped over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc1);/*from  ww w. j  a va  2  s  .co m*/
    Document doc2 = new Document();
    doc2.add(new Field("field", "the fast fox hopped over the hound", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc2);
    writer.close();

    searcher = new IndexSearcher(directory);
}