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:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

@Override
public synchronized void delete(T object) {
    int docNum = getDocNum(object);
    if (docNum != -1) {
        try {//from   ww w.j a  v a 2  s. c  om
            String uniqueIndexFieldName = getUniqueIndexFieldName();
            String uniqueIndexFieldValue = getUniqueIndexFieldValue(object);
            Directory directory = FSDirectory.open(indexDir);
            if (DirectoryReader.indexExists(directory)) {
                Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
                IndexWriter indexWriter = new IndexWriter(directory,
                        new IndexWriterConfig(Version.LUCENE_44, analyzer));
                Term term = new Term(uniqueIndexFieldName, uniqueIndexFieldValue);
                indexWriter.deleteDocuments(term);
                indexWriter.close();
            }
            directory.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.doculibre.constellio.lucene.impl.SkosIndexHelperImpl.java

License:Open Source License

@Override
public void delete(Thesaurus thesaurus) {
    try {/*from  www.  ja  v a  2s.  c o  m*/
        Directory directory = FSDirectory.open(getIndexDir());
        if (DirectoryReader.indexExists(directory)) {
            Analyzer analyzer = getAnalyzerProvider().getAnalyzer(Locale.FRENCH);
            IndexWriter indexWriter = new IndexWriter(directory,
                    new IndexWriterConfig(Version.LUCENE_44, analyzer));
            Term term = new Term(THESAURUS_ID, thesaurus.getId().toString());
            indexWriter.deleteDocuments(term);
            indexWriter.close();
        }
        directory.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.dreamerpartner.codereview.lucene.IndexHelper.java

License:Apache License

/**
 * ?//ww  w .ja  v a2  s. c o m
 * @param module ?
 * @param term ?
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public static void delete(String module, Term... term) throws IOException {
    long beginTime = System.currentTimeMillis();
    IndexWriter writer = null;
    try {
        Directory dir = FSDirectory.open(new File(LuceneUtil.getIndexPath(module)));
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0);
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_0, analyzer);
        iwc.setMaxBufferedDocs(100);
        iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
        writer = new IndexWriter(dir, iwc);
        writer.deleteDocuments(term);
        writer.commit();
    } finally {
        long endTime = System.currentTimeMillis();
        logger.debug(module + " delete " + (endTime - beginTime) + " milliseconds.");
        if (writer != null)
            writer.close();
    }
}

From source file:com.edgenius.wiki.search.lucene.IndexWriterTemplate.java

License:Open Source License

public void deleteDocuments(Term term) {
    IndexWriter writer = indexFactory.getIndexWriter();
    try {//from   w w w .j a  va2  s  . com
        writer.deleteDocuments(term);
        writer.commit();
    } catch (IOException ex) {
        log.error("Error during deleting a document", ex);
        throw new IndexAccessException("Error during deleting a document.", ex);
    }
}

From source file:com.emental.mindraider.core.search.SearchCommander.java

License:Apache License

public static void updateIndex(File conceptFile, String notebookLabel, String conceptLabel, String conceptUri) {
    // TODO the body of this method to by in asynchronous thread
    IndexWriter writer;
    try {/*from   www .  j  ava  2  s . co m*/
        writer = new IndexWriter(getSearchIndexPath(), new StandardAnalyzer(), false);
        // update document via concept URI
        logger.debug("UPDATing FTS index for concept: " + conceptFile + " # " + notebookLabel + " # "
                + conceptLabel + " # " + conceptUri); // {{debug}}
        Document document = FileDocument.Document(conceptFile, notebookLabel, conceptLabel, conceptUri);
        writer.deleteDocuments(new Term("uri", conceptUri));
        writer.addDocument(document);
        // TODO removed just for now (before it will be done in async)
        //writer.optimize();
        writer.close();
    } catch (Exception e) {
        logger.debug("Unable to update FTS index", e); // {{debug}}
        // TODO close it in finally
    }
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

private void deleteDocumentByTypeAndId(FeatureType type, Long id, IndexWriter writer) throws IOException {
    BooleanQuery.Builder deleteQueryBuilder = new BooleanQuery.Builder();
    TermQuery idQuery = new TermQuery(new Term(FeatureIndexFields.FILE_ID.getFieldName(), id.toString()));
    deleteQueryBuilder.add(idQuery, BooleanClause.Occur.MUST);

    if (type != FeatureType.GENE) {
        TermQuery typeQuery = new TermQuery(
                new Term(FeatureIndexFields.FEATURE_TYPE.getFieldName(), type.getFileValue()));
        deleteQueryBuilder.add(typeQuery, BooleanClause.Occur.MUST);
    } else {//from   ww  w  .j av a 2 s  . c om
        deleteQueryBuilder.add(new TermQuery(
                new Term(FeatureIndexFields.FEATURE_TYPE.getFieldName(), FeatureType.BOOKMARK.getFileValue())),
                BooleanClause.Occur.MUST_NOT);
        deleteQueryBuilder.add(new TermQuery(
                new Term(FeatureIndexFields.FEATURE_TYPE.getFieldName(), FeatureType.VARIATION.getFileValue())),
                BooleanClause.Occur.MUST_NOT);
    }

    writer.deleteDocuments(deleteQueryBuilder.build());
}

From source file:com.epimorphics.server.indexers.LuceneIndex.java

License:Apache License

@Override
public void deleteGraph(String graphname) {
    try {//from   w w w .jav a2s  .  c o  m
        IndexWriter iwriter = getIndexWriter();
        iwriter.deleteDocuments(new Term(FIELD_GRAPH, graphname));
        requestCommit();
    } catch (Exception e) {
        throw new EpiException(e);
    }
}

From source file:com.esri.gpt.catalog.lucene.LuceneIndexAdapter.java

License:Apache License

/**
 * Deletes a collection of documents from the index.
 * @param uuids the collection of document UUIDS to delete 
 * @throws CatalogIndexException if an exception occurs  
 *//*  w  w w . j  av  a 2 s. co  m*/
@Override
public void deleteDocuments(String[] uuids) throws CatalogIndexException {

    if (this.useRemoteWriter) {
        RemoteIndexer remoteIndexer = new RemoteIndexer();
        remoteIndexer.send(this.getRequestContext(), "delete", uuids);
    }
    if (!this.useLocalWriter)
        return;

    IndexWriter writer = null;
    try {
        if ((uuids != null) && (uuids.length > 0)) {
            StringBuilder sbMsg = new StringBuilder("Removing UUIDs from the catalog index:");
            ArrayList<Term> alTerms = new ArrayList<Term>();
            for (String uuid : uuids) {
                String sUuid = Val.chkStr(uuid);
                if (sUuid.length() > 0) {
                    alTerms.add(new Term(Storeables.FIELD_UUID, sUuid));
                    if (getLogger().isLoggable(Level.FINER)) {
                        sbMsg.append("\n ").append(sUuid);
                    }
                }
            }
            if (alTerms.size() > 0) {
                if (getLogger().isLoggable(Level.FINER)) {
                    getLogger().finer(sbMsg.toString());
                }
                Term[] aTerms = alTerms.toArray(new Term[0]);
                writer = newWriter();
                this.getObservers().onDocumentDelete(uuids);
                writer.deleteDocuments(aTerms);
                this.getObservers().onDocumentDeleted(uuids);
            }
        }
    } catch (Exception e) {
        String sMsg = "Error deleting document(s):\n " + Val.chkStr(e.getMessage());
        throw new CatalogIndexException(sMsg, e);
    } finally {
        if (this.useSingleSearcher) {
            if (this.getAutoCommitSingleWriter()) {
                closeWriter(writer);
            }
        } else {
            closeWriter(writer);
        }
    }
}

From source file:com.esri.gpt.server.assertion.index.AsnIndexAdapter.java

License:Apache License

/**
 * Deletes an assertion.//  w  w w  .  ja  va2 s .co m
 * @param context the assertion operation context
 * @param assertionId the assertion ID to delete
 * @throws CorruptIndexException if the index is corrupt
 * @throws LockObtainFailedException if a write lock cannot be obtained
 * @throws IOException if an I/O exception occurs
 */
public void delete(AsnContext context, String assertionId)
        throws CorruptIndexException, LockObtainFailedException, IOException {
    IndexWriter writer = null;
    try {
        assertionId = Val.chkStr(assertionId);
        if (assertionId.length() > 0) {
            Term idTerm = new Term(AsnConstants.FIELD_SYS_ASSERTIONID, assertionId);
            writer = this.makeIndexWriter(this.newAnalyzer(context));
            writer.deleteDocuments(idTerm);
        }
    } finally {
        this.closeWriter(writer);
    }
}

From source file:com.gauronit.tagmata.core.Indexer.java

License:Open Source License

public void deleteIndex(String indexName) {
    try {//from  w  w w .  ja  va  2  s  .  c  o  m
        IndexWriter mainIndexWriter = new IndexWriter(
                FSDirectory.open(new File(indexDir + File.separator + MAIN_INDEX)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        Query q = new QueryParser(Version.LUCENE_35, "indexName", new StandardAnalyzer(Version.LUCENE_35))
                .parse(indexName);
        mainIndexWriter.deleteDocuments(q);
        mainIndexWriter.commit();
        mainIndexWriter.close();
        mainIndexWriter = null;

        IndexWriter writer = new IndexWriter(FSDirectory.open(new File(indexDir + File.separator + indexName)),
                new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
        writer.deleteAll();
        writer.prepareCommit();
        writer.commit();
        writer.close();
        writer = null;

        IOUtil.deleteDir(new File(indexDir + File.separator + indexName));
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
    }
}