Example usage for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS

List of usage examples for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS

Introduction

In this page you can find the example usage for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS.

Prototype

int NO_MORE_DOCS

To view the source code for org.apache.lucene.search DocIdSetIterator NO_MORE_DOCS.

Click Source Link

Document

When returned by #nextDoc() , #advance(int) and #docID() it means there are no more docs in the iterator.

Usage

From source file:org.elasticsearch.common.lucene.search.XTermsFilter.java

License:Apache License

@Override
public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException {
    final AtomicReader reader = context.reader();
    FixedBitSet result = null; // lazy init if needed - no need to create a big bitset ahead of time
    final Fields fields = reader.fields();
    final BytesRef spare = new BytesRef(this.termsBytes);
    if (fields == null) {
        return result;
    }//w w  w. j  a v  a2 s  .c  o m
    Terms terms = null;
    TermsEnum termsEnum = null;
    DocsEnum docs = null;
    for (TermsAndField termsAndField : this.termsAndFields) {
        if ((terms = fields.terms(termsAndField.field)) != null) {
            termsEnum = terms.iterator(termsEnum); // this won't return null
            for (int i = termsAndField.start; i < termsAndField.end; i++) {
                spare.offset = offsets[i];
                spare.length = offsets[i + 1] - offsets[i];
                if (termsEnum.seekExact(spare, false)) { // don't use cache since we could pollute the cache here easily
                    docs = termsEnum.docs(acceptDocs, docs, 0); // no freq since we don't need them
                    if (result == null) {
                        if (docs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                            result = new FixedBitSet(reader.maxDoc());
                            // lazy init but don't do it in the hot loop since we could read many docs
                            result.set(docs.docID());
                        }
                    }
                    while (docs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
                        result.set(docs.docID());
                    }
                }
            }
        }
    }
    return result;
}

From source file:org.elasticsearch.common.lucene.uid.PerThreadIDAndVersionLookup.java

License:Apache License

/** Return null if id is not found. */
public DocIdAndVersion lookup(BytesRef id, Bits liveDocs, LeafReaderContext context) throws IOException {
    if (termsEnum.seekExact(id)) {
        if (versions != null || hasPayloads == false) {
            // Use NDV to retrieve the version, in which case we only need PostingsEnum:

            // there may be more than one matching docID, in the case of nested docs, so we want the last one:
            docsEnum = termsEnum.postings(docsEnum, 0);
            int docID = DocIdSetIterator.NO_MORE_DOCS;
            for (int d = docsEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = docsEnum.nextDoc()) {
                if (liveDocs != null && liveDocs.get(d) == false) {
                    continue;
                }/*  w  ww. j a va 2 s.c  o m*/
                docID = d;
            }

            if (docID != DocIdSetIterator.NO_MORE_DOCS) {
                if (versions != null) {
                    return new DocIdAndVersion(docID, versions.get(docID), context);
                } else {
                    // _uid found, but no doc values and no payloads
                    return new DocIdAndVersion(docID, Versions.NOT_SET, context);
                }
            }
        }

        // ... but used to be stored as payloads; in this case we must use PostingsEnum
        posEnum = termsEnum.postings(posEnum, PostingsEnum.PAYLOADS);
        assert posEnum != null; // terms has payloads
        for (int d = posEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = posEnum.nextDoc()) {
            if (liveDocs != null && liveDocs.get(d) == false) {
                continue;
            }
            posEnum.nextPosition();
            final BytesRef payload = posEnum.getPayload();
            if (payload != null && payload.length == 8) {
                // TODO: does this break the nested docs case?  we are not returning the last matching docID here?
                return new DocIdAndVersion(d, Numbers.bytesToLong(payload), context);
            }
        }
    }

    return null;
}

From source file:org.elasticsearch.common.lucene.uid.PerThreadIDVersionAndSeqNoLookup.java

License:Apache License

/** Return null if id is not found.
 * We pass the {@link LeafReaderContext} as an argument so that things
 * still work with reader wrappers that hide some documents while still
 * using the same cache key. Otherwise we'd have to disable caching
 * entirely for these readers.//from  w w  w .j a v  a 2s  . c  o  m
 */
public DocIdAndVersion lookupVersion(BytesRef id, LeafReaderContext context) throws IOException {
    assert context.reader().getCoreCacheHelper().getKey()
            .equals(readerKey) : "context's reader is not the same as the reader class was initialized on.";
    int docID = getDocID(id, context.reader().getLiveDocs());

    if (docID != DocIdSetIterator.NO_MORE_DOCS) {
        final NumericDocValues versions = context.reader().getNumericDocValues(VersionFieldMapper.NAME);
        if (versions == null) {
            throw new IllegalArgumentException("reader misses the [" + VersionFieldMapper.NAME + "] field");
        }
        if (versions.advanceExact(docID) == false) {
            throw new IllegalArgumentException(
                    "Document [" + docID + "] misses the [" + VersionFieldMapper.NAME + "] field");
        }
        return new DocIdAndVersion(docID, versions.longValue(), context);
    } else {
        return null;
    }
}

