Buggy code:
public class TopKInEachNodeHandler extends FacetResultsHandler { public TopKInEachNodeHandler ( TaxonomyReader taxonomyReader , FacetRequest facetRequest ) { } @ Override public IntermediateFacetResult fetchPartitionResult ( FacetArrays arrays , int offset ) throws IOException { int rootNode = this . taxonomyReader . getOrdinal ( this . facetRequest . getCategoryPath ( ) ) ; if ( rootNode == ( TaxonomyReader . INVALID_ORDINAL ) ) { return null ; } int K = Math . min ( facetRequest . getNumResults ( ) , taxonomyReader . getSize ( ) ) ; IntToObjectMap < TopKInEachNodeHandler . AACO > AACOsOfOnePartition = new IntToObjectMap < TopKInEachNodeHandler . AACO > ( ) ; <START_BUG> int partitionSize = arrays . getArraysLength ( ) ; <END_BUG> int depth = facetRequest . getDepth ( ) ; if ( depth == 0 ) { TopKInEachNodeHandler . IntermediateFacetResultWithHash tempFRWH = new TopKInEachNodeHandler . IntermediateFacetResultWithHash ( facetRequest , AACOsOfOnePartition ) ; if ( isSelfPartition ( rootNode , arrays , offset ) ) { tempFRWH . isRootNodeIncluded = true ; tempFRWH . rootNodeValue = this . facetRequest . getValueOf ( arrays , ( rootNode % partitionSize ) ) ; } return tempFRWH ; } if ( depth > ( ( Short . MAX_VALUE ) - 3 ) ) { depth = ( Short . MAX_VALUE ) - 3 ; } int endOffset = offset + partitionSize ; ParallelTaxonomyArrays childrenArray = taxonomyReader . getParallelTaxonomyArrays ( ) ; int [ ] children = childrenArray . children ( ) ; int [ ] siblings = childrenArray . siblings ( ) ; int totalNumOfDescendantsConsidered = 0 ; PriorityQueue < TopKInEachNodeHandler . AggregatedCategory > pq = new TopKInEachNodeHandler . AggregatedCategoryHeap ( K , this . getSuitableACComparator ( ) ) ; TopKInEachNodeHandler . AggregatedCategory [ ] reusables = new TopKInEachNodeHandler . AggregatedCategory [ 2 + K ] ; for ( int i = 0 ; i < ( reusables . length ) ; i ++ ) { reusables [ i ] = new TopKInEachNodeHandler . AggregatedCategory ( 1 , 0 ) ; } int [ ] ordinalStack = new int [ depth + 2 ] ; ordinalStack [ 0 ] = rootNode ; int localDepth = 0 ; int [ ] [ ] bestSignlingsStack = new int [ depth + 2 ] [ ] ; int [ ] siblingExplored = new int [ depth + 2 ] ; int [ ] firstToTheLeftOfPartition = new int [ depth + 2 ] ; int tosOrdinal ; ordinalStack [ ( ++ localDepth ) ] = children [ rootNode ] ; siblingExplored [ localDepth ] = Integer . MAX_VALUE ; siblingExplored [ 0 ] = - 1 ; while ( localDepth > 0 ) { tosOrdinal = ordinalStack [ localDepth ] ; if ( tosOrdinal == ( TaxonomyReader . INVALID_ORDINAL ) ) { localDepth -- ; if ( ( siblingExplored [ localDepth ] ) < 0 ) { ordinalStack [ localDepth ] = siblings [ ordinalStack [ localDepth ] ] ; continue ; } ( siblingExplored [ localDepth ] ) -- ; if ( ( siblingExplored [ localDepth ] ) == ( - 1 ) ) { ordinalStack [ localDepth ] = firstToTheLeftOfPartition [ localDepth ] ; } else { ordinalStack [ localDepth ] = bestSignlingsStack [ localDepth ] [ siblingExplored [ localDepth ] ] ; } continue ; } if ( ( siblingExplored [ localDepth ] ) == ( Integer . MAX_VALUE ) ) { while ( tosOrdinal >= endOffset ) { tosOrdinal = siblings [ tosOrdinal ] ; } double residue = 0.0F ; pq . clear ( ) ; int tosReuslables = ( reusables . length ) - 1 ; while ( tosOrdinal >= offset ) { double value = facetRequest . getValueOf ( arrays , ( tosOrdinal % partitionSize ) ) ; if ( value != 0 ) { totalNumOfDescendantsConsidered ++ ; TopKInEachNodeHandler . AggregatedCategory ac = reusables [ ( tosReuslables -- ) ] ; ac . ordinal = tosOrdinal ; ac . value = value ; ac = pq . insertWithOverflow ( ac ) ; if ( null != ac ) { residue += ac . value ; totalNumOfDescendantsConsidered -- ; totalNumOfDescendantsConsidered += countOnly ( ac . ordinal , children , siblings , arrays , partitionSize , offset , endOffset , localDepth , depth ) ; reusables [ ( ++ tosReuslables ) ] = ac ; } } tosOrdinal = siblings [ tosOrdinal ] ; } firstToTheLeftOfPartition [ localDepth ] = tosOrdinal ; int aaci = pq . size ( ) ; int [ ] ords = new int [ aaci ] ; double [ ] vals = new double [ aaci ] ; while ( aaci > 0 ) { TopKInEachNodeHandler . AggregatedCategory ac = pq . pop ( ) ; ords [ ( -- aaci ) ] = ac . ordinal ; vals [ aaci ] = ac . value ; reusables [ ( ++ tosReuslables ) ] = ac ; } if ( ( ords . length ) > 0 ) { AACOsOfOnePartition . put ( ordinalStack [ ( localDepth - 1 ) ] , new TopKInEachNodeHandler . AACO ( ords , vals , residue ) ) ; bestSignlingsStack [ localDepth ] = ords ; siblingExplored [ localDepth ] = ( ords . length ) - 1 ; ordinalStack [ localDepth ] = ords [ ( ( ords . length ) - 1 ) ] ; } else { ordinalStack [ localDepth ] = tosOrdinal ; siblingExplored [ localDepth ] = - 1 ; } continue ; } if ( localDepth >= depth ) { ordinalStack [ ( ++ localDepth ) ] = TaxonomyReader . INVALID_ORDINAL ; continue ; } ordinalStack [ ( ++ localDepth ) ] = children [ tosOrdinal ] ; siblingExplored [ localDepth ] = Integer . MAX_VALUE ; } TopKInEachNodeHandler . IntermediateFacetResultWithHash tempFRWH = new TopKInEachNodeHandler . IntermediateFacetResultWithHash ( facetRequest , AACOsOfOnePartition ) ; if ( isSelfPartition ( rootNode , arrays , offset ) ) { tempFRWH . isRootNodeIncluded = true ;

Fix:
int partitionSize = arrays . arrayLength ;