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

License:Apache License

/**
 * Delete a group workspace file from the index.
 * /*from w w w  .  ja  va2  s.c  o  m*/
 * @param user - user who has the index you wish to remove the workspace file from
 * @param groupWorkspaceFileId - id of the group workspace file
 */
public void deleteGroupWorkspaceFileFromIndex(IrUser user, Long groupWorkspaceFileId) {
    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 || groupWorkspaceFileId == 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(GROUP_WORKSPACE_FILE_ID, NumericUtils.longToPrefixCoded(groupWorkspaceFileId));
        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 a group workspace folder from the index.
 * //  ww  w .j a  v  a  2  s  . com
 * @param groupWorkspaceId - id of the group workspace file
 * @param user - user whoes index should have the folder removed from
 */
public void deleteGroupWorkspaceFolderFromIndex(IrUser user, Long groupWorkspaceFolderId) {
    // 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 || groupWorkspaceFolderId == 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(GROUP_WORKSPACE_FOLDER_ID, NumericUtils.longToPrefixCoded(groupWorkspaceFolderId));
        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:es.pode.indexador.negocio.servicios.indexado.SrvIndexadorServiceImpl.java

License:Open Source License

/**
 * Eliminacion de ODEs de los indices.//w w  w  .ja va2  s . c om
 * @param idODE Array de identificadores alfanumericos de los ODEs que se quieren eliminar. 
 * @throws java.lang.Exception
 * @see es.pode.indexador.negocio.servicios.SrvIndexadorService#eliminarODE(java.lang.String)
 */
protected IndexadorVO[] handleEliminarODE(String[] idODE) throws java.lang.Exception {
    ArrayList respuestas = new ArrayList();
    if (idODE != null || idODE.length > 0) {
        //         Abrimos todos los indices de lectura para eliminar todos los ODEs de todos los indices que hay
        Hashtable indicesWR = new Hashtable();
        try {
            indicesWR = abrirIndicesWR(); //lo intentamos con los indices de escritura
        } catch (RuntimeException e) {
            logger.error("Error abriendo los indices de los idiomas indexados para eliminacion[" + e.getCause()
                    + "]");
            throw new Exception("Error abriendo los indices de los idiomas indexados para eliminacion", e);
        }
        //         Si no hay indices sobre los que actuar, devuelvo exito.
        if (indicesWR == null || indicesWR.size() <= 0) {
            logger.warn("Error eliminando ODEs. No hay indices sobre los que eliminar.");
            for (int x = 0; x < idODE.length; x++)
                respuestas.add(new IndexadorVO(idODE[x], 0, "Borrado correcto."));
            return (IndexadorVO[]) respuestas.toArray(new IndexadorVO[respuestas.size()]);
        }
        //         Me recorro las claves de la tabla(idiomas) para ir borrando para cada una los odes
        for (Enumeration e = indicesWR.keys(); e.hasMoreElements();) {
            String terminoClave = props.getProperty("campo_identificadorODE");
            String locale = (String) e.nextElement();
            IndexWriter indiceActual = (IndexWriter) indicesWR.get(locale);
            for (int x = 0; x < idODE.length; x++) {
                Term terminoABorrar = new Term(terminoClave, idODE[x]);
                try {
                    try {
                        indiceActual.deleteDocuments(terminoABorrar);
                        respuestas.add(rellenarVO(idODE[x], 0, "Borrado correcto"));
                        logger.info("Documento con idODE[" + idODE[x] + "] borrado del indice[" + locale + "]");
                    } catch (RuntimeException e1) {
                        logger.warn("Intentando borrar documento con idODE[" + idODE[x] + "] del indice["
                                + locale + "] imposible borrar[" + e1.getCause()
                                + "]. Continuamos con el resto del proceso.");
                        respuestas.add(rellenarVO(idODE[x], 1, "Borrado incorrecto"));
                        logger.warn("Aadimos de nuevo a la BD tesauros y areas curriculares del ODE "
                                + idODE[x] + " que habiamos eliminado");
                        logger.warn("Documento con idODE[" + idODE[x] + "] no encontrado en el indice[" + locale
                                + "]");
                    }
                } catch (Exception e1) {
                    logger.warn("Intentando borrar documento con idODE[" + idODE[x] + "] del tesauro [" + locale
                            + "] imposible borrar[" + e1.getCause()
                            + "]. Continuamos con el resto del proceso.");
                    respuestas.add(rellenarVO(idODE[x], 1, "Borrado del tesauro incorrecto"));
                    logger.warn("Documento con idODE[" + idODE[x] + "] no encontrado en el tesauro[" + locale
                            + "]");

                }
                indiceActual.flush();
            }
        }
        //         Cerramos los indices de escritura y regeneramos los spell checkers
        try {
            cierraIndicesWROptimiza(indicesWR);
        } catch (RuntimeException e) {
            logger.error("Error cerrando los indices de lectura despues de borrado[" + e.getCause() + "]");
            throw new Exception("Error cerrando los indices de lectura despues de borrado", e);
        }
    } else {
        logger.error("No hay ODEs para borrar");
        throw new Exception("No hay ODEs para borrar");
    }
    return (IndexadorVO[]) respuestas.toArray(new IndexadorVO[respuestas.size()]);
}

From source file:es.pode.indexador.negocio.servicios.indexado.SrvIndexadorServiceImpl.java

License:Open Source License

/**
 * Eliminacin de todos los documentos de un repositorio de ndices
 * Ademas regenera las sugerencias eliminando los elementos que sobran y regenerandose con los que quedan.
 * /*from   w w  w . ja v a2s .  c o  m*/
 * @param locale. Nos indica el repositorio del que se quieren eliminar todos los documentos.
 * @return Nmero de documentos eliminados
 * @throws java.lang.Exception
 */
private int borradoTodosDocumentos(String locale,
        es.pode.indexador.negocio.servicios.indexado.IdODEVO[] localizadores) throws java.lang.Exception {
    IndexWriter indiceAModificar = null;
    int numDocumentos = 0;
    try {
        indiceAModificar = this.indiceEscritura(locale);
        if (localizadores != null)
            numDocumentos = localizadores.length;

        logger.debug("Numero de documentos: " + numDocumentos);

        for (int i = 0; i < numDocumentos; i++) {
            Term terminoABorrar = new Term(props.getProperty("campo_identificadorODE"),
                    localizadores[i].getIdODE());

            logger.debug("terminoABorrar - localizadorODE[" + localizadores[i].getIdODE() + "]");
            logger.debug("terminoABorrar[" + terminoABorrar + "]");
            indiceAModificar.deleteDocuments(terminoABorrar);
        }
        logger.debug("Se han eliminado " + numDocumentos + " documentos del ndice");
        indiceAModificar.flush();
    } catch (Exception e) {
        logger.error("Error borrando documentos " + e);
        throw new Exception(e);
    } finally {
        if (indiceAModificar != null)
            indiceAModificar.close();
        Directory directorioIndiceSpell = this.indiceSpell(locale);
        //         Eliminamos la informacion del spell checker para regenerarla de nuevo
        spellCheckerReset(directorioIndiceSpell);
        spellCheckerAdd(this.getDirectorioPorLocale(locale), directorioIndiceSpell);
    }

    return numDocumentos;
}

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;/*w w  w. j a v a 2  s .c  o 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:fi.semantum.strategia.Lucene.java

License:Open Source License

public static synchronized void set(IndexWriter writer, String uuid, String text) throws IOException {

    Term term = new Term("uuid", uuid);
    writer.deleteDocuments(term);

    if (text != null) {

        Document document = new Document();

        Field name = new Field("uuid", uuid, STRING_TYPE);
        Field value = new Field("text", text, STRING_TYPE);

        document.add(name);/*from   w ww.ja v  a  2  s.c  o m*/
        document.add(value);

        writer.addDocument(document);

    }

}

