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.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

/**
 * @return false if the index entry was not updated because it
 * was already current; true otherwise.//  ww  w. j av a  2 s.c o m
 */
public boolean addToIndex(final Profile profile) throws IOException {

    boolean needNewEntry = true;

    String key = getKey(profile);
    String userId = profile.getUserId();
    String firstName = profile.getFirstName();
    String middleName = profile.getMiddleName();
    String lastName = profile.getLastName();
    String nickName = profile.getNickName();
    String primaryEmail = profile.getPrimaryEmail();
    String displayName = profile.getDisplayName();

    IndexReader reader = IndexReader.open(indexDir);
    TermDocs termDocs = reader.termDocs(new Term("key", key));
    while (termDocs.next()) {
        Document existingDoc = reader.document(termDocs.doc());
        if (areEqual("profile", existingDoc.get("class")) && areEqual(userId, existingDoc.get("userId"))
                && areEqual(firstName, existingDoc.get("firstName"))
                && areEqual(middleName, existingDoc.get("middleName"))
                && areEqual(lastName, existingDoc.get("lastName"))
                && areEqual(nickName, existingDoc.get("nickName"))
                && areEqual(primaryEmail, existingDoc.get("primaryEmail"))
                && areEqual(displayName, existingDoc.get("displayName"))) {
            needNewEntry = false;
        }
    }
    termDocs.close();
    reader.close();

    if (needNewEntry) {
        Document newDoc = new Document();
        newDoc.add(new Field("key", key, Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("class", "profile", Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("userId", userId, Field.Store.YES, Field.Index.UN_TOKENIZED));
        if (firstName != null)
            newDoc.add(new Field("firstName", firstName, Field.Store.YES, Field.Index.TOKENIZED));
        if (middleName != null)
            newDoc.add(new Field("middleName", middleName, Field.Store.YES, Field.Index.TOKENIZED));
        if (lastName != null)
            newDoc.add(new Field("lastName", lastName, Field.Store.YES, Field.Index.TOKENIZED));
        if (nickName != null)
            newDoc.add(new Field("nickName", nickName, Field.Store.YES, Field.Index.TOKENIZED));
        if (primaryEmail != null)
            newDoc.add(new Field("primaryEmail", primaryEmail, Field.Store.YES, Field.Index.UN_TOKENIZED));
        if (displayName != null)
            newDoc.add(new Field("displayName", displayName, Field.Store.YES, Field.Index.TOKENIZED));

        IndexWriter writer = null;
        try {
            writer = new IndexWriter(indexDir, analyzer, false);
            writer.deleteDocuments(new Term("key", key)); // Delete old entry, if present
            writer.addDocument(newDoc);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (Exception e) {
                }
            ;
        }

        log.trace(String.format(
                "Indexed profile[userId=%s,firstName=%s,lastName=%s,nickName=%s,primaryEmail=%s,displayName=%s]",
                userId, firstName, lastName, nickName, primaryEmail, displayName));
    }

    return needNewEntry;
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public void removeFromIndex(final App app) throws IOException {
    IndexWriter writer = null;
    try {/*from  ww  w  .java 2 s.c o m*/
        writer = new IndexWriter(indexDir, analyzer, false);
        writer.deleteDocuments(new Term("key", getKey(app)));
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (Exception e) {
            }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public void removeFromIndex(final Group group) throws IOException {
    IndexWriter writer = null;
    try {//  w  ww  .j av a 2s.  c  o  m
        writer = new IndexWriter(indexDir, analyzer, false);
        writer.deleteDocuments(new Term("key", getKey(group)));
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (Exception e) {
            }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public void removeFromIndex(final Profile profile) throws IOException {
    IndexWriter writer = null;
    try {//from   w w  w. j  ava  2 s.  c  o m
        writer = new IndexWriter(indexDir, analyzer, false);
        writer.deleteDocuments(new Term("key", getKey(profile)));
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (Exception e) {
            }
        ;
    }
}

From source file:com.sxc.lucene.index.IndexingTest.java

License:Apache License

public void testDeleteBeforeOptimize() throws IOException {
    IndexWriter writer = getWriter();
    assertEquals(2, writer.numDocs()); // A
    writer.deleteDocuments(new Term("id", "1")); // B
    writer.commit();//from  ww  w.  j av a 2 s . co  m
    assertTrue(writer.hasDeletions()); // 1
    assertEquals(2, writer.maxDoc()); // 2
    assertEquals(1, writer.numDocs()); // 2
    writer.close();
}

From source file:com.sxc.lucene.index.IndexingTest.java

License:Apache License

public void testDeleteAfterOptimize() throws IOException {
    IndexWriter writer = getWriter();
    assertEquals(2, writer.numDocs());//from ww w .j a  v a2  s  .c o  m
    writer.deleteDocuments(new Term("id", "1"));
    writer.forceMergeDeletes(); // 3
    writer.commit();
    assertFalse(writer.hasDeletions());
    assertEquals(1, writer.maxDoc()); // C
    assertEquals(1, writer.numDocs()); // C
    writer.close();
}

From source file:com.vmware.dcp.services.common.LuceneBlobIndexService.java

License:Open Source License

private void applyBlobRetentionPolicy(Query linkQuery, long updateTime) throws IOException {
    IndexWriter wr = this.writer;
    if (wr == null) {
        return;//from w w w .  j  av  a  2s .  c  o  m
    }

    if (!this.indexOptions.contains(BlobIndexOption.SINGLE_USE_KEYS)) {
        return;
    }

    BooleanQuery bq = new BooleanQuery();
    bq.add(linkQuery, Occur.MUST);
    NumericRangeQuery<Long> timeQuery = NumericRangeQuery.newLongRange(URI_PARAM_NAME_UPDATE_TIME, null,
            updateTime, false, true);
    bq.add(timeQuery, Occur.MUST);
    wr.deleteDocuments(linkQuery);
    this.indexUpdateTimeMicros = Utils.getNowMicrosUtc();
}

From source file:com.vmware.dcp.services.common.LuceneDocumentIndexService.java

License:Open Source License

/**
 * Deletes all indexed documents with range of deleteCount,indexed with the specified self link
 *
 * @throws Throwable//w w w .  ja v a2  s. c  o m
 */
private void deleteDocumentsFromIndex(Operation delete, String link, SelfLinkInfo info, long versionsToKeep)
        throws Throwable {
    IndexWriter wr = this.writer;
    if (wr == null) {
        delete.fail(new CancellationException());
        return;
    }

    Query linkQuery = new TermQuery(new Term(ServiceDocument.FIELD_NAME_SELF_LINK, link));

    IndexSearcher s = updateSearcher(link, Integer.MAX_VALUE, wr);
    if (s == null) {
        delete.fail(new CancellationException());
        return;
    }

    TopDocs results;

    results = s.search(linkQuery, Integer.MAX_VALUE, this.versionSort, false, false);
    if (results == null) {
        return;
    }

    ScoreDoc[] hits = results.scoreDocs;

    if (hits == null || hits.length == 0) {
        return;
    }

    Document hitDoc = s.doc(hits[0].doc);

    if (versionsToKeep == 0) {
        // we are asked to delete everything, no need to sort or query
        this.selfLinks.remove(link);
        wr.deleteDocuments(linkQuery);
        this.indexUpdateTimeMicros = Utils.getNowMicrosUtc();
        delete.complete();
        return;
    }

    if (hits.length < versionsToKeep) {
        return;
    }

    BooleanQuery bq = new BooleanQuery();
    // grab the document at the tail of the results, and use it to form a new query
    // that will delete all documents from that document up to the version at the
    // retention
    // limit
    hitDoc = s.doc(hits[hits.length - 1].doc);
    long versionLowerBound = Long.parseLong(hitDoc.get(ServiceDocument.FIELD_NAME_VERSION));
    hitDoc = s.doc(hits[(int) versionsToKeep - 1].doc);
    long versionUpperBound = Long.parseLong(hitDoc.get(ServiceDocument.FIELD_NAME_VERSION));

    NumericRangeQuery<Long> versionQuery = NumericRangeQuery.newLongRange(ServiceDocument.FIELD_NAME_VERSION,
            versionLowerBound, versionUpperBound, true, true);

    bq.add(versionQuery, Occur.MUST);
    bq.add(linkQuery, Occur.MUST);
    results = s.search(bq, Integer.MAX_VALUE);
    long now = Utils.getNowMicrosUtc();
    logInfo("trimming index for %s from %d to %d, query returned %d", link, hits.length, versionsToKeep,
            results.totalHits);
    wr.deleteDocuments(bq);
    if (info != null) {
        info.updateMicros = now;
    }
    this.indexUpdateTimeMicros = now;
    delete.complete();
}

From source file:com.vmware.xenon.services.common.LuceneBlobIndexService.java

License:Open Source License

private void applyBlobRetentionPolicy(Query linkQuery, long updateTime) throws IOException {
    IndexWriter wr = this.writer;
    if (wr == null) {
        return;//  w  ww . j a v a  2s. c o m
    }

    if (!this.indexOptions.contains(BlobIndexOption.SINGLE_USE_KEYS)) {
        return;
    }

    // Query all blobs that satisfy the passed linkQuery and have
    // URI_PARAM_NAME_UPDATE_TIME field set to less than or
    // equal to updateTime
    Query timeQuery = LongPoint.newRangeQuery(URI_PARAM_NAME_UPDATE_TIME, Long.MIN_VALUE, updateTime);
    BooleanQuery.Builder builder = new BooleanQuery.Builder().add(linkQuery, Occur.MUST).add(timeQuery,
            Occur.MUST);
    wr.deleteDocuments(builder.build());
    this.indexUpdateTimeMicros = Utils.getNowMicrosUtc();
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexBackupService.java

License:Open Source License

private void performTimeSnapshotRecovery(Long timeSnapshotBoundaryMicros, IndexWriter newWriter)
        throws IOException {

    // For documents with metadata indexing enabled, the version which was current at
    // the restore time may have subsequently been marked as not current. Update the
    // current field for any such documents.

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(newWriter, true, true));

    Query updateTimeQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS,
            timeSnapshotBoundaryMicros + 1, Long.MAX_VALUE);

    Sort selfLinkSort = new Sort(new SortField(
            LuceneIndexDocumentHelper.createSortFieldPropertyName(ServiceDocument.FIELD_NAME_SELF_LINK),
            SortField.Type.STRING));

    final int pageSize = 10000;

    Set<String> prevPageLinks = new HashSet<>();
    ScoreDoc after = null;/* w w w .j a v  a 2s  .c om*/
    while (true) {
        TopDocs results = searcher.searchAfter(after, updateTimeQuery, pageSize, selfLinkSort, false, false);
        if (results == null || results.scoreDocs == null || results.scoreDocs.length == 0) {
            break;
        }

        Set<String> pageLinks = new HashSet<>();
        DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
        for (ScoreDoc sd : results.scoreDocs) {
            visitor.reset(ServiceDocument.FIELD_NAME_SELF_LINK);
            searcher.doc(sd.doc, visitor);
            if (prevPageLinks.contains(visitor.documentSelfLink)) {
                pageLinks.add(visitor.documentSelfLink);
                continue;
            }

            if (!pageLinks.add(visitor.documentSelfLink)) {
                continue;
            }

            updateCurrentAttributeForSelfLink(searcher, timeSnapshotBoundaryMicros, visitor.documentSelfLink,
                    newWriter);
        }

        if (results.scoreDocs.length < pageSize) {
            break;
        }

        after = results.scoreDocs[results.scoreDocs.length - 1];
        prevPageLinks = pageLinks;
    }

    // Now that metadata indexing attributes have been updated appropriately, delete any
    // documents which were created after the restore point.
    Query luceneQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS,
            timeSnapshotBoundaryMicros + 1, Long.MAX_VALUE);
    newWriter.deleteDocuments(luceneQuery);
}