From source file:org.elasticsearch.common.lucene.uid.PerThreadIDVersionAndSeqNoLookup.java

License:Apache License

/**
 * returns the internal lucene doc id for the given id bytes.
 * {@link DocIdSetIterator#NO_MORE_DOCS} is returned if not found
 * *//*from www  .j  a v a  2  s.c  om*/
private int getDocID(BytesRef id, Bits liveDocs) throws IOException {
    if (termsEnum.seekExact(id)) {
        int docID = DocIdSetIterator.NO_MORE_DOCS;
        // there may be more than one matching docID, in the case of nested docs, so we want the last one:
        docsEnum = termsEnum.postings(docsEnum, 0);
        for (int d = docsEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = docsEnum.nextDoc()) {
            if (liveDocs != null && liveDocs.get(d) == false) {
                continue;
            }
            docID = d;
        }
        return docID;
    } else {
        return DocIdSetIterator.NO_MORE_DOCS;
    }
}

From source file:org.elasticsearch.common.lucene.uid.PerThreadIDVersionAndSeqNoLookup.java

License:Apache License

/** Return null if id is not found. */
DocIdAndSeqNo lookupSeqNo(BytesRef id, LeafReaderContext context) throws IOException {
    assert context.reader().getCoreCacheHelper().getKey()
            .equals(readerKey) : "context's reader is not the same as the reader class was initialized on.";
    int docID = getDocID(id, context.reader().getLiveDocs());
    if (docID != DocIdSetIterator.NO_MORE_DOCS) {
        NumericDocValues seqNos = context.reader().getNumericDocValues(SeqNoFieldMapper.NAME);
        long seqNo;
        if (seqNos != null && seqNos.advanceExact(docID)) {
            seqNo = seqNos.longValue();/*  w w w .  j  a v a2s.c o m*/
        } else {
            seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
        }
        return new DocIdAndSeqNo(docID, seqNo, context);
    } else {
        return null;
    }
}

From source file:org.elasticsearch.common.lucene.uid.VersionsResolver.java

License:Apache License

/**
 * Returns the sequence number for the given uid term, returning
 * {@code SequenceNumbersService.UNASSIGNED_SEQ_NO} if none is found.
 *//*  ww  w  .j  a v a 2s . com*/
public static long loadSeqNo(IndexReader reader, Term term) throws IOException {
    assert term.field().equals(UidFieldMapper.NAME) : "can only load _seq_no by uid";
    List<LeafReaderContext> leaves = reader.leaves();
    if (leaves.isEmpty()) {
        return SequenceNumbersService.UNASSIGNED_SEQ_NO;
    }

    // iterate backwards to optimize for the frequently updated documents
    // which are likely to be in the last segments
    for (int i = leaves.size() - 1; i >= 0; i--) {
        LeafReader leaf = leaves.get(i).reader();
        Bits liveDocs = leaf.getLiveDocs();

        TermsEnum termsEnum = null;
        SortedNumericDocValues dvField = null;
        PostingsEnum docsEnum = null;

        final Fields fields = leaf.fields();
        if (fields != null) {
            Terms terms = fields.terms(UidFieldMapper.NAME);
            if (terms != null) {
                termsEnum = terms.iterator();
                assert termsEnum != null;
                dvField = leaf.getSortedNumericDocValues(SeqNoFieldMapper.NAME);
                assert dvField != null;

                final BytesRef id = term.bytes();
                if (termsEnum.seekExact(id)) {
                    // there may be more than one matching docID, in the
                    // case of nested docs, so we want the last one:
                    docsEnum = termsEnum.postings(docsEnum, 0);
                    int docID = DocIdSetIterator.NO_MORE_DOCS;
                    for (int d = docsEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = docsEnum
                            .nextDoc()) {
                        if (liveDocs != null && liveDocs.get(d) == false) {
                            continue;
                        }
                        docID = d;
                    }

                    if (docID != DocIdSetIterator.NO_MORE_DOCS) {
                        dvField.setDocument(docID);
                        assert dvField.count() == 1 : "expected only a single value for _seq_no but got "
                                + dvField.count();
                        return dvField.valueAt(0);
                    }
                }
            }
        }

    }
    return SequenceNumbersService.UNASSIGNED_SEQ_NO;
}