From source file:fr.paris.lutece.plugins.directory.service.directorysearch.DirectoryIndexer.java

License:Open Source License

/**
 * {@inheritDoc}//  ww  w  .  jav a2 s  .c  o  m
 */
@Override
public void processIndexing(IndexWriter indexWriter, boolean bCreate, StringBuffer sbLogs)
        throws IOException, InterruptedException, SiteMessageException {
    Plugin plugin = PluginService.getPlugin(DirectoryPlugin.PLUGIN_NAME);
    RecordFieldFilter recordFieldFilter = new RecordFieldFilter();
    HashMap<Integer, List<Integer>> hm = new HashMap<Integer, List<Integer>>();
    IRecordService recordService = SpringContextService.getBean(RecordService.BEAN_SERVICE);

    if (!bCreate) {
        //incremental indexing
        //delete all record which must be delete
        for (IndexerAction action : DirectorySearchService.getInstance()
                .getAllIndexerActionByTask(IndexerAction.TASK_DELETE, plugin)) {
            sbLogRecord(sbLogs, action.getIdRecord(), DirectoryUtils.CONSTANT_ID_NULL,
                    IndexerAction.TASK_DELETE);
            indexWriter.deleteDocuments(new Term(DirectorySearchItem.FIELD_ID_DIRECTORY_RECORD,
                    Integer.toString(action.getIdRecord())));
            DirectorySearchService.getInstance().removeIndexerAction(action.getIdAction(), plugin);
        }

        //Hack : see this.appendListRecordToDelete() comments
        for (Integer nIdRecord : _lListIdRecordToDelete) {
            indexWriter.deleteDocuments(
                    new Term(DirectorySearchItem.FIELD_ID_DIRECTORY_RECORD, Integer.toString(nIdRecord)));
        }

        _lListIdRecordToDelete.clear();

        //Update all record which must be update
        for (IndexerAction action : DirectorySearchService.getInstance()
                .getAllIndexerActionByTask(IndexerAction.TASK_MODIFY, plugin)) {
            Integer nDirectoryId = recordService.getDirectoryIdByRecordId(Integer.valueOf(action.getIdRecord()),
                    plugin);

            if (nDirectoryId != null) {
                sbLogRecord(sbLogs, action.getIdRecord(), nDirectoryId.intValue(), IndexerAction.TASK_MODIFY);

                indexWriter.deleteDocuments(new Term(DirectorySearchItem.FIELD_ID_DIRECTORY_RECORD,
                        Integer.toString(action.getIdRecord())));

                this.appendKey(nDirectoryId, action.getIdRecord(), hm);
            }

            DirectorySearchService.getInstance().removeIndexerAction(action.getIdAction(), plugin);
        }

        hm = this.indexListRecord(indexWriter, hm, plugin);

        //add all record which must be add
        for (IndexerAction action : DirectorySearchService.getInstance()
                .getAllIndexerActionByTask(IndexerAction.TASK_CREATE, plugin)) {
            Integer nDirectoryId = recordService.getDirectoryIdByRecordId(Integer.valueOf(action.getIdRecord()),
                    plugin);

            if (nDirectoryId != null) {
                sbLogRecord(sbLogs, action.getIdRecord(), nDirectoryId, IndexerAction.TASK_CREATE);

                this.appendKey(nDirectoryId, action.getIdRecord(), hm);
            }

            DirectorySearchService.getInstance().removeIndexerAction(action.getIdAction(), plugin);
        }

        hm = this.indexListRecord(indexWriter, hm, plugin);
    } else {
        // Index only the directories that have the attribute is_indexed as true
        DirectoryFilter filter = new DirectoryFilter();
        filter.setIsIndexed(DirectoryFilter.FILTER_TRUE);

        for (Directory directory : DirectoryHome.getDirectoryList(filter, plugin)) {
            sbLogs.append("Indexing Directory");
            sbLogs.append("\r\n");
            recordFieldFilter.setIdDirectory(directory.getIdDirectory());

            for (Record record : recordService.getListRecord(recordFieldFilter, plugin)) {
                sbLogRecord(sbLogs, record.getIdRecord(), record.getDirectory().getIdDirectory(),
                        IndexerAction.TASK_CREATE);

                this.appendKey(directory.getIdDirectory(), record.getIdRecord(), hm);
            }
        }

        hm = this.indexListRecord(indexWriter, hm, plugin);
    }
}

