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

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

Introduction

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

Prototype

@Override
public final long commit() throws IOException 

Source Link

Document

Commits all pending changes (added and deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.

Usage

From source file:edu.ur.ir.user.service.DefaultUserGroupIndexService.java

License:Apache License

/**
 * Re-index the specified user groups.  This can be used to re-index 
 * all user groups/*from w  ww  .j ava  2s. c om*/
 * 
 * @param userGroups - user groups to re index
 * @param userGroupIndexFolder - folder location of the index
 * @param overwriteExistingIndex - if set to true, will overwrite the exiting index.
 */
public void add(List<IrUserGroup> userGroups, File userGroupIndexFolder, boolean overwriteExistingIndex) {
    LinkedList<Document> docs = new LinkedList<Document>();

    for (IrUserGroup g : userGroups) {
        log.debug("Adding user group " + g);
        docs.add(getDocument(g));
    }

    IndexWriter writer = null;
    Directory directory = null;
    try {
        directory = FSDirectory.open(userGroupIndexFolder);

        if (overwriteExistingIndex) {
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
            writer = new IndexWriter(directory, indexWriterConfig);
        } else {
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
            writer = new IndexWriter(directory, indexWriterConfig);
        }

        for (Document d : docs) {
            writer.addDocument(d);
        }
        writer.commit();
    } catch (Exception e) {
        log.error(e);
        errorEmailService.sendError(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                log.error(e);
                try {
                    if (IndexWriter.isLocked(directory)) {
                        IndexWriter.unlock(directory);
                    }
                } catch (IOException e1) {
                    log.error(e1);
                }
            }
        }
        writer = null;
        if (directory != null) {
            try {
                directory.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        directory = null;
        docs = null;
    }
}

From source file:edu.ur.ir.user.service.DefaultUserGroupIndexService.java

License:Apache License

/**
 * Write the document to the index in the directory.
 * /* w  ww .j a va  2  s  . c om*/
 * @param directoryPath - location where the directory exists.
 * @param documents - documents to add to the directory.
 */
private void writeDocument(String directoryPath, Document document) {
    log.debug("write document to directory " + directoryPath);
    Directory directory = null;
    IndexWriter writer = null;
    try {
        directory = FSDirectory.open(new File(directoryPath));
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
        writer = new IndexWriter(directory, indexWriterConfig);
        writer.addDocument(document);
        writer.commit();
    } catch (Exception e) {
        log.error(e);
        errorEmailService.sendError(e);
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            log.error(e);
            try {
                if (IndexWriter.isLocked(directory)) {
                    IndexWriter.unlock(directory);
                }
            } catch (IOException e1) {
                log.error(e1);
            }
        }
        writer = null;
        if (directory != null) {
            try {
                directory.close();
            } catch (Exception e) {
                log.error(e);

            }
        }
        directory = null;
    }
}

From source file:edu.ur.ir.user.service.DefaultUserIndexService.java

License:Apache License

/**
 * Write the document to the index in the directory.
 * // w w  w .ja v a2  s .  c  o m
 * @param directoryPath - location where the directory exists.
 * @param documents - documents to add to the directory.
 */
private void writeDocument(File directoryPath, Document document) {
    log.debug("write document to directory " + directoryPath);
    IndexWriter writer = null;
    Directory directory = null;
    try {
        directory = FSDirectory.open(directoryPath);
        writer = getWriter(directory);
        writer.addDocument(document);
        writer.commit();

    } catch (IOException e) {
        log.error(e);
        errorEmailService.sendError(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        writer = null;
        try {
            IndexWriter.unlock(directory);
        } catch (IOException e1) {
            log.error(e1);
        }

        if (directory != null) {
            try {
                directory.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        directory = null;
    }
}

From source file:edu.ur.ir.user.service.DefaultUserIndexService.java

License:Apache License

/**
 * Add users to the index//from  ww w .j ava2  s .co m
 * @see edu.ur.ir.user.UserIndexService#addUsers(java.util.List, java.io.File, boolean)
 */
public void addUsers(List<IrUser> users, File userIndexFolder, boolean overwriteExistingIndex) {

    LinkedList<Document> docs = new LinkedList<Document>();

    for (IrUser user : users) {
        log.debug("adding user " + user);
        docs.add(getDocument(user));
    }

    IndexWriter writer = null;
    Directory directory = null;
    try {
        directory = FSDirectory.open(userIndexFolder);

        if (overwriteExistingIndex) {
            writer = getWriterOverwriteExisting(directory);
        } else {
            writer = getWriter(directory);
        }

        for (Document d : docs) {
            writer.addDocument(d);
        }
        writer.commit();

    } catch (IOException e) {
        log.error(e);
        errorEmailService.sendError(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        writer = null;
        try {
            IndexWriter.unlock(directory);
        } catch (IOException e1) {
            log.error(e1);
        }

        if (directory != null) {
            try {
                directory.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        directory = null;
        docs = null;
    }
}

From source file:edu.ur.ir.user.service.DefaultUserWorkspaceIndexService.java

License:Apache License

/**
 * Write the list of documents to the index in the directory.
 * //from w  ww .j a va2  s . com
 * @param directoryPath - location where the directory exists.
 * @param documents - documents to add to the directory.
 */
private void writeDocument(File directoryPath, Document document) {
    log.debug("write document to directory " + directoryPath);
    IndexWriter writer = null;
    Directory directory = null;
    try {
        directory = FSDirectory.open(directoryPath);
        writer = getWriter(directory);
        writer.addDocument(document);
        writer.commit();

    } catch (Exception e) {
        log.error(e);
        errorEmailService.sendError(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        writer = null;
        try {
            IndexWriter.unlock(directory);
        } catch (IOException e1) {
            log.error(e1);
        }

        if (directory != null) {
            try {
                directory.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        directory = null;
    }
}

From source file:edu.utsa.sifter.Bookmark.java

License:Apache License

public void index(final IndexWriter writer) throws IOException {
    // System.err.println("Indexing bookmark: \"" + Comment + "\" on docs \"" + Docs + "\"");
    final Date now = new Date();
    Created = now.getTime();//from ww w. j  av  a2 s  .  c om

    final Document doc = new Document();
    doc.add(new TextField("Comment", Comment, Field.Store.YES));
    doc.add(new LongField("Created", Created, Field.Store.YES));
    doc.add(new TextField("Docs", Docs, Field.Store.YES));

    writer.addDocument(doc);
    writer.commit();
}

From source file:engine.easy.indexer.EasySearchIndexBuilder.java

License:Apache License

public static void updateDocuments(Map<Integer, Document> docsMap) {

    try {//from www  .j ava2 s.  c om
        if (!docsMap.isEmpty()) {
            Directory indexDir = FSDirectory.open(new File(AppConstants.INDEX_DIR_PATH));
            IndexWriter indexWriter = new IndexWriter(indexDir, new EasySearchAnalyzer(), Boolean.TRUE,
                    MaxFieldLength.UNLIMITED);
            EasySearchIndexWriter esiWrtier = new EasySearchIndexWriter(indexWriter);

            if (IndexWriter.isLocked(indexDir)) {
                IndexWriter.unlock(indexDir);
            }

            for (Integer docId : docsMap.keySet()) {
                Document doc = docsMap.get(docId);
                indexWriter.updateDocument(new Term("DOCID", docId.toString()), doc);
            }

            indexWriter.optimize();
            indexWriter.commit();
            indexWriter.close();
        }
    } catch (Exception e) {
        System.out.println("Exception : " + e.toString());
    }
}

From source file:es.ua.labidiomas.corpus.index.Indexer.java

private void deleteNgrams(String textID, String lang, String fileSeparator) throws IOException {
    for (int i = 1; i <= 4; i++) {
        File indexDir = new File(
                indexPath + fileSeparator + "ngrams" + fileSeparator + i + fileSeparator + lang);
        Directory directory = null;//from   www  .ja v a  2s  .  co m
        IndexWriter indexEraser = null;
        try {
            directory = FSDirectory.open(indexDir);
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer);
            config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
            config.setWriteLockTimeout(5000l);
            indexEraser = new IndexWriter(directory, config);
            Term term = new Term("textID", textID);
            indexEraser.deleteDocuments(term);
            indexEraser.commit();
        } finally {
            if (directory != null) {
                directory.close();
            }
            if (indexEraser != null) {
                indexEraser.close();
            }
        }
    }
}

From source file:eu.eexcess.sourceselection.redde.indexer.TrecToLuceneIndexBuilder.java

License:Apache License

/**
 * Builds/overwrites existing Lucene index using TREC documents as source
 *///  w  ww .java 2  s  . com
public void index() {
    Date startTimestamp = new Date();
    final File documentsDirectory = new File(documentsPath);

    if (!documentsDirectory.exists() || !documentsDirectory.canRead()) {

        logger.severe("cannot access document directory [" + documentsDirectory.getAbsolutePath() + "]");

    } else {

        try {
            logger.info("processing directory [" + documentsPath + "] to index [" + indexPath + "]");

            Directory indexDirectory = FSDirectory.open(new File(indexPath));
            Analyzer analyzer = new EnglishAnalyzer();
            IndexWriterConfig writerConfig = new IndexWriterConfig(luceneVersion, analyzer);

            writerConfig.setOpenMode(OpenMode.CREATE);
            writerConfig.setRAMBufferSizeMB(ramBufferSize);

            IndexWriter indexWriter = new IndexWriter(indexDirectory, writerConfig);
            indexDocs(indexWriter, documentsDirectory);

            indexWriter.commit();
            indexWriter.close();

            Date stopTimestamp = new Date();
            logger.info("processed [" + dirsCount + "] dirs [" + filesCount + "] files [" + documentsTotal
                    + "] documents [" + filesSkipped + "] files skipped in ["
                    + (stopTimestamp.getTime() - startTimestamp.getTime()) + "] ms]");

        } catch (IOException e) {
            logger.log(Level.SEVERE, "failed indexing documents", e);
        }
    }
}

From source file:fr.ericlab.sondy.algo.eventdetection.ET.java

License:Open Source License

public static LinkedList<String> getFrequentBigrams(String tweets, HashSet<String> bigrams) {
    try {//from   w ww .  j ava 2  s .c  o m
        LinkedList<String> FCB = new LinkedList<String>();
        WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_36);
        RAMDirectory temporaryIndex = new RAMDirectory();
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
        IndexWriter temporaryWriter = new IndexWriter(temporaryIndex, config);
        Document doc = new Document();
        doc.add(new Field("content", tweets, Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.YES));
        temporaryWriter.addDocument(doc);
        temporaryWriter.commit();
        IndexReader temporaryReader = IndexReader.open(temporaryWriter, true);
        TermEnum allTerms = temporaryReader.terms();
        while (allTerms.next()) {
            String term = allTerms.term().text();
            if (bigrams.contains(term)) {
                FCB.add(term);
            }
        }
        temporaryWriter.close();
        temporaryReader.close();
        temporaryIndex.close();
        return FCB;
    } catch (LockObtainFailedException ex) {
        Logger.getLogger(ET.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ET.class.getName()).log(Level.SEVERE, null, ex);
    }
    return new LinkedList<>();
}