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.institution.service.DefaultInstitutionalCollectionIndexService.java

License:Apache License

/**
 * Re-index the specified collections.  This can be used to re-index 
 * all collections/*from  w ww  .ja  va  2s .  co  m*/
 * 
 * @param collections - collections to re index
 * @param collectionIndexFolder - folder location of the index
 * @param overwriteExistingIndex - if set to true, will overwrite the exiting index.
 * 
 * @see edu.ur.ir.institution.InstitutionalCollectionIndexService#add(java.util.List, java.io.File, boolean)
 */
public void add(List<InstitutionalCollection> collections, File collectionIndexFolder,
        boolean overwriteExistingIndex) {
    LinkedList<Document> docs = new LinkedList<Document>();

    for (InstitutionalCollection c : collections) {
        log.debug("Adding collection " + c);
        docs.add(getDocument(c));
    }

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

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

        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.institution.service.DefaultInstitutionalCollectionIndexService.java

License:Apache License

/**
 * Delete the collection in the index./*from   ww  w  . ja  v  a 2s.c  o m*/
 * 
 * @param collectionId - id of the collection
 * @param collectionIndexFolder  - folder location of the collection index
 * @see edu.ur.ir.institution.InstitutionalCollectionIndexService#deleteFromIndex(java.lang.Long, java.io.File)
 */
public void delete(Long collectionId, File collectionIndexFolder) {
    if (log.isDebugEnabled()) {
        log.debug("deleting collection id : " + collectionId + " from index folder "
                + collectionIndexFolder.getAbsolutePath());
    }
    // if there is not collection folder then don't do anything
    if (collectionIndexFolder == null || !collectionIndexFolder.exists() || collectionIndexFolder.list() == null
            || collectionIndexFolder.list().length == 0) {
        return;
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {
        directory = FSDirectory.open(collectionIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(ID, NumericUtils.longToPrefixCoded(collectionId));
        writer.deleteDocuments(term);
        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.institution.service.DefaultInstitutionalCollectionIndexService.java

License:Apache License

/**
 * Write the document to the index in the directory.
 * //from www  . ja  va 2  s  .com
 * @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));
        writer = getWriter(directory);
        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.institution.service.DefaultInstitutionalItemIndexService.java

License:Apache License

/**
 * Add a set of items to the index - this is generally used for batch processing of multiple institutional items.  
 * This can also be used to re-index a set the existing set of items
 * /* w  w w . j av a2 s.  c o  m*/
 * @param items - set of items to add
 * @param institutionalItemIndex - index to add it to
 * @param overwriteExistingIndex - indicates this should overwrite the current index
 */
public void addItems(List<InstitutionalItem> items, File institutionalItemIndex,
        boolean overwriteExistingIndex) {

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

    for (InstitutionalItem item : items) {
        docs.add(getDocument(item, true));
    }

    IndexWriter writer = null;
    Directory directory = null;
    try {
        directory = FSDirectory.open(institutionalItemIndex);
        if (overwriteExistingIndex) {
            writer = getWriterOverwriteExisting(directory);
        } else {
            writer = getWriter(directory);
        }

        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);
            }
        }
        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.institution.service.DefaultInstitutionalItemIndexService.java

License:Apache License

/**
 * Write the list of documents to the index in the directory.
 * /*w ww. j a va 2s.c  o m*/
 * @param directoryPath - location where the directory exists.
 * @param documents - documents to add to the directory.
 */
private void writeDocument(File path, Document document, boolean create) {
    IndexWriter writer = null;
    Directory directory = null;
    try {
        directory = FSDirectory.open(path);
        if (!create) {
            writer = getWriter(directory);
        } else {
            writer = getWriterOverwriteExisting(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 (Exception e1) {
            log.error(e1);
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (Exception e) {
                log.error(e);
            }
        }
        directory = null;

    }
}

From source file:edu.ur.ir.person.service.DefaultNameAuthorityIndexService.java

License:Apache License

/**
 * Add the set of names to the index./*  w w  w.  j  a va  2 s  .c o  m*/
 * 
 * @see edu.ur.ir.person.NameAuthorityIndexService#addNames(java.util.List, java.io.File, boolean)
 */
public void addNames(List<PersonNameAuthority> names, File nameAuthorityIndexFolder,
        boolean overwriteExistingIndex) {

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

    for (PersonNameAuthority name : names) {
        docs.add(getDocument(name));
    }

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

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

        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);
            }
        }
        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.person.service.DefaultNameAuthorityIndexService.java

License:Apache License

/**
 * Write the list of documents to the index in the directory.
 * /*  w ww . j av  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) {
    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.ur.ir.researcher.service.DefaultResearcherIndexService.java

License:Apache License

/**
 * Delete the researcher from the specified index.
 * //from   w w  w  .jav a 2s .  c  o m
 * @see edu.ur.ir.researcher.ResearcherIndexService#deleteFromIndex(edu.ur.ir.researcher.Researcher, java.io.File)
 */
public void deleteFromIndex(Long researcherId, File researcherIndexFolder) {
    if (log.isDebugEnabled()) {
        log.debug("deleting researcher id : " + researcherId + " from index folder "
                + researcherIndexFolder.getAbsolutePath());
    }
    // if the researcher does not have an index folder
    // don't need to do anything.
    if (researcherIndexFolder == null || !researcherIndexFolder.exists() || researcherIndexFolder.list() == null
            || researcherIndexFolder.list().length == 0) {
        return;
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {
        directory = FSDirectory.open(researcherIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(ID, NumericUtils.longToPrefixCoded(researcherId));
        writer.deleteDocuments(term);
        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.ur.ir.researcher.service.DefaultResearcherIndexService.java

License:Apache License

/**
 * Add researchers to the index//from www . j a  v  a2s.c om
 * @see edu.ur.ir.researcher.ResearcherIndexService#addResearchers(java.util.List, java.io.File, boolean)
 */
public void addResearchers(List<Researcher> researchers, File researcherIndexFolder,
        boolean overwriteExistingIndex) {

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

    for (Researcher r : researchers) {
        log.debug("Adding researcher " + r);
        docs.add(getDocument(r));
    }

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

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

        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);
            }
        }
        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.DefaultUserGroupIndexService.java

License:Apache License

/**
 * Delete the user group in the index./*from w ww .j a va 2 s .co  m*/
 * 
 * @param userGroupId - id of the user
 * @param userIndexFolder  - folder location of the collection index
 */
public void delete(Long userGroupId, File userGroupIndexFolder) {
    if (log.isDebugEnabled()) {
        log.debug("deleting user group id : " + userGroupId + " from index folder "
                + userGroupIndexFolder.getAbsolutePath());
    }
    // if there is not collection folder then don't do anything
    if (userGroupIndexFolder == null || !userGroupIndexFolder.exists() || userGroupIndexFolder.list() == null
            || userGroupIndexFolder.list().length == 0) {
        return;
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {
        directory = FSDirectory.open(userGroupIndexFolder);
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
        writer = new IndexWriter(directory, indexWriterConfig);
        Term term = new Term(ID, NumericUtils.longToPrefixCoded(userGroupId));
        writer.deleteDocuments(term);
        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;
    }
}