From source file:fr.paris.lutece.plugins.document.service.docsearch.DocSearchService.java

License:Open Source License

/**
 * Indexing documents for searching/*from  w w  w  .  j a va  2 s.c o m*/
 * @param bCreate tell if it's total indexing or total (total = true)
 * @return indexing logs
 */
public String processIndexing(boolean bCreate) {
    StringBuilder sbLogs = new StringBuilder();

    IndexWriter writer = null;
    boolean bCreateIndex = bCreate;

    try {
        sbLogs.append("\r\nIndexing all contents ...\r\n");

        Directory dir = NIOFSDirectory.open(new File(_strIndex));

        if (!DirectoryReader.indexExists(dir)) { //init index
            bCreateIndex = true;
        }

        Date start = new Date();
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_46, _analyzer);

        if (bCreateIndex) {
            conf.setOpenMode(OpenMode.CREATE);
        } else {
            conf.setOpenMode(OpenMode.APPEND);
        }

        writer = new IndexWriter(dir, conf);

        if (!bCreateIndex) {
            //incremental indexing

            //add all document which must be add
            for (IndexerAction action : getAllIndexerActionByTask(IndexerAction.TASK_CREATE)) {
                try {
                    ArrayList<Integer> luceneDocumentId = new ArrayList<Integer>();
                    luceneDocumentId.add(action.getIdDocument());

                    List<org.apache.lucene.document.Document> luceneDocument = _indexer
                            .getDocuments(luceneDocumentId);

                    if ((luceneDocument != null) && (luceneDocument.size() > 0)) {
                        Iterator<org.apache.lucene.document.Document> it = luceneDocument.iterator();

                        while (it.hasNext()) {
                            org.apache.lucene.document.Document doc = it.next();
                            writer.addDocument(doc);
                            sbLogs.append("Adding ");
                            sbLogs.append(doc.get(DocSearchItem.FIELD_TYPE));
                            sbLogs.append(" #");
                            sbLogs.append(doc.get(DocSearchItem.FIELD_UID));
                            sbLogs.append(" - ");
                            sbLogs.append(doc.get(DocSearchItem.FIELD_TITLE));
                            sbLogs.append("\r\n");
                        }
                    }
                } catch (IOException e) {
                    sbLogs.append("Error durign document indexation parsing.");
                    sbLogs.append("\r\n");
                }

                removeIndexerAction(action.getIdAction());
            }

            //Update all document which must be update
            for (IndexerAction action : getAllIndexerActionByTask(IndexerAction.TASK_MODIFY)) {
                try {
                    ArrayList<Integer> luceneDocumentId = new ArrayList<Integer>();
                    luceneDocumentId.add(action.getIdDocument());

                    List<org.apache.lucene.document.Document> luceneDocument = _indexer
                            .getDocuments(luceneDocumentId);

                    if ((luceneDocument != null) && (luceneDocument.size() > 0)) {
                        Iterator<org.apache.lucene.document.Document> it = luceneDocument.iterator();

                        while (it.hasNext()) {
                            org.apache.lucene.document.Document doc = it.next();
                            writer.updateDocument(
                                    new Term(DocSearchItem.FIELD_UID, Integer.toString(action.getIdDocument())),
                                    doc);
                            sbLogs.append("Updating ");
                            sbLogs.append(doc.get(DocSearchItem.FIELD_TYPE));
                            sbLogs.append(" #");
                            sbLogs.append(doc.get(DocSearchItem.FIELD_UID));
                            sbLogs.append(" - ");
                            sbLogs.append(doc.get(DocSearchItem.FIELD_TITLE));
                            sbLogs.append("\r\n");
                        }
                    }
                } catch (IOException e) {
                    sbLogs.append("Error durign document indexation parsing.");
                    sbLogs.append("\r\n");
                }

                removeIndexerAction(action.getIdAction());
            }

            //delete all document which must be delete
            for (IndexerAction action : getAllIndexerActionByTask(IndexerAction.TASK_DELETE)) {
                writer.deleteDocuments(
                        new Term(DocSearchItem.FIELD_UID, Integer.toString(action.getIdDocument())));
                sbLogs.append("Deleting ");
                sbLogs.append(" #");
                sbLogs.append(action.getIdDocument());
                sbLogs.append("\r\n");

                removeIndexerAction(action.getIdAction());
            }
        } else {
            //delete all incremental action
            removeAllIndexerAction();

            Collection<Integer> listIdDocuments = DocumentHome.findAllPrimaryKeys();
            ArrayList<Integer> luceneDocumentId;

            for (Integer nIdDocument : listIdDocuments) {
                try {
                    luceneDocumentId = new ArrayList<Integer>();
                    luceneDocumentId.add(nIdDocument);

                    List<Document> listDocuments = _indexer.getDocuments(luceneDocumentId);

                    for (Document doc : listDocuments) {
                        writer.addDocument(doc);
                        sbLogs.append("Indexing ");
                        sbLogs.append(doc.get(DocSearchItem.FIELD_TYPE));
                        sbLogs.append(" #");
                        sbLogs.append(doc.get(DocSearchItem.FIELD_UID));
                        sbLogs.append(" - ");
                        sbLogs.append(doc.get(DocSearchItem.FIELD_TITLE));
                        sbLogs.append("\r\n");
                    }
                } catch (IOException e) {
                    sbLogs.append("Error durign document indexation parsing.");
                    sbLogs.append("\r\n");
                }
            }
        }

        Date end = new Date();
        sbLogs.append("Duration of the treatment : ");
        sbLogs.append(end.getTime() - start.getTime());
        sbLogs.append(" milliseconds\r\n");
    } catch (Exception e) {
        sbLogs.append(" caught a ");
        sbLogs.append(e.getClass());
        sbLogs.append("\n with message: ");
        sbLogs.append(e.getMessage());
        sbLogs.append("\r\n");
        AppLogService.error("Indexing error : " + e.getMessage(), e);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            AppLogService.error(e.getMessage(), e);
        }
    }

    return sbLogs.toString();
}

