Example usage for org.apache.lucene.document LongPoint newRangeQuery

List of usage examples for org.apache.lucene.document LongPoint newRangeQuery

Introduction

In this page you can find the example usage for org.apache.lucene.document LongPoint newRangeQuery.

Prototype

public static Query newRangeQuery(String field, long[] lowerValue, long[] upperValue) 

Source Link

Document

Create a range query for n-dimensional long values.

Usage

From source file:com.b2international.snowowl.snomed.api.impl.ClassificationRunIndex.java

License:Apache License

public void trimIndex(int maximumResultsToKeep) throws IOException {
    final Query query = Fields.newQuery().field(FIELD_CLASS, ClassificationRun.class.getSimpleName())
            .matchAll();//w  w w  .j  av  a 2s.co m

    // Sort by decreasing document order
    final Sort sort = new Sort(new SortField(null, Type.DOC, true));

    final ClassificationRun lastRunToKeep = Iterables
            .getFirst(search(query, ClassificationRun.class, sort, maximumResultsToKeep - 1, 1), null);
    if (lastRunToKeep == null) {
        return;
    }

    final Date lastCreationDate = lastRunToKeep.getCreationDate();
    final Query trimmingQuery = LongPoint.newRangeQuery(FIELD_CREATION_DATE, Long.MIN_VALUE,
            lastCreationDate.getTime());
    writer.deleteDocuments(trimmingQuery);
    commit();
}

From source file:com.czw.search.lucene.example.facet.RangeFacetsExample.java

License:Apache License

/** User drills down on the specified range. */
public TopDocs drillDown(LongRange range) throws IOException {

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(getConfig());

    q.add("timestamp", LongPoint.newRangeQuery("timestamp", range.min, range.max));
    return searcher.search(q, 10);
}

From source file:com.czw.search.lucene.example.facet.RangeFacetsExample.java

License:Apache License

/** User drills down on the specified range, and also computes drill sideways counts. */
public DrillSideways.DrillSidewaysResult drillSideways(LongRange range) throws IOException {
    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(getConfig());
    q.add("timestamp", LongPoint.newRangeQuery("timestamp", range.min, range.max));

    // DrillSideways only handles taxonomy and sorted set drill facets by default; to do range facets we must subclass and override the
    // buildFacetsResult method.
    DrillSideways.DrillSidewaysResult result = new DrillSideways(searcher, getConfig(), null, null) {
        @Override//from   ww  w.j av a  2s .  c  o  m
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
                String[] drillSidewaysDims) throws IOException {
            // If we had other dims we would also compute their drill-down or drill-sideways facets here:
            assert drillSidewaysDims[0].equals("timestamp");
            return new LongRangeFacetCounts("timestamp", drillSideways[0], PAST_HOUR, PAST_SIX_HOURS, PAST_DAY);
        }
    }.search(q, 10);

    return result;
}

From source file:com.semantic.model.filter.OFixedFileDateFilter.java

@Override
public Query createQuery() {
    Calendar[] dates = generate(date);
    /* we dont cache query because of the dates */
    return LongPoint.newRangeQuery(getLuceneField(), dates[0].getTimeInMillis(), dates[1].getTimeInMillis());
}

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;//from w w w .  ja  va  2  s .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;/*from  ww  w . j  ava2s .  c o m*/
    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);
}

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

License:Open Source License

private void updateCurrentAttributeForSelfLink(IndexSearcher searcher, long timeSnapshotBoundaryMicros,
        String selfLink, IndexWriter newWriter) throws IOException {

    Query selfLinkClause = new TermQuery(new Term(ServiceDocument.FIELD_NAME_SELF_LINK, selfLink));
    Query updateTimeClause = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, 0,
            timeSnapshotBoundaryMicros);
    Query booleanQuery = new BooleanQuery.Builder().add(selfLinkClause, Occur.MUST)
            .add(updateTimeClause, Occur.MUST).build();

    Sort versionSort = new Sort(
            new SortedNumericSortField(ServiceDocument.FIELD_NAME_VERSION, SortField.Type.LONG, true));

    TopDocs results = searcher.search(booleanQuery, 1, versionSort, false, false);
    if (results == null || results.scoreDocs == null || results.scoreDocs.length == 0) {
        return;/*  w  ww .  ja  v a  2  s .  c o m*/
    }

    DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
    visitor.reset(LuceneIndexDocumentHelper.FIELD_NAME_INDEXING_ID);
    searcher.doc(results.scoreDocs[0].doc, visitor);
    if (visitor.documentIndexingId == null) {
        return;
    }

    Term indexingIdTerm = new Term(LuceneIndexDocumentHelper.FIELD_NAME_INDEXING_ID,
            visitor.documentIndexingId);
    newWriter.updateNumericDocValue(indexingIdTerm,
            LuceneIndexDocumentHelper.FIELD_NAME_INDEXING_METADATA_VALUE_TOMBSTONE_TIME,
            LuceneIndexDocumentHelper.ACTIVE_DOCUMENT_TOMBSTONE_TIME);
}

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

License:Open Source License

/**
 * Find the document given a self link and version number.
 *
 * This function is used for two purposes; find given version to...
 * 1) load state if the service state is not yet cached
 * 2) filter query results to only include the given version
 *
 * In case (1), authorization is applied in the service host (either against
 * the cached state or freshly loaded state).
 * In case (2), authorization should NOT be applied because the original query
 * already included the resource group query per the authorization context.
 * Query results will be filtered given the REAL latest version, not the latest
 * version subject to the resource group query. This means older versions of
 * a document will NOT appear in the query result if the user is not authorized
 * to see the newer version./* w  w w .  ja  va2s.c om*/
 *
 * If given version is null then function returns the latest version.
 * And if given version is not found then no document is returned.
 */
