private void loadNextValue( LeafReaderContext context, int docID ) { NumericDocValues docValues; if ( docValuesCache.containsKey( context ) ) { docValues = docValuesCache.get( context ); } else { try { docValues = context.reader().getNumericDocValues( field ); docValuesCache.put( context, docValues ); } catch ( IOException e ) { throw new RuntimeException( e ); } } if ( docValues != null ) { currentValue = docValues.get( docID ); } else { currentValue = -1; } } }
@Override public boolean advanceExact(int targetDocID) throws IOException { if (targetDocID < docID) { throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID); } int readerIndex = ReaderUtil.subIndex(targetDocID, leaves); if (readerIndex >= nextLeaf) { if (readerIndex == leaves.size()) { throw new IllegalArgumentException("Out of range: " + targetDocID); } currentLeaf = leaves.get(readerIndex); currentValues = currentLeaf.reader().getNumericDocValues(field); nextLeaf = readerIndex+1; } docID = targetDocID; if (currentValues == null) { return false; } return currentValues.advanceExact(targetDocID - currentLeaf.docBase); } @Override
private long getValueForDoc(int doc) throws IOException { if (currentReaderValues.advanceExact(doc)) { return currentReaderValues.longValue(); } else { return missingValue; } }
private static void checkNumericDocValues(String fieldName, NumericDocValues ndv) throws IOException { int doc; if (ndv.docID() != -1) { throw new RuntimeException("dv iterator for field: " + fieldName + " should start at docID=-1, but got " + ndv.docID()); } // TODO: we could add stats to DVs, e.g. total doc count w/ a value for this field while ((doc = ndv.nextDoc()) != NO_MORE_DOCS) { ndv.longValue(); } }
Term qTerm = new Term(fieldToQuery, queryTokens.get(qt)); Terms terms = leafReaderContext.reader().terms(fieldToQuery); if (te.seekExact(qTerms[qt].bytes())) { // if the query term is in the term vocabulary NumericDocValues norms = leafReaderContext.reader().getNormValues(fieldToQuery); for (int doc = 0; doc < leafReaderContext.reader().numDocs(); doc++) { long docLength = norms.get(doc);
return (int)cachedFieldLengths.get(doc); Document document = reader.document(doc); String strLength = document.get(lengthTokensFieldName); lookedForLengthField = true; if (strLength != null) { Terms vector = reader.getTermVector(doc, fieldName); TermsEnum termsEnum = vector.iterator(); int termFreq = 0;
/** * Get the value of our field in the specified document. * * @param doc the document * @return value of the int field */ public int getFieldValue(int doc) { // Cached doc values? if (docValues != null) { return (int)docValues.get(doc); } // No; get the field value from the Document object. // (Note that this code should never be executed, but just to be safe) Document document; try { document = reader.document(doc); } catch (Exception e) { throw new RuntimeException(e); } String strVal = document.get(intFieldName); if (strVal != null) { // Yes, found the field length stored in the index. // Parse and return it. return Integer.parseInt(strVal); } throw new RuntimeException("Can't find " + intFieldName + " for doc " + doc); } }
public long get(int docId) { if (cachedFiids != null) { // Find the fiid in the correct segment Entry<Integer, NumericDocValues> prev = null; for (Entry<Integer, NumericDocValues> e: cachedFiids.entrySet()) { Integer docBase = e.getKey(); if (docBase > docId) { // Previous segment (the highest docBase lower than docId) is the right one Integer prevDocBase = prev.getKey(); NumericDocValues prevDocValues = prev.getValue(); return prevDocValues.get(docId - prevDocBase); } prev = e; } // Last segment is the right one Integer prevDocBase = prev.getKey(); NumericDocValues prevDocValues = prev.getValue(); return prevDocValues.get(docId - prevDocBase); } // Not cached; find fiid by reading stored value from Document now try { return Integer.parseInt(reader.document(docId).get(fiidFieldName)); } catch (Exception e) { throw new RuntimeException(e); } }
final Query query = LongPoint.newRangeQuery(SeqNoFieldMapper.NAME, fromSeqNo, toSeqNo); final Weight weight = searcher.createWeight(query, false, 1.0f); for (LeafReaderContext leaf : reader.leaves()) { final Scorer scorer = weight.scorer(leaf); if (scorer == null) { final NumericDocValues seqNoDocValues = leaf.reader().getNumericDocValues(SeqNoFieldMapper.NAME); int docId; while ((docId = docIdSetIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { if (seqNoDocValues == null || seqNoDocValues.advanceExact(docId) == false) { throw new IllegalStateException("seq_no doc_values not found for doc_id=" + docId); final long seqNo = seqNoDocValues.longValue(); assert fromSeqNo <= seqNo && seqNo <= toSeqNo : "from_seq_no=" + fromSeqNo + " seq_no=" + seqNo + " to_seq_no=" + toSeqNo; onNewSeqNo.accept(seqNo);
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 (versions.advanceExact(docID) == false) { throw new IllegalArgumentException("Document [" + docID + "] misses the [" + VersionFieldMapper.NAME + "] field"); NumericDocValues seqNos = context.reader().getNumericDocValues(SeqNoFieldMapper.NAME); if (seqNos != null && seqNos.advanceExact(docID)) { seqNo = seqNos.longValue(); } else { seqNo = UNASSIGNED_SEQ_NO; NumericDocValues terms = context.reader().getNumericDocValues(SeqNoFieldMapper.PRIMARY_TERM_NAME); if (terms != null && terms.advanceExact(docID)) { term = terms.longValue(); } else { term = UNASSIGNED_PRIMARY_TERM; term = UNASSIGNED_PRIMARY_TERM; return new DocIdAndVersion(docID, versions.longValue(), seqNo, term, context.reader(), context.docBase); } else { return null;
@Override public int nextDoc() throws IOException { while (true) { while (currentValues == null) { if (nextLeaf == leaves.size()) { docID = NO_MORE_DOCS; return docID; } currentLeaf = leaves.get(nextLeaf); currentValues = currentLeaf.reader().getNumericDocValues(field); nextLeaf++; } int newDocID = currentValues.nextDoc(); if (newDocID == NO_MORE_DOCS) { currentValues = null; continue; } else { docID = currentLeaf.docBase + newDocID; return docID; } } }
@Override public boolean advanceExact(int targetDocID) throws IOException { if (targetDocID < docID) { throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID); } int readerIndex = ReaderUtil.subIndex(targetDocID, leaves); if (readerIndex >= nextLeaf) { if (readerIndex == leaves.size()) { throw new IllegalArgumentException("Out of range: " + targetDocID); } currentLeaf = leaves.get(readerIndex); currentValues = currentLeaf.reader().getNormValues(field); nextLeaf = readerIndex+1; } docID = targetDocID; if (currentValues == null) { return false; } return currentValues.advanceExact(targetDocID - currentLeaf.docBase); }
/** * Returns the value of the <code>weightField</code> for the current document. * Retrieves the value for the <code>weightField</code> if it's stored (using <code>doc</code>) * or if it's indexed as {@link NumericDocValues} (using <code>docId</code>) for the document. * If no value is found, then the weight is 0. */ protected long getWeight(Document doc, int docId) { IndexableField weight = doc.getField(weightField); if (weight != null) { // found weight as stored return (weight.numericValue() != null) ? weight.numericValue().longValue() : 0; } else if (weightValues != null) { // found weight as NumericDocValue return weightValues.get(docId); } else { // fall back return 0; } }
@Override public int nextDoc() throws IOException { while (true) { if (currentValues == null) { if (nextLeaf == leaves.size()) { docID = NO_MORE_DOCS; return docID; } currentLeaf = leaves.get(nextLeaf); currentValues = currentLeaf.reader().getNormValues(field); nextLeaf++; continue; } int newDocID = currentValues.nextDoc(); if (newDocID == NO_MORE_DOCS) { currentValues = null; continue; } else { docID = currentLeaf.docBase + newDocID; return docID; } } }
@Override public void collect(int doc) { try { FieldsVisitor fieldsVisitor = new FieldsVisitor(false); context.reader().document(doc, fieldsVisitor); Uid uid = fieldsVisitor.uid(); final long version = versions == null ? Versions.NOT_FOUND : versions.get(doc); docsToPurge.add(new DocToPurge(uid.type(), uid.id(), version, fieldsVisitor.routing())); } catch (Exception e) { logger.trace("failed to collect doc", e); } }
@Override public int advance(int targetDocID) throws IOException { if (targetDocID <= docID) { throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID); } int readerIndex = ReaderUtil.subIndex(targetDocID, leaves); if (readerIndex >= nextLeaf) { if (readerIndex == leaves.size()) { currentValues = null; docID = NO_MORE_DOCS; return docID; } currentLeaf = leaves.get(readerIndex); currentValues = currentLeaf.reader().getNumericDocValues(field); nextLeaf = readerIndex+1; if (currentValues == null) { return nextDoc(); } } int newDocID = currentValues.advance(targetDocID - currentLeaf.docBase); if (newDocID == NO_MORE_DOCS) { currentValues = null; return nextDoc(); } else { docID = currentLeaf.docBase + newDocID; return docID; } }
@Override public int advance(int targetDocID) throws IOException { if (targetDocID <= docID) { throw new IllegalArgumentException("can only advance beyond current document: on docID=" + docID + " but targetDocID=" + targetDocID); } int readerIndex = ReaderUtil.subIndex(targetDocID, leaves); if (readerIndex >= nextLeaf) { if (readerIndex == leaves.size()) { currentValues = null; docID = NO_MORE_DOCS; return docID; } currentLeaf = leaves.get(readerIndex); currentValues = currentLeaf.reader().getNormValues(field); if (currentValues == null) { return nextDoc(); } nextLeaf = readerIndex+1; } int newDocID = currentValues.advance(targetDocID - currentLeaf.docBase); if (newDocID == NO_MORE_DOCS) { currentValues = null; return nextDoc(); } else { docID = currentLeaf.docBase + newDocID; return docID; } }
/** Return null if id is not found. */ public DocIdAndVersion lookupVersion(BytesRef id, Bits liveDocs, LeafReaderContext context) throws IOException { assert context.reader().getCoreCacheKey().equals(readerKey) : "context's reader is not the same as the reader class was initialized on."; int docID = getDocID(id, liveDocs); if (docID != DocIdSetIterator.NO_MORE_DOCS) { return new DocIdAndVersion(docID, versions.get(docID), context); } else { return null; } }
boolean isTombstone(int segmentDocId) throws IOException { if (tombstoneDV == null) { return false; } assert tombstoneDV.docID() < segmentDocId; return tombstoneDV.advanceExact(segmentDocId) && tombstoneDV.longValue() > 0; }