protected BoundedPriorityQueue < Result > computeNearestNeighborsInternal ( int k , double [ ] queryVector ) throws Exception { BoundedPriorityQueue < Result > nn = new BoundedPriorityQueue < Result > ( new Result ( ) , k ) ; double lowest = Double . MAX_VALUE ; for ( int i = 0 ; i < ( vectorsList . size ( ) / vectorLength ) ; i ++ ) { boolean skip = false ; int startIndex = i * vectorLength ; double l2distance = 0 ; for ( int j = 0 ; j < vectorLength ; j ++ ) { l2distance += ( queryVector [ j ] - vectorsList . getQuick ( startIndex + j ) ) * ( queryVector [ j ] - vectorsList . getQuick ( startIndex + j ) ) ; if ( l2distance > lowest ) { skip = true ; break ; } } if ( ! skip ) { nn . offer ( new Result ( i , l2distance ) ) ; if ( i >= k ) { lowest = nn . last ( ) . getDistance ( ) ; } } } return nn ; }
Computes the k - nearest neighbors of the given query vector . The search is exhaustive but includes some optimizations that make it faster especially for high dimensional vectors .