private TopDocs searchByVersion(String selfLink, IndexSearcher s, Long version) throws IOException {
    Query tqSelfLink = new TermQuery(new Term(ServiceDocument.FIELD_NAME_SELF_LINK, selfLink));

    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(tqSelfLink, Occur.MUST);

    if (version != null) {
        Query versionQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_VERSION, version, version);
        builder.add(versionQuery, Occur.MUST);
    }

    TopDocs hits = s.search(builder.build(), 1, this.versionSort, false, false);
    return hits;
}

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

License:Open Source License

/**
 * Deletes all indexed documents with range of deleteCount,indexed with the specified self link
 *
 * @throws Throwable/*from  w w  w .  j  a  va2 s. c  o m*/
 */
private void deleteDocumentsFromIndex(Operation delete, String link, 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;

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

    int versionCount = hits.length;

    hitDoc = s.doc(hits[versionCount - 1].doc);
    long versionLowerBound = Long.parseLong(hitDoc.get(ServiceDocument.FIELD_NAME_VERSION));

    hitDoc = s.doc(hits[0].doc);
    long versionUpperBound = Long.parseLong(hitDoc.get(ServiceDocument.FIELD_NAME_VERSION));

    // If the number of versions found are already less than the limit
    // then there is nothing to delete. Just exit.
    if (versionCount <= versionsToKeep) {
        return;
    }

    BooleanQuery.Builder builder = new BooleanQuery.Builder();

    // 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[(int) versionsToKeep].doc);
    long cutOffVersion = Long.parseLong(hitDoc.get(ServiceDocument.FIELD_NAME_VERSION));

    Query versionQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_VERSION, versionLowerBound,
            cutOffVersion);

    builder.add(versionQuery, Occur.MUST);
    builder.add(linkQuery, Occur.MUST);
    BooleanQuery bq = builder.build();

    results = s.search(bq, Integer.MAX_VALUE);

    logInfo("Version grooming for %s found %d versions from %d to %d. Trimming %d versions from %d to %d", link,
            versionCount, versionLowerBound, versionUpperBound, results.scoreDocs.length, versionLowerBound,
            cutOffVersion);

    wr.deleteDocuments(bq);

    // We have observed that sometimes Lucene search does not return all the document
    // versions in the index. Normally, the number of documents returned should be
    // equal to or more than the delta between the lower and upper versions. It can be more
    // because of duplicate document versions. If that's not the case, we add the
    // link back for retention so that the next grooming run can cleanup the missed document.
    if (versionCount < versionUpperBound - versionLowerBound + 1) {
        logWarning(
                "Adding %s back for version grooming since versionCount %d "
                        + "was lower than version delta from %d to %d.",
                link, versionCount, versionLowerBound, versionUpperBound);
        synchronized (this.linkDocumentRetentionEstimates) {
            this.linkDocumentRetentionEstimates.put(link, versionsToKeep);
        }
    }

    long now = Utils.getNowMicrosUtc();

    // Use time AFTER index was updated to be sure that it can be compared
    // against the time the searcher was updated and have this change
    // be reflected in the new searcher. If the start time would be used,
    // it is possible to race with updating the searcher and NOT have this
    // change be reflected in the searcher.
    updateLinkAccessTime(now, link);

    delete.complete();
}

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

License:Open Source License

private void applyDocumentExpirationPolicy(IndexWriter w) throws Throwable {
    // if we miss a document update, we will catch it, and refresh the searcher on the
    // next update or maintenance
    IndexSearcher s = this.searcher != null ? this.searcher : updateSearcher(null, Integer.MAX_VALUE, w);
    if (s == null) {
        return;/*  w w w .  j  a  v a  2  s . c o  m*/
    }

    long expirationUpperBound = Utils.getNowMicrosUtc();

    Query versionQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_EXPIRATION_TIME_MICROS, 1L,
            expirationUpperBound);

    TopDocs results = s.search(versionQuery, EXPIRED_DOCUMENT_SEARCH_THRESHOLD);
    if (results.totalHits == 0) {
        return;
    }

    // The expiration query will return all versions for a link. Use a set so we only delete once per link
    Set<String> links = new HashSet<>();
    long now = Utils.getNowMicrosUtc();
    for (ScoreDoc sd : results.scoreDocs) {
        Document d = s.getIndexReader().document(sd.doc, this.fieldsToLoadNoExpand);
        String link = d.get(ServiceDocument.FIELD_NAME_SELF_LINK);
        IndexableField versionField = d.getField(ServiceDocument.FIELD_NAME_VERSION);
        long versionExpired = versionField.numericValue().longValue();
        long latestVersion = this.getLatestVersion(s, link);
        if (versionExpired < latestVersion) {
            continue;
        }
        if (!links.add(link)) {
            continue;
        }
        checkAndDeleteExpiratedDocuments(link, s, sd.doc, d, now);
    }

    // More documents to be expired trigger maintenance right away.
    if (results.totalHits > EXPIRED_DOCUMENT_SEARCH_THRESHOLD) {
        adjustStat(STAT_NAME_DOCUMENT_EXPIRATION_FORCED_MAINTENANCE_COUNT, 1);
        ServiceMaintenanceRequest body = ServiceMaintenanceRequest.create();
        Operation servicePost = Operation.createPost(UriUtils.buildUri(getHost(), getSelfLink()))
                .setReferer(getHost().getUri()).setBody(body);
        // servicePost can be cached
        handleMaintenance(servicePost);
    }
}