From source file:org.elasticsearch.common.lucene.uid.VersionsResolver.java

License:Apache License

/**
 * Returns the primary term for the given uid term, returning {@code 0} if none is found.
 *//*from   w  w w .  j  a va  2  s  . co m*/
public static long loadPrimaryTerm(IndexReader reader, Term term) throws IOException {
    assert term.field().equals(UidFieldMapper.NAME) : "can only load _primary_term by uid";
    List<LeafReaderContext> leaves = reader.leaves();
    if (leaves.isEmpty()) {
        return 0;
    }

    // iterate backwards to optimize for the frequently updated documents
    // which are likely to be in the last segments
    for (int i = leaves.size() - 1; i >= 0; i--) {
        LeafReader leaf = leaves.get(i).reader();
        Bits liveDocs = leaf.getLiveDocs();

        TermsEnum termsEnum = null;
        NumericDocValues dvField = null;
        PostingsEnum docsEnum = null;

        final Fields fields = leaf.fields();
        if (fields != null) {
            Terms terms = fields.terms(UidFieldMapper.NAME);
            if (terms != null) {
                termsEnum = terms.iterator();
                assert termsEnum != null;
                dvField = leaf.getNumericDocValues(SeqNoFieldMapper.PRIMARY_TERM_NAME);
                assert dvField != null;

                final BytesRef id = term.bytes();
                if (termsEnum.seekExact(id)) {
                    // there may be more than one matching docID, in the
                    // case of nested docs, so we want the last one:
                    docsEnum = termsEnum.postings(docsEnum, 0);
                    int docID = DocIdSetIterator.NO_MORE_DOCS;
                    for (int d = docsEnum.nextDoc(); d != DocIdSetIterator.NO_MORE_DOCS; d = docsEnum
                            .nextDoc()) {
                        if (liveDocs != null && liveDocs.get(d) == false) {
                            continue;
                        }
                        docID = d;
                    }

                    if (docID != DocIdSetIterator.NO_MORE_DOCS) {
                        return dvField.get(docID);
                    }
                }
            }
        }

    }
    return 0;
}

From source file:org.elasticsearch.index.cache.filter.support.FilterCacheValue.java

License:Apache License