From source file:framework.retrieval.engine.index.create.impl.RIndexWriter.java

License:Apache License

/**
 * //from  w  w w  .j  av a2 s .  c  om
 * @param indexPathType
 * @param term
 */
public void deleteDocument(String indexPathType, Term term) {

    //      RetrievalIndexLock.getInstance().lock(indexPathType);

    IndexWriter indexWriter = null;
    try {
        RetrievalUtil.debugLog(log, "" + indexPathType);
        try {
            indexWriter = getIndexWriter(indexPathType);
            indexWriter.deleteDocuments(term);
        } catch (Exception e) {
            throw new RetrievalDocumentException(e);
        }
    } finally {
        if (indexWriter != null) {
            try {
                indexWriter.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                indexWriter.close();
            } catch (Exception e) {
                RetrievalUtil.errorLog(log, e);
            }
        }
        //         RetrievalIndexLock.getInstance().unlock(indexPathType);
    }
}

From source file:framework.retrieval.engine.index.create.impl.RIndexWriter.java

License:Apache License

/**
 * ?//from w  w w  . j a  v  a 2  s  .c  om
 * @param indexPathType
 * @param terms
 */
public void deleteDocument(String indexPathType, List<Term> terms) {

    if (terms == null || terms.size() <= 0) {
        return;
    }

    //      RetrievalIndexLock.getInstance().lock(indexPathType);

    IndexWriter indexWriter = null;
    try {
        int length = terms.size();

        RetrievalUtil.debugLog(log, "" + length + "" + indexPathType);
        try {
            indexWriter = getIndexWriter(indexPathType);
            indexWriter.deleteDocuments(terms.toArray(new Term[length]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        if (indexWriter != null) {
            try {
                indexWriter.commit();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                indexWriter.close();
            } catch (Exception e) {
                RetrievalUtil.errorLog(log, e);
            }
        }
        //         RetrievalIndexLock.getInstance().unlock(indexPathType);
    }
}