protected final void addAllDocVectors(Fields vectors,MergeState mergeState) throws IOException {
  if (vectors == null) {
    startDocument(0);
    finishDocument();
    return;
  }
  int numFields=vectors.size();
  if (numFields == -1) {
    numFields=0;
    for (final Iterator<String> it=vectors.iterator(); it.hasNext(); ) {
      it.next();
      numFields++;
    }
  }
  startDocument(numFields);
  String lastFieldName=null;
  TermsEnum termsEnum=null;
  PostingsEnum docsAndPositionsEnum=null;
  int fieldCount=0;
  for (  String fieldName : vectors) {
    fieldCount++;
    final FieldInfo fieldInfo=mergeState.mergeFieldInfos.fieldInfo(fieldName);
    assert lastFieldName == null || fieldName.compareTo(lastFieldName) > 0 : "lastFieldName=" + lastFieldName + " fieldName="+ fieldName;
    lastFieldName=fieldName;
    final Terms terms=vectors.terms(fieldName);
    if (terms == null) {
      continue;
    }
    final boolean hasPositions=terms.hasPositions();
    final boolean hasOffsets=terms.hasOffsets();
    final boolean hasPayloads=terms.hasPayloads();
    assert !hasPayloads || hasPositions;
    int numTerms=(int)terms.size();
    if (numTerms == -1) {
      numTerms=0;
      termsEnum=terms.iterator();
      while (termsEnum.next() != null) {
        numTerms++;
      }
    }
    startField(fieldInfo,numTerms,hasPositions,hasOffsets,hasPayloads);
    termsEnum=terms.iterator();
    int termCount=0;
    while (termsEnum.next() != null) {
      termCount++;
      final int freq=(int)termsEnum.totalTermFreq();
      startTerm(termsEnum.term(),freq);
      if (hasPositions || hasOffsets) {
        docsAndPositionsEnum=termsEnum.postings(docsAndPositionsEnum,PostingsEnum.OFFSETS | PostingsEnum.PAYLOADS);
        assert docsAndPositionsEnum != null;
        final int docID=docsAndPositionsEnum.nextDoc();
        assert docID != DocIdSetIterator.NO_MORE_DOCS;
        assert docsAndPositionsEnum.freq() == freq;
        for (int posUpto=0; posUpto < freq; posUpto++) {
          final int pos=docsAndPositionsEnum.nextPosition();
          final int startOffset=docsAndPositionsEnum.startOffset();
          final int endOffset=docsAndPositionsEnum.endOffset();
          final BytesRef payload=docsAndPositionsEnum.getPayload();
          assert !hasPositions || pos >= 0;
          addPosition(pos,startOffset,endOffset,payload);
        }
      }
      finishTerm();
    }
    assert termCount == numTerms;
    finishField();
  }
  assert fieldCount == numFields;
  finishDocument();
}

protected final void addAllDocVectors ( Fields vectors , MergeState mergeState ) throws IOException { if ( vectors == null ) { startDocument ( 0 ) ; finishDocument ( ) ; return ; } int numFields = vectors . size ( ) ; if ( numFields == - 1 ) { numFields = 0 ; for ( final Iterator < String > it = vectors . iterator ( ) ; it . hasNext ( ) ; ) { it . next ( ) ; numFields ++ ; } } startDocument ( numFields ) ; String lastFieldName = null ; TermsEnum termsEnum = null ; PostingsEnum docsAndPositionsEnum = null ; int fieldCount = 0 ; for ( String fieldName : vectors ) { fieldCount ++ ; final FieldInfo fieldInfo = mergeState . mergeFieldInfos . fieldInfo ( fieldName ) ; assert lastFieldName == null || fieldName . compareTo ( lastFieldName ) > 0 : "lastFieldName=" + lastFieldName + " fieldName=" + fieldName ; lastFieldName = fieldName ; final Terms terms = vectors . terms ( fieldName ) ; if ( terms == null ) { continue ; } final boolean hasPositions = terms . hasPositions ( ) ; final boolean hasOffsets = terms . hasOffsets ( ) ; final boolean hasPayloads = terms . hasPayloads ( ) ; assert ! hasPayloads || hasPositions ; int numTerms = ( int ) terms . size ( ) ; if ( numTerms == - 1 ) { numTerms = 0 ; termsEnum = terms . iterator ( ) ; while ( termsEnum . next ( ) != null ) { numTerms ++ ; } } startField ( fieldInfo , numTerms , hasPositions , hasOffsets , hasPayloads ) ; termsEnum = terms . iterator ( ) ; int termCount = 0 ; while ( termsEnum . next ( ) != null ) { termCount ++ ; final int freq = ( int ) termsEnum . totalTermFreq ( ) ; startTerm ( termsEnum . term ( ) , freq ) ; if ( hasPositions || hasOffsets ) { docsAndPositionsEnum = termsEnum . postings ( docsAndPositionsEnum , PostingsEnum . OFFSETS | PostingsEnum . PAYLOADS ) ; assert docsAndPositionsEnum != null ; final int docID = docsAndPositionsEnum . nextDoc ( ) ; assert docID != DocIdSetIterator . NO_MORE_DOCS ; assert docsAndPositionsEnum . freq ( ) == freq ; for ( int posUpto = 0 ; posUpto < freq ; posUpto ++ ) { final int pos = docsAndPositionsEnum . nextPosition ( ) ; final int startOffset = docsAndPositionsEnum . startOffset ( ) ; final int endOffset = docsAndPositionsEnum . endOffset ( ) ; final BytesRef payload = docsAndPositionsEnum . getPayload ( ) ; assert ! hasPositions || pos >= 0 ; addPosition ( pos , startOffset , endOffset , payload ) ; } } finishTerm ( ) ; } assert termCount == numTerms ; finishField ( ) ; } assert fieldCount == numFields ; finishDocument ( ) ; }
safe ( but , slowish ) default method to write every vector field in the document .
