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

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

Introduction

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

Prototype

public long deleteDocuments(Query... queries) throws IOException 

Source Link

Document

Deletes the document(s) matching any of the provided queries.

Usage

From source file:edu.ur.ir.institution.service.DefaultInstitutionalItemIndexService.java

License:Apache License

/**
 * Delete the institutional item from the index.
 * /*from   www. j a  v a  2 s .c  om*/
 * @see edu.ur.ir.institution.InstitutionalItemIndexService#deleteItem(edu.ur.ir.institution.InstitutionalItem, java.io.File)
 */
public void deleteItem(Long id, File institutionalItemIndex) {

    Directory directory = null;
    IndexWriter writer = null;

    // if the index is empty or does not exist then do nothing
    if (institutionalItemIndex == null || institutionalItemIndex.list() == null
            || institutionalItemIndex.list().length == 0) {
        return;
    }

    try {
        directory = FSDirectory.open(institutionalItemIndex);
        writer = getWriter(directory);
        Term term = new Term(ID, NumericUtils.longToPrefixCoded(id));
        writer.deleteDocuments(term);

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

License:Apache License

/**
 * Delete the name from the index./*from  w  w w . j  a  v  a2 s  .  c  o m*/
 * 
 * @see edu.ur.ir.person.NameAuthorityIndexService#deleteFromIndex(edu.ur.ir.person.PersonNameAuthority)
 */
public void deleteFromIndex(PersonNameAuthority personNameAuthority, File nameAuthorityIndexFolder) {
    // if the name index folder doesnot exist 
    // do nothing.
    if (nameAuthorityIndexFolder == null) {
        return;
    }

    Directory directory = null;
    IndexWriter writer = null;

    try {
        directory = FSDirectory.open(nameAuthorityIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(PERSON_NAME_AUTHORITY_ID, personNameAuthority.getId().toString());
        writer.deleteDocuments(term);
        writer.close();

    } catch (Exception e) {
        log.error(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   www  .jav  a  2s . com*/
 * @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.user.service.DefaultUserGroupIndexService.java

License:Apache License

/**
 * Delete the user group in the index./*from www. j  ava2s  .c om*/
 * 
 * @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;
    }
}

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

License:Apache License

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

    Directory directory = null;
    IndexWriter writer = null;
    try {
        directory = FSDirectory.open(userIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(USER_ID, NumericUtils.longToPrefixCoded(userId));
        writer.deleteDocuments(term);

    } 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.DefaultUserWorkspaceIndexService.java

License:Apache License

/**
 * Delete the personal file from the index.
 * /* w  w w .ja  v  a2 s .  c  o  m*/
 * @see edu.ur.ir.user.UserWorkspaceIndexService#deleteFromIndex(edu.ur.ir.user.PersonalFile)
 */
public void deleteFileFromIndex(IrUser user, Long personalFileId) {

    String info = user.getPersonalIndexFolder();
    File personalIndexFolder = null;

    // if the user does not have an index folder
    // don't need to do anything.
    if (info == null || personalFileId == null) {
        return;
    } else {
        personalIndexFolder = new File(info);
        if (!personalIndexFolder.exists() || personalIndexFolder.list() == null
                || personalIndexFolder.list().length == 0) {
            return;
        }
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {

        directory = FSDirectory.open(personalIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(PERSONAL_FILE_ID, NumericUtils.longToPrefixCoded(personalFileId));
        writer.deleteDocuments(term);

    } 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.user.service.DefaultUserWorkspaceIndexService.java

License:Apache License

/**
 * Delete the collection from the index.
 * /*  w  w w  . j  a  v a 2  s . co  m*/
 * @see edu.ur.ir.user.UserWorkspaceIndexService#deleteFromIndex(edu.ur.ir.user.PersonalFolder)
 */
public void deleteCollectionFromIndex(IrUser user, Long personalCollectionId) {

    // if the user does not have an index folder
    // don't need to do anything.
    String info = user.getPersonalIndexFolder();
    File personalIndexFolder = null;

    // if the user does not have an index folder
    // don't need to do anything.
    if (info == null || personalCollectionId == null) {
        return;
    } else {
        personalIndexFolder = new File(info);
        if (!personalIndexFolder.exists()) {
            return;
        }
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {

        directory = FSDirectory.open(personalIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(PERSONAL_COLLECTION_ID, NumericUtils.longToPrefixCoded(personalCollectionId));
        writer.deleteDocuments(term);
        writer.close();
    } 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.user.service.DefaultUserWorkspaceIndexService.java

License:Apache License

/**
 * Delete the folder from the index./*from  w  w w  .  jav  a2 s .co  m*/
 * 
 * @see edu.ur.ir.user.UserWorkspaceIndexService#deleteFromIndex(edu.ur.ir.user.PersonalFolder)
 */
public void deleteFolderFromIndex(IrUser user, Long personalFolderId) {

    // if the user does not have an index folder
    // don't need to do anything.
    String info = user.getPersonalIndexFolder();
    File personalIndexFolder = null;

    // if the user does not have an index folder
    // don't need to do anything.
    if (info == null || personalFolderId == null) {
        return;
    } else {
        personalIndexFolder = new File(info);
        if (!personalIndexFolder.exists()) {
            return;
        }
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {

        directory = FSDirectory.open(personalIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(PERSONAL_FOLDER_ID, NumericUtils.longToPrefixCoded(personalFolderId));
        writer.deleteDocuments(term);
        writer.close();
    } 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.user.service.DefaultUserWorkspaceIndexService.java

License:Apache License

/**
 * Delete the shared inbox file from the index.
 * /* www . j av  a  2  s  . co  m*/
 * @see edu.ur.ir.user.UserWorkspaceIndexService#deleteFromIndex(java.io.File, edu.ur.ir.user.SharedInboxFile)
 */
public void deleteInboxFileFromIndex(IrUser user, Long sharedInboxFileId) {

    // if the user does not have an index folder
    // don't need to do anything.
    String info = user.getPersonalIndexFolder();
    File personalIndexFolder = null;

    // if the user does not have an index folder
    // don't need to do anything.
    if (info == null || sharedInboxFileId == null) {
        return;
    } else {
        personalIndexFolder = new File(info);
        if (!personalIndexFolder.exists()) {
            return;
        }
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {

        directory = FSDirectory.open(personalIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(SHARED_INBOX_FILE_ID, NumericUtils.longToPrefixCoded(sharedInboxFileId));
        writer.deleteDocuments(term);
    } 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.user.service.DefaultUserWorkspaceIndexService.java

License:Apache License

/**
 * Delete the personal item from the index.
 * /*from  w  ww  .j  ava 2 s  . c om*/
 * @see edu.ur.ir.user.UserWorkspaceIndexService#deleteFromIndex(edu.ur.ir.user.PersonalItem)
 */
public void deleteItemFromIndex(IrUser user, Long personalItemId) {
    // if the user does not have an index folder
    // don't need to do anything.
    String info = user.getPersonalIndexFolder();
    File personalIndexFolder = null;

    // if the user does not have an index folder
    // don't need to do anything.
    if (info == null || personalItemId == null) {
        return;
    } else {
        personalIndexFolder = new File(info);
        if (!personalIndexFolder.exists()) {
            return;
        }
    }

    Directory directory = null;
    IndexWriter writer = null;
    try {

        directory = FSDirectory.open(personalIndexFolder);
        writer = getWriter(directory);
        Term term = new Term(PERSONAL_ITEM_ID, NumericUtils.longToPrefixCoded(personalItemId));
        writer.deleteDocuments(term);
    } 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;
    }
}