public static DocSet cacheable(IndexReader reader, @Nullable LongsLAB longsLAB, DocIdSet set)
        throws IOException {
    if (set == null) {
        return DocSet.EMPTY_DOC_SET;
    }/*from w w w.j ava  2s.com*/
    if (set == DocIdSet.EMPTY_DOCIDSET) {
        return DocSet.EMPTY_DOC_SET;
    }

    DocIdSetIterator it = set.iterator();
    if (it == null) {
        return DocSet.EMPTY_DOC_SET;
    }
    int doc = it.nextDoc();
    if (doc == DocIdSetIterator.NO_MORE_DOCS) {
        return DocSet.EMPTY_DOC_SET;
    }

    // we have a LAB, check if can be used...
    if (longsLAB == null) {
        return DocSets.cacheable(reader, set);
    }

    int numOfWords = OpenBitSet.bits2words(reader.maxDoc());
    LongsLAB.Allocation allocation = longsLAB.allocateLongs(numOfWords);
    if (allocation == null) {
        return DocSets.cacheable(reader, set);
    }
    // we have an allocation, use it to create SlicedOpenBitSet
    if (set instanceof OpenBitSet) {
        return new SlicedOpenBitSet(allocation.getData(), allocation.getOffset(), (OpenBitSet) set);
    } else if (set instanceof OpenBitDocSet) {
        return new SlicedOpenBitSet(allocation.getData(), allocation.getOffset(), ((OpenBitDocSet) set).set());
    } else {
        SlicedOpenBitSet slicedSet = new SlicedOpenBitSet(allocation.getData(), numOfWords,
                allocation.getOffset());
        slicedSet.fastSet(doc); // we already have an open iterator, so use it, and don't forget to set the initial one
        while ((doc = it.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            slicedSet.fastSet(doc);
        }
        return slicedSet;
    }
}

From source file:org.elasticsearch.index.cache.fixedbitset.FixedBitSetFilterCache.java

License:Apache License

private FixedBitSet getAndLoadIfNotPresent(final Filter filter, final AtomicReaderContext context)
        throws IOException, ExecutionException {
    final Object coreCacheReader = context.reader().getCoreCacheKey();
    final ShardId shardId = ShardUtils.extractShardId(context.reader());
    Cache<Filter, Value> filterToFbs = loadedFilters.get(coreCacheReader, new Callable<Cache<Filter, Value>>() {
        @Override/*from   ww  w.  j  a  v a 2s .com*/
        public Cache<Filter, Value> call() throws Exception {
            SegmentReaderUtils.registerCoreListener(context.reader(), FixedBitSetFilterCache.this);
            return CacheBuilder.newBuilder().build();
        }
    });
    return filterToFbs.get(filter, new Callable<Value>() {
        @Override
        public Value call() throws Exception {
            DocIdSet docIdSet = filter.getDocIdSet(context, null);
            final FixedBitSet fixedBitSet;
            if (docIdSet instanceof FixedBitSet) {
                fixedBitSet = (FixedBitSet) docIdSet;
            } else {
                fixedBitSet = new FixedBitSet(context.reader().maxDoc());
                if (docIdSet != null && docIdSet != DocIdSet.EMPTY) {
                    DocIdSetIterator iterator = docIdSet.iterator();
                    if (iterator != null) {
                        int doc = iterator.nextDoc();
                        if (doc != DocIdSetIterator.NO_MORE_DOCS) {
                            do {
                                fixedBitSet.set(doc);
                                doc = iterator.nextDoc();
                            } while (doc != DocIdSetIterator.NO_MORE_DOCS);
                        }
                    }
                }
            }

            Value value = new Value(fixedBitSet, shardId);
            if (shardId != null) {
                IndexShard shard = indexService.shard(shardId.id());
                if (shard != null) {
                    shard.shardFixedBitSetFilterCache().onCached(value.fixedBitSet.ramBytesUsed());
                }
            }
            return value;
        }
    }).fixedBitSet;
}

From source file:org.elasticsearch.index.percolator.PercolatorQueryCache.java

License:Apache License

QueriesLeaf loadQueries(LeafReaderContext context, IndexShard indexShard) throws IOException {
    Version indexVersionCreated = indexShard.indexSettings().getIndexVersionCreated();
    MapperService mapperService = indexShard.mapperService();
    LeafReader leafReader = context.reader();
    ShardId shardId = ShardUtils.extractShardId(leafReader);
    if (shardId == null) {
        throw new IllegalStateException("can't resolve shard id");
    }/*from w ww  .  ja  v a2  s.  co m*/
    if (indexSettings.getIndex().equals(shardId.getIndex()) == false) {
        // percolator cache insanity
        String message = "Trying to load queries for index " + shardId.getIndex() + " with cache of index "
                + indexSettings.getIndex();
        throw new IllegalStateException(message);
    }

    IntObjectHashMap<Query> queries = new IntObjectHashMap<>();
    boolean legacyLoading = indexVersionCreated.before(Version.V_5_0_0_alpha1);
    if (legacyLoading) {
        PostingsEnum postings = leafReader.postings(new Term(TypeFieldMapper.NAME, LEGACY_TYPE_NAME),
                PostingsEnum.NONE);
        if (postings != null) {
            LegacyQueryFieldVisitor visitor = new LegacyQueryFieldVisitor();
            for (int docId = postings.nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = postings
                    .nextDoc()) {
                leafReader.document(docId, visitor);
                queries.put(docId, parseLegacyPercolatorDocument(docId, visitor.source));
                visitor.source = null; // reset
            }
        }
    } else {
        // Each type can have one percolator field mapper,
        // So for each type we check if there is a percolator field mapper
        // and parse all the queries for the documents of that type.
        IndexSearcher indexSearcher = new IndexSearcher(leafReader);
        for (DocumentMapper documentMapper : mapperService.docMappers(false)) {
            Weight queryWeight = indexSearcher.createNormalizedWeight(documentMapper.typeFilter(), false);
            for (FieldMapper fieldMapper : documentMapper.mappers()) {
                if (fieldMapper instanceof PercolatorFieldMapper) {
                    PercolatorFieldType fieldType = (PercolatorFieldType) fieldMapper.fieldType();
                    BinaryDocValues binaryDocValues = leafReader
                            .getBinaryDocValues(fieldType.getQueryBuilderFieldName());
                    if (binaryDocValues != null) {
                        // use the same leaf reader context the indexSearcher is using too:
                        Scorer scorer = queryWeight.scorer(leafReader.getContext());
                        if (scorer != null) {
                            DocIdSetIterator iterator = scorer.iterator();
                            for (int docId = iterator
                                    .nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = iterator
                                            .nextDoc()) {
                                BytesRef qbSource = binaryDocValues.get(docId);
                                if (qbSource.length > 0) {
                                    queries.put(docId, parseQueryBuilder(docId, qbSource));
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    leafReader.addCoreClosedListener(this);
    return new QueriesLeaf(shardId, queries);
}